Menu
Question Index
...

What will be the output of the following program?

import java.util.*;
public class ArrangeAndRearrange {
    public static void main(String[] args) {
        List<A> list = new ArrayList<A>();
        list.add(new A(15, 4, 'A', 'C', "Zaw"));
        list.add(new A(10, 0, 'B', 'C', "Caw"));
        list.add(new A(11, 1, 'D', 'E', "Raw"));
        list.add(new A(-61, 0, 'A', 'C', "Kite"));
        Collections.sort(list);
        System.out.println(list);
        Collections.sort(list, new Arranger());
        System.out.println(list);
        Collections.sort(list, new Arranger2());
        System.out.println(list);
        Collections.sort(list, new Arranger3());
        System.out.println(list);
    }
}
class A implements Comparable<A> {
    int i, k;
    char a, b;
    String s;
    public A(int i, int k, char a, char b, String s) {
        super();
        this.i = i;    this.k = k;    this.a = a;    this.b = b;    this.s = s;
    }
    public int compareTo(A other) {    return ((i + k) - (other.i + other.k));    }
    public String toString() {    return "" + i + k + "~" + a + b + "~" + s;    }
}
class Arranger implements Comparator<A> {
    public int compare(A arg0, A arg1) {    return arg0.a - arg1.a;    }
}
class Arranger2 implements Comparator<A> {
    public int compare(A arg0, A arg1) {    return arg0.s.compareTo(arg1.s);    }
}
class Arranger3 implements Comparator<A> {
    public int compare(A arg0, A arg1) {    return arg1.compareTo(arg0);    }
}


[-610~AC~Kite, 100~BC~Caw, 111~DE~Raw, 154~AC~Zaw]
[-610~AC~Kite, 154~AC~Zaw, 100~BC~Caw, 111~DE~Raw]
[100~BC~Caw, -610~AC~Kite, 111~DE~Raw, 154~AC~Zaw]
[154~AC~Zaw, 111~DE~Raw, 100~BC~Caw, -610~AC~Kite]
[-61~AC~Kite, 10~BC~Caw, 12~DE~Raw, 19~AC~Zaw]
[-61~AC~Kite, 19~AC~Zaw, 10~BC~Caw, 12~DE~Raw]
[10~BC~Caw, -61~AC~Kite, 12~DE~Raw, 19~AC~Zaw]
[19~AC~Zaw, 12~DE~Raw, 10~BC~Caw, -61~AC~Kite]
[-610~AC~Kite, 100~BC~Caw, 111~DE~Raw, 154~AC~Zaw]
[100~BC~Caw, -610~AC~Kite, 111~DE~Raw, 154~AC~Zaw]
[-610~AC~Kite, 154~AC~Zaw, 100~BC~Caw, 111~DE~Raw]
[-610~AC~Kite, 100~BC~Caw, 111~DE~Raw, 154~AC~Zaw]
Output can not be determined
Some other output
Compilation Error Runtime Error

Doubts

Problems

Topic: Java Collection Framework - Collections In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App