views:

77

answers:

2

Hello, everyone!

I have to refactor an existing project, which is not that small. It contains a lot of arrays declarations and accesses:
(X is not a generic type, it's just a placeholder).
declarations: X[] and X[][],
access: someArray[i] and someArray[i][j].

I have to rewrite everything to use generic Lists:
declaration: List<X> and List<List<X>>,
access: someList.get(i) and someList.get(i).get(j).

I couldn't find a possibility to automatize such refactoring, neither in Eclipse, nor in Netbeans (both newest versions).

Are there some tools available to do such refactoring?

EDIT:
The project is very algorithm-oriented. Internal implementation of algorithms will not be touched. But the exposure to the external world must be changed. Most classes are made in such a way, that they only hold some result arrays or arrays of arrays.

+2  A: 
  1. I can hardly agree that things you are going to do could be called 'refactoring'.

  2. Arrays has some possibilities that lists don't have (and of course vise versa).

For example, if you create new array of 10 integer elements then its size is 10, and it has ten zero values.

You can also use its index in some tricky way. For example, think about radix sort algorithm. To implement it with lists you should first add a lot of zeros to this list and you'll get twice worse performance.

I'm telling this to explain the idea that it's almost impossible to implement robust tool for doing what you want to do.

Roman
+3  A: 

exposure to the external world must be changed

In that case I'd avise not to change it everywhere, but to :

  • change only return types of the public methods
  • write an utility method, which looks like:

    public static List<List<X>> asList(X[][] x) {
        List<X[]> list = Arrays.asList(x);
        List<List<X>> newList = new ArrayList<List<X>>(list.size());
        for (X[] xArray : list) {
            newList.add(Arrays.asList(xArray));
        }
        return list;
    }
    
  • use that method to change the only result of each public method. I.e.

    public List<List<X>> someAlgorithm(...) {
        // algorithm code
        X[][] result = ...;
        return Utils.asList(result); // add only this line
    }
    
Bozho
+1 although I would probably just overload the original method, instead of modifying the existing one. What if in one place you want a 2d array? Than just change the methods that use it.
Casey