0

Basically I would like to create an two-dimensional Integer array:

 this.colors = new int[height][];

I know the size of the first-dimension,

but the second dimension should be variable so that I can add values for e.g like this:

 this.colors[y].push( 40)

How can I create such an array and add values?

I tried the following: 2 dimensional array list

Community
  • 1
  • 1
John Smith
  • 6,105
  • 16
  • 58
  • 109
  • 1
    You can try an ArrayList of Arrays: http://stackoverflow.com/questions/13665188/arraylist-of-integer-arrays-in-java – AMACB Jan 17 '16 at 16:02
  • Also, you can overwrite the array once you know the larger size. A list is probably better however. – Ashwin Gupta Jan 17 '16 at 17:13

3 Answers3

2

In Java, an array has a fixed size. With your requirements, I would advice using an array of lists:

List<Integer>[] colors;

Using this, you'll be able to do such a statement:

colors[y].add(40);

Note that you could also use a int[][] multidimensional array and resize it when it's necessary with Arrays.copyOf() method.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Gaël J
  • 11,274
  • 4
  • 17
  • 32
1

Since arrays have fixed length in Java, you should work with ArrayLists here. Though you know the size of the first dimension, it's difficult to combine an array and an ArrayList in your case. So the best way would be build up your matrix with ArrayLists in both dimensions:

ArrayList<ArrayList<Integer>> colors = new ArrayList<ArrayList<Integer>>();

// initialize outer arrays
final int height = 3; // this is your first known dimension
for (int i = 0; i < height; i++) {
    colors.add(new ArrayList<Integer>());
}

// now all lists from the first dimension (height) are initialized with empty lists
// so you can put add some colors to height 0 like this ...
colors.get(0).add(1);
colors.get(0).add(4);
colors.get(0).add(5);

// ... or add one color to height 1
colors.get(1).add(7);
vchris
  • 51
  • 3
-1

Arrays in Java have a fixed size. To push as you've shown, you'd have to create a new, larger array, copy the existing entries to it, and then add your entry.

Usually, when you want to do that, you really want a List<int[]> instead, either a LinkedList<int[]> or an ArrayList<int[]>, etc. At the end, when the size won't change anymore, you can use List#toArray<T>() to get an array. But that would let you add new arrays on the fly, not new entries to an existing array.

Alternately, you may want an array of lists:

List<Integer>[] colors = new ArrayList[height];
// Oddly, we don't put <Integer> here ^

Then you can fill in each entry in the array

for (int n = 0; n < colors.length; ++n) {
    colors[n] = new ArrayList<Integer>();
}

...and then you can push (add) onto any of those lists:

colors[n].add(40);

Live Example

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Hi with `new ArrayList[height];` somehow I get the error: `Cannot create a generic array of ArrayList` how can I fix that? Thanks – John Smith Jan 17 '16 at 16:25
  • @JohnSmith: Huh, oddly we don't put the type parameter there. Just `new ArrayList[height]`. Yet another dark corner of Java generics. The array works as expected (no casting required, presumably because of the declared type of `colors`). I've added a live example. – T.J. Crowder Jan 17 '16 at 17:10