I want to use a collection in place of 2D array so that I don't need to give its size at the time of declaration and I can add as many elements as I want dynamically.
I'm personally using the Vector class for that purpose, though different requirements may eventually dictate the use of other, more specialized classes.
java.util.ArrayList is my preferred choice.
http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html
import java.util.ArrayList;
ArrayList is what you want, you dont need to set up its size at creation time and you can add elements dinamycally using the add() method.
The easiest way is to use nested collections... say (assuming your values are Strings) List<List<String>>
which can then be used like this:
List<List<String>> fakeArray = new ArrayList<List<String>>();
// Pretend you fill it with values between these calls
String retrieve = fakeArray.get(0).get(0);
Edit: This was originally a Map<String,List<String>>
which really doesn't make sense in this context.
However, you may want to see if Google Collections or Apache Commons Collections have something more specialized that you can use.
It depends on what you're trying to do, but I would recommend ArrayList. It's faster than Vector. Unless you care about synchronization! If you want it as a 2-dimensional list, then you create an ArrayList and each element of this list would be another ArrayList.
You could do a trial with an ArrayList
having ArrayList
s as items. If that doesn't do what you want, it will give instight in what you need to built yourself.
What do you want to be able to do with it? I would probably simply use a Collection<Collection<Element>>
(where Collection might be replaced by List).
Or you might create your own class with metods to iterate over rows or columns or all elements as needed.
It depends on the way you want to use the data structure. Your options are:
- Two lists; it's your job to synchronize between them.
- A map; instead of a key-value relationship, your map entries will simply be tuples of objects.
- A list of 2-cell object arrays; each item in the list will be an object array of size 2.
EDIT: I completely misread the question; I thought it was about a 2D array of width 2.
Having properly read the question (I hope :-)), I agree with those who said list-of-lists.
The problem with List> is you have to redimension each row if you want to redimension your matrix.
If you want to use a sparse matrix, or maybe an infinite matrix you can do something like:
class SparseMatrix<X> {
private Map<Coord, X> values = new HashMap<Coord, X>();
public SparseMatrix() {
}
public X get(int x, int y) {
return values.get(new Coord(x,y)); // null if there's no value
}
public void set(int x, int y, X value) { // you can use null (like in a List)
values.set(new Coord(x,y), value);
}
private static class Coord {
int x; int y;
public Cood(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object other) {
if (other instance of Coord) {
Coord o = (Coord) other;
return o.x == x && o.y == y;
}
return false;
}
@Override
public int hashCode() {
return o.x + o.y; // or some more clever implementation :)
}
}
}