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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26948421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:56:11  来源:igfitidea点击:

How to create multiple arrays with a loop?

javaarrays

提问by GrumpyKitten

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:

我在 Java 中使用循环创建多个数组时遇到问题。我想要做的是创建一组数组,以便接下来的每个数组中都有 3 个以上的数字,并且所有数字都是连续的。只是为了澄清,我需要得到的是一组,比如说 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!

等等。非常感谢任何帮助!

回答by ruakh

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

为此,您需要跟踪三件事:(1) 您已经创建了多少个数组(因此您可以在 30 处停止);(2) 您所在的数组长度是多少(这样您就可以创建具有正确长度的下一个数组);和 (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;
}

回答by theonlygusti

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. The second for-loop tells us which item we are adding, and adds the correct item to that position.

第一个 for 循环通过变量 j 告诉我们当前正在向哪个数组添加项。第二个 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. 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++.

在这里,j 是 0,我们看到第一项是 1。在第一项处,i 也等于 0,所以我们可以说,这里,每一项都等于i + 1, 或i++

However, in the next array,

然而,在下一个数组中,

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

each item is not equal to i++, because ihas 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;博士

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.

有用。

回答by Sahil Jain

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

    }

回答by lthms

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

您必须使用双 for 循环。第一个循环将迭代您的数组,第二个循环将迭代它们的内容。

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:

或者,第一个 for 必须从 0 迭代到 30。第二个不太容易编写。您必须记住上次停留的位置以及上次停留的物品数量。最后,它看起来像这样:

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