Take the 2-minute tour ×

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?

share|improve this question
add comment

5 Answers

up vote 37 down vote accepted

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.

share|improve this answer
add comment

BTW. you should prefer coding against an Interface.


Should be

private List<String[]> action = new ArrayList<String[]>();
share|improve this answer
+1 agreed. Also, it should usually be declared final. There are very few use cases for rebinding a container variable. –  dsmith Sep 4 '10 at 15:36
Why is this so? –  athena Sep 4 '10 at 15:40
@athena: See the first question and its answer in this Erich Gamma interview: goo.gl/fz9G –  missingfaktor Sep 4 '10 at 16:29
add comment

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.


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. ]

share|improve this answer
+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 Sep 4 '10 at 15:43
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 Sep 4 '10 at 22:18
Java has java.util.Map.Pair. No need to define ur own. –  emory Sep 5 '10 at 6:46
@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 Sep 5 '10 at 6:56
add comment

Don't need String[2];

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer


