views:

158

answers:

5

I want to create a mutli dimensional array without a fixed size.

I need to be able to add items of String[2] to it.

I have tried looking at:

private ArrayList<String[]> action = new ArrayList<String[2]>();

but that doesn't work. does anyone have any other ideas?

+1  A: 
ArrayList<String[]> action = new ArrayList<String[]>();

don't need String[2];

Zenofo
+12  A: 

Should be

private ArrayList<String[]> action = new ArrayList<String[]>();
action.add(new String[2]);
...

You can't specify the size of the array within the generic parameter, only add arrays of specific size to the list later. This also means that the compiler can't guarantee that all sub-arrays be of the same size, it must be ensured by you.

A better solution might be to encapsulate this within a class, where you can ensure the uniform size of the arrays as a type invariant.

Péter Török
A: 

1.First you creating the arraylist reference name action like ;

ArrayList action ;

2.In jdk1.5 or higher it will be accepted as ArrayList <string[]> reference name ;

3.In jdk1.4 or lower it will be accepted as ArrayList reference name ;

4.Then you specified the access specifiers namely, a.public b.private c.protected

5.public can be accessed in any where.

6.private is accessed within the class.

7.protected is accessed within the class and different package subclasses.

8.Then the reference it will be assigned in action = new ArrayList<String[]>();

9.In jvm new keyword will allocate memory in runtime for the object.

10.You should not assigned the value where as declared ,beacause you are asking without fixed size.

11.finally you can be use the add() method in arraylist.

12.Then use like action.add(new string[how much you need]).

13.it will allocate the specific memory area in heap.

AdalArasan
+3  A: 

BTW. you should prefer coding against an Interface.

private ArrayList<String[]> action = new ArrayList<String[]>();

should be

private List<String[]> action = new ArrayList<String[]>();

daniel
+1 agreed. Also, it should usually be declared final. There are very few use cases for rebinding a container variable.
dsmith
@daniel: Why is this so?
athena
@athena: See the first question and its answer in this Erich Gamma interview: http://goo.gl/fz9G
missingfaktor
+2  A: 

Since the size of your string array is fixed at compile time, you'd be better off using a structure (like Pair) that mandates exactly two fields, and thus avoid the runtime errors possible with the array approach.

Code:

Since Java doesn't supply a Pair class, you'll need to define your own.

class Pair<A, B> {
  public final A first;
  public final B second;

  public Pair(final A first, final B second) {
    this.first = first;
    this.second = second;
  }

  //
  // Override 'equals', 'hashcode' and 'toString'
  //
}

and then use it as:

List<Pair<String, String>> action = new ArrayList<Pair<String, String>>();

[ Here I used List because it's considered a good practice to program to interfaces. ]

missingfaktor
+1 I was just about to give the exact same answer. There as been much debate about whether a java.util.Pair class is needed. I think yes.
dsmith
Agreed to an extent - a Pair class that you have provided is a great start and is a nice generalization as I find that Collections/Arrays that contain other Collections/Arrays is a code smell. However, in this case, it is quite likely that the two Strings the OP is pairing together probably have some special relationship to each other, and in such case probably deserve a class of their own that specifies that relationship (and contains methods specific to the pair). It could be a subclass of Pair<String,String> of course.
whaley
Java has java.util.Map.Pair. No need to define ur own.
emory
@emory: It's `java.util.Map.Entry`. Its name indicates its purpose i.e. serve as a structure to store a key and a value together in a `Map`. It's not supposed to be used as general purpose structure to store any two elements (which is what `Pair` is for).
missingfaktor