我需要编写一个 Mobile 数组列表,执行一些操作,例如添加、删除、更新和显示。然而,当谈到对 arraylist 中的对象进行排序时,我有点困惑。
我正在谈论并参考https://beginnersbook.com/2017/08/comparator-interface-in-java/
在驾驶类中
//Create an arraylist of Mobile
ArrayList<Mobile> themobile = new ArrayList<>();
Scanner input = new Scanner(System.in);
System.out.println("Welcome to Mobile");
System.out.println("Please select a number in the following: ");
while (true) {
//A list of options
System.out.println("1. Diplay the next mobile.");
System.out.println("2. Display the previous mobile.");
System.out.println("3. Add a new mobile.");
System.out.println("4. Remove a new mobile");
System.out.println("5. Update a mobile.");
System.out.println("0. Exit");
//prompt user input
int choice = input.nextInt();
switch(choice) {
case 1:
//displayNext(themobile);
break;
case 2:
//displayPrevious(themobile);
break;
case 3:
addMobile(themobile);
break;
case 4:
removeMobile(themobile);
break;
case 5:
updateMobile(themobile);
break;
case 0:
System.out.println("Thank you for using a Mobile arraylist");
System.exit(0);
}
}
Collections.sort((themobile, new MobileByBrandName());
System.out.println("Sorted by brand name" + themobile);
Collections.sort(themobile, new MobileByMoNum());
System.out.println("Sorted by brand name" + themobile);
Collections.sort(themobile, new MobileByInS());
System.out.println("Sorted by brand name" + themobile);
}
FFIVE
千巷猫影
LEATH
相关分类