《Java语言程序设计与数据结构》编程练习答案(第二十章)(一)

《Java语言程序设计与数据结构》编程练习答案(第二十章)(一)

英文名:Introduction to Java Programming and Data Structures, Comprehensive Version, 11th Edition

20.1

public class book {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        ArrayList<String> strings = new ArrayList<>();

        for(int i=0;i<10;i++){
            String tmp = input.nextLine();
            strings.add(tmp);
        }
        Comparator<String> c = new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        };

        strings.sort(c);
        for(int i=0;i<10;i++)
            System.out.print(strings.get(i)+" ");
    }
}

20.2

public class book {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        LinkedList<Integer> test = new LinkedList<>();
        System.out.println("Please enter numbers");
        while(true){
            int tmp = input.nextInt();
            if(tmp == 0){
                break;
            }else{
                if(!test.contains(tmp)) {
                    test.add(tmp);
                }
            }
        }
        System.out.println("The input is finished");
        Comparator<Integer> com = (o1, o2) -> o1.compareTo(o2);
        boolean loop = true;

        while(loop){

            String tmp = input.nextLine();
            switch (tmp){
                case "sort":
                    test.sort(com);
                    for(Integer i : test){
                        System.out.print(i+" ");
                    }
                    break;
                case "reverse":
                    LinkedList<Integer> newList = new LinkedList<>();
                    while (!test.isEmpty()){
                        newList.addLast(test.removeLast());
                    }
                    test = newList;
                    for (Integer i : test){
                        System.out.print(i+" ");
                    }
                    break;
                case "shuffle":
                    Collections.shuffle(test);
                    for (Integer i : test){
                        System.out.print(i+" ");
                    }
                    break;
                case "break":
                    loop = false;
                    break;
                default:
                    break;
            }
        }
    }
}

20.3

public class book {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        ArrayList<question> test = new ArrayList<>();
        test.add(new question("Liaoning","Shenyang"));
        test.add(new question("Jilin","Changchun"));
        while(true){
            question tmp = test.get((int)(test.size()*Math.random()));
            System.out.println(tmp.makeQuestion());
            String ans = input.nextLine();
            if(tmp.checkAnswer(ans)){
                System.out.println("You are right");
            }
            else{
                break;
            }
        }

    }
}

class question{

    private String province;
    private String capital;

    public question(String p, String c){
        this.province = p;
        this.capital = c;
    }

    public String makeQuestion(){
        return "What is the capital of "+province;
    }

    public boolean checkAnswer(String ans){
        return ans.equals(this.capital);
    }
}

20.4

public class book {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);


        Comparator<point> comparator = new Comparator<point>() {
            @Override
            public int compare(point o1, point o2) {
                if(o1.getY() != o2.getY()){
                    return o1.getY()-o2.getY();
                }else{
                    return o1.getX()-o2.getX();
                }
            }
        };

        point[] test = new point[100];

        for(int i=0;i<100;i++){
            test[i] = new point((int)(100*Math.random()),(int)(100*Math.random()));
        }
        Arrays.sort(test, comparator);
        for(int i=0;i<5;i++){
            System.out.println(test[i].toString());
        }
    }
}

class point{
    private int x;
    private int y;
    public point(int x, int y){
        this.x = x;
        this.y = y;
    }

    public int getX(){
        return this.x;
    }
    public int getY(){
        return this.y;
    }
    public String toString(){
        return "("+x+","+y+")";
    }
}

20.5

🐴

20.6

public class book {


    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        LinkedList<Integer> test = new LinkedList<>();
        for(int i=0;i<5000000;i++){
            test.addLast(i);
        }
        System.out.println("iterator");
        long start = System.currentTimeMillis();
        Iterator<Integer> iterator = test.iterator();
        while(iterator.hasNext()){
            iterator.next();
        }
        long end = System.currentTimeMillis();
        System.out.println("Iterator costs "+(end - start)+" ms");

        System.out.println("forEach");
        start = System.currentTimeMillis();
        test.forEach(e -> {e += 1;});
        end = System.currentTimeMillis();
        System.out.println("ForEach costs "+(end-start)+" ms");
    }
}

20.7

🐎

20.8

🦓

  • 2
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值