Java Data Structure-linear table merging and java data structure merging

Source: Internet
Author: User

Java Data Structure-linear table merging and java data structure merging

Question: we know that the data elements in the linear table LA and LB are arranged in a descending order by value. Now we need to combine la and lb into a new linear table LC, the data elements in LC are still arranged in a descending order by values. For example, Set
LA = (3, 5, 8, 11)
LB = (, 15, 20)
Then
LC = (, 20)

For linear tables, see the previous chapter. Here we use the sequential storage structure of linear tables.

Code:

Package com. yds. list; public class JavaMain {private static void MergeList (SequenceList <Integer> listA, SequenceList <Integer> listB, SequenceList <Integer> listC) {// data elements in the known linear table La and Lb are arranged in non-decreasing order by value // The new linear table Lc is obtained by combining La AND Lb, the data elements of Lc are also arranged in a non-descending order of int La_length = listA. length (); int Lb_length = listB. length (); int I = 0, j = 0; while (I <La_length & j <Lb_length) {if (listA. get (I) <listB. get (j) {listC. add (listA. get (I); I ++;} else {listC. add (listB. get (j); j ++ ;}} while (I <La_length) {listC. add (listA. get (I); I ++;} while (j <Lb_length) {listC. add (listB. get (j); j ++ ;}} public static void main (String [] args) {// TODO Auto-generated method stub SequenceList <Integer> listA = new SequenceList <Integer> (); SequenceList <Integer> listB = new SequenceList <Integer> (); sequenceList <Integer> listC = new SequenceList <Integer> (); int [] la = {3, 5, 8, 11}; int [] lb = {2, 6, 8 }; for (int I = 0; I <la. length; I ++) {listA. add (la [I]) ;}for (int I = 0; I <lb. length; I ++) {listB. add (lb [I]);} MergeList (listA, listB, listC); for (int k = 0; k <listC. length (); k ++) {System. out. println (listC. get (k ));}}}

Result:

 

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.