简体   繁体   English

Java定义多维数组

[英]Java defining multidimensional array

I am defining an array like so: int [][] intervals = new int[10][10]; 我正在这样定义一个数组: int [][] intervals = new int[10][10]; BUT my array will have to have different dimensions, depending on who calls the function in which this array is defined. 但是我的数组必须具有不同的尺寸,具体取决于谁调用定义此数组的函数。

I wanted to try and do something like this: int [][] intervals = new int[][]; 我想尝试做这样的事情: int [][] intervals = new int[][]; but it says "Variable must provide either dimension expressions or an array initializer" 但它说“变量必须提供维表达式或数组初始化器”

I also tried int [][] intervals = null , but afterwards when i try and do intervals[3][4] = 10 it gives exception; 我也尝试过int [][] intervals = null ,但是之后,当我尝试执行intervals[3][4] = 10它给出了异常;

So how can i accomplish this? 那我该怎么做呢?

When creating a multidimensional array in Java, you have to specify at least the dimension of the outermost array. 在Java中创建多维数组时,必须至少指定最外层数组的维数。 In your case, if you want all the arrays to be of a different size, you will still need to specify how many arrays you need at all. 在您的情况下,如果希望所有阵列的大小都不同,则仍然需要指定您需要多少个阵列。 For example, if you need six arrays, you could say 例如,如果您需要六个数组,您可以说

int [][] intervals = new int[6][];

You could then fill them in like this: 然后,您可以像这样填写它们:

intervals[0] = new int[137];
intervals[1] = new int[42];
/* ... */

If you don't know in advance how many arrays you'll need, you might want to consider using an ArrayList<int[]> to explicitly add in new arrays. 如果您事先不知道需要多少个数组,则可能需要考虑使用ArrayList<int[]>显式添加新数组。 For example: 例如:

ArrayList<int[]> intervals = new ArrayList<int[]>();
intervals.add(new int[137]);
intervals.add(new int[42]);
/* ... */

Hope this helps! 希望这可以帮助!

Try to think of a multi-dimension array as an array of arrays 尝试将多维数组视为数组数组

It's not exactly like that inside the JVM, but that's the best mental model to work with for these sorts of questions. 这与JVM内部的情况并不完全相同,但这是解决此类问题的最佳心理模型。

So, if you have 所以,如果你有

int [][] intervals = null;

then your outer-array is null, and can later be initialised with 那么您的外部数组为null,以后可以使用初始化

int size = 10;
intervals = new int[size][];

Which creates an array of int[] , but in that case each of the 10 inner-arrays are null. 这将创建一个int[]数组,但在这种情况下,10个内部数组中的每一个都是null。

If you want them all to be the same size (eg an 10 x 5 array) then do something like: 如果希望它们的大小都相同(例如10 x 5阵列),请执行以下操作:

int size1 = 10;
int size2 = 5;
intervals = new int[size1][size2];

But if you want them to be different sizes then do something like: 但是,如果您希望它们的大小不同,请执行以下操作:

int size = 10;
intervals = new int[size][];
intervals[0] = new int[5];
intervals[1] = new int[4];
// etc...

But all of these assume that you know how big you want your array to be before you start filling it. 但是所有这些假设都假定您在开始填充数组之前就知道数组要多大。
If you don't know that, then you want to use a List instead - have a look at ArrayList . 如果您不知道,则想使用List -看一下ArrayList

Edit I've just seen in your comment that this latter case is what you want, so ... 编辑我刚刚在您的评论中看到了后一种情况就是您想要的,所以...

Try something like: 尝试类似:

List< List<Integer> > list = new ArrayList< List<Integer> >();


for(int i=0; i<10; i++) // You would loop over whatever input you're processing
{                       //  but I don't know what you're doing in that part of you code
                        //  so I'll just do it 10 times.

    List<Integer> innerList = new ArrayList< Integer >();

    innerList.add( 1 );
    innerList.add( 2 );
    innerList.add( 3 );

    // etc.

    list.add( innerList );
}

Alternatively, you could have: 或者,您可以拥有:

  • List< int[] > which would mean using a List for the outside collection, but an array for the inner collection, if you know how long your inner collection is going to be. List< int[] > ,这意味着对外部集合使用List,对内部集合使用数组,如果您知道内部集合将要使用多长时间。
  • List<Integer>[] which uses an array for the outer collection, and a list for the inner collection. List<Integer>[] ,它对外部集合使用数组,对内部集合使用列表。

It really depends on exactly how your code needs to work. 这实际上取决于您的代码需要如何工作。

If you want to allocate elements in array dynamically, may be you should consider ArrayList : 如果要动态分配数组中的元素,则应考虑ArrayList

ArrayList< ArrayList< int > > array = new ArrayList< ArrayList< int > >();
array.add( new ArrayList< int >() );
array.get( 0 ).add( 1 );
array.get( 0 ).add( 2 );
array.add( new ArrayList< int >() );
array.get( 1 ).add( 3 );

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

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