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

2
  • 1
    You can try an ArrayList of Arrays: stackoverflow.com/questions/13665188/… Commented Jan 17, 2016 at 16:02
  • Also, you can overwrite the array once you know the larger size. A list is probably better however. Commented Jan 17, 2016 at 17:13

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I would like to have it at this size height and then each {}
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);

Comments

-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

2 Comments

Hi with new ArrayList<Integer>[height]; somehow I get the error: Cannot create a generic array of ArrayList<Integer> how can I fix that? Thanks
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.