Java ordered array merging

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.