如何将两个或多个数组放入单个数组?

时间:2018-11-16 20:47:50

标签: java arrays

我想将三个数组的值放到一个数组中并输出,但是我不知道该怎么做。我将不胜感激任何引导我朝着正确方向前进的答案。

    // num is user entered as number of times to output
    int[] skuNum = new int[num];
    int[] letters = new int[4];
    int[] l_p_Num = new int[4];

    int count = 1;
    // Fill numbers array
    for (int s: skuNum) {
        System.out.print(String.format("%05d", (s + count)) + "     ");
        count++;
        // Fill letters array
        for(int i: letters) {
            i = (rnd.nextInt(26) + 'A');
            System.out.print((char)i);
        }
        System.out.print(" ");
        for(int q: l_p_Num) {
            q = rnd.nextInt(9) + 0;
            System.out.print(q);
        }
        System.out.println();
    }

4 个答案:

答案 0 :(得分:3)

可能的Java 8解决方案如下:

    int[] skuNum = new int[num];
    int[] letters = new int[] {1, 2, 3, 4};
    int[] l_p_Num = new int[] {11, 12, 13, 14};


    IntStream joined = IntStream.concat(IntStream.concat(IntStream.of(skuNum), IntStream.of(letters)), IntStream.of(l_p_Num));

    System.out.println(joined
            .boxed()
            .map(String::valueOf)
            .collect(Collectors.joining(",")));

答案 1 :(得分:0)

声明一个新的int[]数组以容纳所有数字,然后使用System.arraycopy()将较小的数组复制到result数组中:

int[] skuNum = new int[num]; // 1, 2
int[] letters = new int[4];  // 4, 5, 6, 7
int[] l_p_Num = new int[4];  // 9, 8, 7, 6

int[] result = new int[num + 8];
System.arraycopy(skuNum, 0, result, 0, num);
System.arraycopy(letters, 0, result, num, 4);
System.arraycopy(l_p_Num, 0, result, num + 4, 4);

System.out.println(Arrays.toString(result));

将输出:

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

答案 2 :(得分:0)

您可以使用:

int[][] arrays = {skuNum, letters, l_p_Num};

int[][] arrays = new int[][] {skuNum, letters, l_p_Num};

创建数组数组并使用for循环遍历每个数组:

for (int i = 0; i < arrays .length; i++) {
   // each array from arrays
   for (int j = 0; j < arrays [i].length; j++) {
       //each int value from each array
       System.out.println(arrays[i][j]);
   } 
}

以“ 00001 DVAH 4238”格式显示结果

for (int i=0; i<skuNum.length; i++) {
   String row = String.valueOf(skuNum[i]) + String.valueOf(letters[i]) + String.valueOf(l_p_Num[i]);
   System.out.println(row);
}

答案 3 :(得分:0)

所以这没有Java泛型,只有for循环

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

    int[] finalInt = new int[skuNum.length+letters.length+l_p_Num.length];

    for (int i = 0; i<skuNum.length;i++) {
        finalInt[i] =skuNum[i];

    }

    for (int i = 0; i<letters.length;i++) {
        finalInt[i+skuNum.length] =letters[i];

    }
    for (int i = 0; i<l_p_Num.length;i++) {
        finalInt[i+skuNum.length+letters.length] =l_p_Num[i];

    }
    for (int i : finalInt)
        System.out.print(i);

结果:

123456789

如果要在其中包含字符串,请参见以下代码:

        int[] skuNum = {1,2,3};
    String[] letters = {"asd","asd","aefuh"};
    int[] l_p_Num = {7,8,9};

    String[] finalArray = new String[skuNum.length+letters.length+l_p_Num.length];

    for (int i = 0; i<skuNum.length;i++) {
        finalArray[i] =String.valueOf(skuNum[i]);

    }

    for (int i = 0; i<letters.length;i++) {
        finalArray[i+skuNum.length] =letters[i];

    }
    for (int i = 0; i<l_p_Num.length;i++) {
        finalArray[i+skuNum.length+letters.length] = String.valueOf(l_p_Num[i]);

    }
    for (String st : finalArray)
        System.out.print(st);