Sequential array merging, for example:
Array a=[100, 89, 88, 67, 65, 34],
B=[120, 110, 103, 79, 66, 35, 20]
Combined results result=[120, 110, 103, 100, 89, 88, 79, 67, 66, 65, 35, 34, 20]
Program:
Importjava.util.Arrays; Public classTest { Public Static voidMain (string[] args) {int[] A = {100, 89, 88, 67, 65, 34 }; int[] B = {120, 110, 103, 79 }; intA_len =a.length; intB_len =b.length; int[] result =New int[A_len +B_len]; //I: Used to mark a array J: Used to mark the B array K: Used to mark the incoming array inti = 0; intj = 0; intK = 0; while(I < A_len && J <B_len) { if(A[i] >=B[i]) result[k+ +] = a[i++]; Elseresult[k+ +] = b[j++]; } //A while loop is used to ensure that the elements in the remaining array are passed through after two arrays are compared . while(I <A_len) {Result[k+ +] = a[i++]; } while(J <B_len) {Result[k+ +] = b[j++]; } System.out.println (Arrays.tostring (a)); System.out.println (arrays.tostring (b)); System.out.println (arrays.tostring (result)); }}
Results:
[120, the ",", ","), "the", "the", "the" [], the 103, the 89, the 88, the 79, the 67, the 66, 34, 20]
Java ordered array merging