开发者

Initialising array names in java

In Java is there any way of writing a loop so that you can 开发者_JAVA技巧initialize an array of arrays quickly in java with the names 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


aioobe is correct, and you can also initialize it as:

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. 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. For instance you could use a foreach loop to loop through all arrays :

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

Regards, Stéphane


If you already have "row"-arrays, a_1...a_n, the most compact way of doing it is

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

You could obviously substitute a_1 for { 1, 2, 3 } and so on, like this:

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


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].

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.

    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:

    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;
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜