简体   繁体   English

如何使用 java 中的 do while 循环从第一个数组中填充第二个数组?

[英]How to fill second array from the first one using do while loop in java?

So basically my problem is this:所以基本上我的问题是这样的:

I have an array (the first array, array A) which has 20 elements with integer values from 0 to 20 (they can be random) like:我有一个数组(第一个数组,数组 A),它有 20 个元素,integer 值从 0 到 20(它们可以是随机的),例如:

1 2 3 4 5 6 1 2 3 4 

1 2 3 4 5 6 1 2 3 4 

I need to make another array with 40 elements (the second array, array B) which has the same values as the first array, but the catch is that the values of first array are used in second array, but in this pattern:我需要创建另一个具有 40 个元素的数组(第二个数组,数组 B),它与第一个数组具有相同的值,但问题是第一个数组的值用于第二个数组,但是在这种模式下:

1 1 2 2 3 3 4 4 5 5
6 6 1 1 2 2 3 3 4 4
5 5 6 6 1 1 2 2 3 3
4 4 5 5 6 6 1 1 2 2

Basically, the second array's values from the start are:基本上,第二个数组的值从一开始就是:

B[0] = A[0]
B[1] = A[0]
B[2] = A[1]
B[3] = A[1]
etc.

and I need to achieve it by using do while loop.我需要通过使用 do while 循环来实现它。

     do {
        i++;
        B[i] = A[i];
        if(i>1) {
            B[i] = A[i-1];
        }
        System.out.print("\t" + B[i]+ " ");
        }
        while(i < 20);

Right now I am stuck with this, I can't figure out the part which states how the second array(B) is filled out.现在我被这个困住了,我无法弄清楚说明第二个数组(B)是如何填充的部分。 Any help is appreciated!任何帮助表示赞赏!

Similar to a for:类似于 for:

 int[] A = {1, 2, 3, 4, 5, 6, 1, 2, 3, 4};
 int[] B = new int[A.length * 2];
 int index_a = 0; 
 int index_b = 0;
 do {
      B[index_b++] = B[index_b++] = A[index_a++];
 }
 while(index_a < A.length);
 System.out.println(Arrays.toString(B));

You need two initial condition in this case index_a ;在这种情况下,您需要两个初始条件index_a and index_b the operation that you want to doindex_b你想要做的操作

     B[index_b++] = B[index_b++] = A[index_a++];

and the loop condition index_a < A.length .和循环条件index_a < A.length

This B[index_b++] = B[index_b++] = A[index_a++];这个B[index_b++] = B[index_b++] = A[index_a++]; is the same as是相同的

B[index_b] = A[index_a];
index_b++;
B[index_b] = A[index_a];
index_b++;
index_a++;

You may do something like this:你可以这样做:

int[] A = {1,2,3,4,5,6,1,2,3,4};
int[] B = new int[A.length * 2];
int i = 0;

do {
    int index = 2 * i;
    B[index] = A[i];
    B[index + 1] = A[i];
    i++;
} while(i < A.length);

What you do here is map each element from A to 2x and 2x + 1 of its index.您在这里所做的是 map 每个元素从 A 到2x2x + 1的索引。 Then you may fill in the "gaps" like you intended.然后您可以按照您的意图填写“空白”。

Here's an example of a do-while loop that does exactly what you asked.这是一个 do-while 循环的示例,它完全按照您的要求执行。

int[] A = {1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4};
int[] B = new int[A.length*2];

int i = 0;
do {
    // Iterates through 0 & 1, then 2 & 3, etc.
    B[i*2] = A[i];
    B[i*2+1] = A[i];
    i++;
} while (i < A.length);

for (int num : B) {
    System.out.printf("%d ", num);
}

This prints 1 1 2 2 3 3 4 4 5 5 6 6 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 5 5 6 6 1 1 2 2 3 3 4 4 .这将打印1 1 2 2 3 3 4 4 5 5 6 6 1 1 2 2 3 3 4 4 1 1 2 2 3 3 4 4 5 5 6 6 1 1 2 2 3 3 4 4

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

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