第七章第三十一题(合并两个有序列表)(Merge two ordered tables)

这篇博客介绍了一个Java方法,用于合并两个已排序的整数列表,确保合并后的列表仍然有序。通过遍历两个列表并利用排序,实现了仅进行两列表长度之和次数的比较。此外,提供了一个测试程序,允许用户输入两个有序列表并展示合并结果。
摘要由CSDN通过智能技术生成

#第七章第三十一题(合并两个有序列表)(Merge two ordered tables)

  • **7.31(合并两个有序列表)编写下面的方法,将两个有序列表变成一个新的有序列表。
    public static int[] merge(int[] list1,int[] list2)
    只进行list.length+list2.length次比较来实现该方法。该实现的动画演示参见。编写一个测试程序,提示用户输入两个有序列表,然后显示合并后的列表。下面是一个运行示例。注意,输入的第一个数字表示列表元素的个数。该数字不是列表的一部分。
    Enter list1 size and contents:5 1 5 16 61 111
    Enter list2 size and contents:4 2 4 5 6
    1 2 4 5 5 6 16 61 111
    *7.31(Merge two ordered tables)Write the following method to change two ordered tables into a new ordered list.
    public static int[] merge(int[] list1,int[] list2)
    Only list.length +List2. Length times to implement the method. Animation of the implementation [for demonstration, see]( http://liveexample.pearsoncmg.com/dsanimation/MergeSortNeweBook.html )。 Write a test program, prompt the user to input two sequential tables, and then display the merged list. Here is a running example. Note that the first number you enter represents the number of list elements. The number is not part of the list.
    Enter list1 size and contents:5 1 5 16 61 111
    Enter list2 size and contents:4 2 4 5 6
    1 2 4 5 5 6 16 61 111

  • 参考代码:

    package chapter07;
    
    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Code_31 {
        public static void main(String[] args) {
            Scanner input=new Scanner(System.in);
    
            System.out.print("Enter list1 size and contents:");
            int length1=input.nextInt();
            int[] list1=new int[length1];
            for(int i=0;i<list1.length;i++){
                list1[i]=input.nextInt();
            }
    
            System.out.print("Enter list2 size and contents:");
            int length2=input.nextInt();
            int[] list2=new int[length2];
            for(int i=0;i<list2.length;i++){
                list2[i]=input.nextInt();
            }
    
            for(int i=0;i<merge(list1,list2).length;i++)
                System.out.print(merge(list1,list2)[i]+" ");
        }
    
        public static int[] merge(int[] list1,int[] list2){
            int[] list3=new int[list1.length+list2.length];
            for(int i=0;i<list1.length;i++){
                list3[i]=list1[i];
            }
            for(int i=list1.length,j=0;i<list1.length+list2.length;i++,j++){
                if(j>list2.length)
                    break;
                list3[i]=list2[j];
            }
            Arrays.sort(list3);
            return list3;
        }
    }
    
    
  • 结果显示:

    Enter list1 size and contents:5 1 5 16 61 111
    Enter list2 size and contents:4 2 4 5 6
    1 2 4 5 5 6 16 61 111 
    Process finished with exit code 0
    
    
  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值