Combine two integer arrays into one array in java

后端 未结 12 2190
借酒劲吻你
借酒劲吻你 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:51

    Merge two array without arraylist.

        public class Main {
    
    
        static int a[] = {1, 2, 3, 4};
        static int b[] = {5, 6, 7, 8};
    
        public static void main(String[] args) {
    
            System.out.println("Hello World!");
            int totalLengh = a.length + b.length;
    
            int c[] = new int[totalLengh];
    
            int j = 0;
            for (int i = 0; i < totalLengh; i++) {
    
                if (i < a.length) {
    
                    c[i] = a[i];
    
                } else {
                    c[i] = b[j];
                    j++;
                }
    
                System.out.println("" + c[i]);
            }
        }
    }
    

提交回复
热议问题