views:

282

answers:

9

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.

A: 

I'm personally using the Vector class for that purpose, though different requirements may eventually dictate the use of other, more specialized classes.

moritz
Vector is pretty much deprecated and has been replaced by ArrayList.
Yoni
@mortiz - `java.util.Vector` should generally be avoided - its thread safety isn't very useful for most multi-threaded scenarios and just adds overhead to single-threaded usage. Most people use `ArrayList` as their default `List` type instead.
McDowell
learned something, thanks!
moritz
A: 

java.util.ArrayList is my preferred choice.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/ArrayList.html

Ben Hanzl
I think it is used when we want to have dynamic 1-D array
Yatendra Goel
You can have an ArrayList that each of its element would be another ArrayList (as rsp and me are proposing in our answers). In this way, an ArrayList can be used for 2D arrays as well.
Alex
A: 

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.

sap
A: 

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.

R. Bemrose
A: 

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.

Alex
A: 

You could do a trial with an ArrayList having ArrayLists as items. If that doesn't do what you want, it will give instight in what you need to built yourself.

rsp
+1  A: 

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.

Rasmus Kaj
A: 

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.

Eli Acherkan
+1  A: 

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 :)
    }

  }
}
helios
Your hashCode may overflow - however you *do* have commented it accordingly... :)
Esko
Sharp view!!! There's an utility method: java.util.Arrays.hashCode(int[]) that makes a hashcode for an int[] but of course, it wouldn't be nice to create an array each time the hashChode is needed (or to have it created). Maybe we could copy it's implementation for only two integers: (31 + o.x) * 31 + o.y. But it seems to overflow anyway... (I think it doesn't matters, is it cyclic?).
helios