简体   繁体   English

固定的二维Int数组

[英]Two Dimensional Int array, fixed,

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 eg 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维数组列表

In Java, an array has a fixed size. 在Java中,数组的大小固定。 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. 请注意,您还可以使用int[][]多维数组,并在需要时使用Arrays.copyOf()方法调整其大小。

Since arrays have fixed length in Java, you should work with ArrayLists here. 由于数组在Java中具有固定的长度,因此您应在此处使用ArrayLists。 Though you know the size of the first dimension, it's difficult to combine an array and an ArrayList in your case. 尽管您知道第一个维度的大小,但是要结合使用数组和ArrayList很难。 So the best way would be build up your matrix with ArrayLists in both dimensions: 因此,最好的方法是在两个维度上都使用ArrayLists建立矩阵:

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);

Arrays in Java have a fixed size. Java中的数组具有固定大小。 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. 要按照显示的方式进行push ,您必须创建一个更大的新数组,将现有条目复制到其中,然后添加您的条目​​。

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. 通常,当您要这样做时,实际上需要List<int[]>而不是LinkedList<int[]>ArrayList<int[]>等。最后,当大小不进行更改后,可以使用List#toArray<T>()获得一个数组。 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: ...然后您可以将( add )推送到任何这些列表中:

colors[n].add(40);

Live Example 现场例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM