简体   繁体   中英

How to create multiple arrays with a loop?

I am having trouble creating multiple arrays with a loop in Java. What I am trying to do is create a set of arrays, so that each following array has 3 more numbers in it, and all numbers are consecutive. Just to clarify, what I need to get is a set of, let's say 30 arrays, so that it looks like this:

[1,2,3]
[4,5,6,7,8,9]
[10,11,12,13,14,15,16,17,18]
[19,20,21,22,23,24,25,26,27,28,29,30]
....

And so on. Any help much appreciated!

To do this, you need to keep track of three things: (1) how many arrays you've already created (so you can stop at 30); (2) what length of array you're on (so you can create the next array with the right length); and (3) what integer-value you're up to (so you can populate the next array with the right values).

Here's one way:

private Set<int[]> createArrays() {
    final Set<int[]> arrays = new HashSet<int[]>();
    int arrayLength = 3;
    int value = 1;
    for (int arrayNum = 0; arrayNum < 30; ++arrayNum) {
        final int[] array = new int[arrayLength];
        for (int j = 0; j < array.length; ++j) {
            array[j] = value;
            ++value;
        }
        arrays.add(array);
        arrayLength += 3;
    }
    return arrays;
}

I don't think that you can "create" arrays in java, but you can create an array of arrays, so the output will look something like this:

[[1,2,3],[4,5,6,7,8,9],[10,11,12,13...]...]

you can do this very succinctly by using two for-loops

Quick Answer

==================

int arrays[][] = new int[30][];
for (int j = 0; j < 30; j++){
  for (int i = 0; i < (j++)*3; i++){
    arrays[j][i] = (i++)+j*3;
  }
}

the first for-loop tells us, via the variable j, which array we are currently adding items to. The second for-loop tells us which item we are adding, and adds the correct item to that position.

All you have to remember is that j++ means j + 1 .

Now, the super long-winded explanation:

I've used some simple (well, I say simple, but...) maths to generate the correct item each time:

[1,2,3]

here, j is 0, and we see that the first item is one. At the first item, i is also equal to 0, so we can say that, here, each item is equal to i + 1 , or i++ .

However, in the next array,

[4,5,6,7,8,9]

each item is not equal to i++ , because i has been reset to 0. However, j=1 , so we can use this to our advantage to generate the correct elements this time: each item is equal to (i++)+j*3 .

Does this rule hold up?

Well, we can look at the next one, where j is 2:

[10,11,12,13,14...]

i = 0, j = 2 and 10 = (0+1)+2*3, so it still follows our rule.

That's how I was able to generate each element correctly.

tl;dr

int arrays[][] = new int[30][];
for (int j = 0; j < 30; j++){
  for (int i = 0; i < (j++)*3; i++){
    arrays[j][i] = (i++)+j*3;
  }
}

It works.

Do you need something like this?

    int size = 3;
    int values = 1;
    for (int i = 0; i < size; i = i + 3) {
        int[] arr = new int[size];
        for (int j = 0; j < size; j++) {
            arr[j] = values;
            values++;
        }
        size += 3;
        int count = 0;
        for (int j : arr) { // for display
            ++count;
            System.out.print(j);
            if (count != arr.length) {
                System.out.print(" , ");
            }
        }
        System.out.println();

        if (i > 6) { // to put an end to endless creation of arrays
            break;
        }

    }

You have to use a double for loop. First loop will iterate for your arrays, second for their contents.

Sor the first for has to iterate from 0 to 30. The second one is a little less easy to write. You have to remember where you last stop and how many items you had in the last one. At the end, it will look like that:

int base = 1;
int size = 3;
int arrays[][] = new int[30][];    

for(int i = 0; i < 30; i++) {
    arrays[i] = new int[size];

    for(int j = 0; j < size; j++) {
        arrays[i][j] = base;
        base++;
    }
    size += 3;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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