Combine two integer arrays into one array in java

后端 未结 12 2181
借酒劲吻你
借酒劲吻你 2020-12-21 04:03

I\'ve seen similar questions and none provide the answer that I\'m looking for, so I apologize in advance if this is considered a duplicate. I\'m trying to combine arrays {1

12条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-21 04:26

    Please try this code, I hope it's useful for you

    String a[] = new String[4];
        String b[] = new String[2];
        String[] ab = new String[a.length + b.length];
        int i, j, d, s = 0;
        @SuppressWarnings("resource")
        Scanner x = new Scanner(System.in);
        System.out.println("Enter the first array");
    
        for (i = 0; i < a.length; i++) {
            a[i] = x.next();
            for (d = i; d < a.length; d++) {
                ab[d] = a[i];
            }
        }
    
        System.out.println("Enter the second array");
    
        for (j = 0; j < b.length; j++) {
            b[j] = x.next();
            for (d = a.length + j; d < ab.length; d++)
                ab[d] = b[j];
        }
        System.out.println();
        System.out.println("The new array is !!");
        System.out.println("--------------------");
        for (s = 0; s < ab.length; s++) {
            System.out.print(ab[s] + " ");
        }
    

提交回复
热议问题