简体   繁体   English

在Java中初始化数组名称

[英]Initialising array names in java

In Java is there any way of writing a loop so that you can initialize an array of arrays quickly in java with the names x{a_1[], a_2[], a_3[], ... , a_n[]} . 在Java中,可以使用任何方法编写循环,以便您可以在Java中快速初始化名称为x{a_1[], a_2[], a_3[], ... , a_n[]}的数组。 Or would it have to be done by just typing them in? 还是仅需输入即可完成?

I have written a new question that might clear up what I trying to acheive. 我写了一个新问题,可能会弄清楚我要达到的目标。 Java Poset simulation Java Poset模拟

If you already have "row"-arrays, a_1 ... a_n , the most compact way of doing it is 如果您已经具有“行”数组a_1 ... a_n ,则最紧凑的方法是

int[] a_1 = { 1, 2, 3 };
int[] a_2 = { 4, 5, 6 };
int[] a_3 = { 7, 8, 9 };

int[][] matrix = { a_1, a_2, a_3 };

Even if you use a loop, you'll still need to specify which arrays a_1 , a_2 , and so on, you wish to iterate over (so there's no way around mentioning them all). 即使使用循环,您仍然需要指定a_1a_2等等数组,您希望对其进行迭代(因此无法一概而论)。

You could obviously substitute a_1 for { 1, 2, 3 } and so on, like this: 您显然可以用a_1代替{ 1, 2, 3 } ,依此类推:

int[][] matrix = { { 1, 2, 3 },
                   { 4, 5, 6 },
                   { 7, 8, 9 } };

aioobe is correct, and you can also initialize it as: aioobe是正确的,您也可以将其初始化为:

int[][] matrix = {
        { 1, 2, 3 },
        { 4, 5, 6 },
        { 7, 8, 9 }
    };

Althouh your question is not clear at all, I will try giving it an answer. 您的问题还不清楚,我将尽力回答。

In java, 2D arrays are treated as arrays of arrays. 在Java中,二维数组被视为数组的数组。 If you want to get references on an array of arrays, you can use a 2D array variables and use each of its elements as an array. 如果要获取数组数组的引用,则可以使用2D数组变量,并将其每个元素用作数组。 For instance you could use a foreach loop to loop through all arrays : 例如,您可以使用foreach循环遍历所有数组:

int[][] foo = new int[ 10 ][ 20 ];
for( int[] arrayInFoo : foo )
{
  arrayInFoo[ 0 ] = ...;
  arrayInFoo[ 1 ] = ...;
  ...
  arrayInFoo[ 9 ] = ...;
}//for

Regards, Stéphane 此致,Stéphane

A two-dimensional array is an array of arrays. 二维数组是数组的数组。

// declare & allocate
int[][] x = new int[5][4]; 

// assign value in 3rd position of the 2nd array
x[1][2] = 5; 

// create array containing 1 & 2 in the first "row" (or array) 
// and 3 & 4 in the second one.
int[][] x = new int {{1,2}, {3,4}};

// create an array of 2 arrays of different size:
int[][] x = new int[2][];
x[0] = new int[4];
x[1] = new int[2];

What do you mean with a named array? 你说一个命名数组是什么意思?

a_1 in your case will be x[0]. 您的情况下,a_1为x [0]。

Closed you can get is this: 关闭您可以得到的是这样的:

    int a1[], a2[];
    int aa[][] = { (a1 = new int[] { 1 }), a2 = new int[] { 2, 3 } };

But the array of arrays hardly add value here. 但是数组数组很难在这里增加价值。 If you just want to init a multidimensional array, do it like this: 如果您只想初始化多维数组,请按照以下步骤操作:

    int ba[][] = { { 1 }, { 2, 3 }, { 2, 3, 4 }, { 2, 3, 4 } };

You can also fill it with the same value using Arrays , sadly it only support the first level. 您也可以使用Arrays用相同的值填充它,可惜它仅支持第一级。

    int c1[] = new int[5];
    Arrays.fill(c1, 5);
    int ca[][] = { Arrays.copyOf(c1, 5),
                   Arrays.copyOf(c1, 5),
                   Arrays.copyOf(c1, 5) };

Or: 要么:

    int da[][] = new int[5][5];
    for (int i = 0; i < da.length; i++) {
        Arrays.fill(da[i], 5);
    }

Or possibly: 或者可能:

    int ea[][] = new int[5][5];
    for (int i = 0; i < ea.length; i++) {
        for (int j = 0; j < ea[i].length; j++) {
            ea[i][j] = 5;
        }
    }

With foreach: 与foreach:

    int fa[][] = new int[5][5];
    for (int[] is : fa) {
        Arrays.fill(is, 5);
    }

and: 和:

    int ga[][] = new int[5][5];
    for (int[] is : ga) {
        for (int i : is) {
            i = 5;
        }
    }

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

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