简体   繁体   English

如何用循环创建多个数组?

[英]How to create multiple arrays with a loop?

I am having trouble creating multiple arrays with a loop in Java. 我在用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. 我想做的是创建一组数组,以便每个后续数组中都有3个以上的数字,并且所有数字都是连续的。 Just to clarify, what I need to get is a set of, let's say 30 arrays, so that it looks like this: 为了明确起见,我需要得到一组(假设有30个)数组,因此它看起来像这样:

[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); 为此,您需要跟踪以下三件事:(1)您已经创建了多少个数组(因此可以在30个位置停止); (2) what length of array you're on (so you can create the next array with the right length); (2)您使用的数组长度(因此您可以创建长度合适的下一个数组); and (3) what integer-value you're up to (so you can populate the next array with the right values). (3)您要达到的整数值(以便可以使用正确的值填充下一个数组)。

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: 我认为您无法在Java中“创建”数组,但是可以创建数组的数组,因此输出看起来像这样:

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

you can do this very succinctly by using two for-loops 您可以使用两个for循环非常简洁地执行此操作

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. 第一个for循环通过变量j告诉我们当前要向其中添加项目的数组。 The second for-loop tells us which item we are adding, and adds the correct item to that position. 第二个for循环告诉我们我们要添加哪个项目,并将正确的项目添加到该位置。

All you have to remember is that j++ means j + 1 . 您只需要记住, j++就是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. 在这里,j是0,我们看到第一项是1。 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++ . 在第一个项目中,i也等于0,所以我们可以说这里的每个项目都等于i + 1i++

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 . 每个项目都不等于i++ ,因为i已被重置为0。但是, j=1 ,所以我们可以利用它来产生这次正确的元素:每个项目等于(i++)+j*3

Does this rule hold up? 这个规则成立吗?

Well, we can look at the next one, where j is 2: 好吧,我们来看下一个,其中j为2:

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

i = 0, j = 2 and 10 = (0+1)+2*3, so it still follows our rule. i = 0,j = 2和10 =(0 + 1)+ 2 * 3,因此它仍然遵循我们的规则。

That's how I was able to generate each element correctly. 这就是我能够正确生成每个元素的方式。

tl;dr 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. 您必须使用double for循环。 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. 不好意思,第一个for必须从0到30进行迭代。第二个则不太容易编写。 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;
}

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

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