Java ArrayList集合错误:我的代码的哪一部分是错误的?
我将被要求写一个移动数组列表,做一些操作,如添加,删除,更新和显示。然而,当谈到在arraylist中对对象进行排序时,我有点困惑 我指的是 驾驶舱Java ArrayList集合错误:我的代码的哪一部分是错误的?,java,arraylist,collections,Java,Arraylist,Collections,我将被要求写一个移动数组列表,做一些操作,如添加,删除,更新和显示。然而,当谈到在arraylist中对对象进行排序时,我有点困惑 我指的是 驾驶舱 //Create an arraylist of Mobile ArrayList<Mobile> themobile = new ArrayList<>(); Scanner input = new Scanner(System.in); System.out.println("Welco
//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);
}
非常感谢您的帮助和澄清。谢谢作为此代码
while (true) {
永不退出,此循环下的代码无法访问
可能系统退出(0)代码>可能只会在
循环时中断
注意在开关中断开
不会断开而循环当
循环从未到达后的代码。您需要编写一些逻辑来打破while循环。解决此问题的一个简单方法是:
int choice = -1;
while (choice != 0) {
choice = input.nextInt();
switch (choice) {
//...other cases
case 0:
System.out.println("Thank you for using a Mobile arraylist");
}
}
Collections.sort(...);
System.out.println(...);
在循环时把这些线放进去
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);
此外,您还可以使用Comparator.comparating
简化比较操作,该操作从java 8开始提供。比如:
Comparator<Mobile> comparator = Comparator.comparing(new Function<Mobile,
@Override
public String apply(Mobile t) {
return t.getBrandName();
}
}).thenComparingInt(new ToIntFunction<Mobile>() {
@Override
public int applyAsInt(Mobile t) {
return t.getModelNumber();
}
}).thenComparingInt(new ToIntFunction<Mobile>() {
@Override
public int applyAsInt(Mobile t) {
return t.getInternalMemoryS();
}
});
Collections.sort(themobile, comparator);
Comparator Comparator=Comparator.comparating(新函数)我们看不到包含Collections.sort(移动)的实际代码
实际出现。根据代码中的逻辑,很可能这一行代码在逻辑上不可访问。这就是为什么我想知道我的逻辑的哪一部分出错。请发布这一行的整个方法Collections.sort(themobile,new MobileByBrandName())
我指的是@Simone001你的问题与可比性无关
它与打破你的循环有关。
int choice = -1;
while (choice != 0) {
choice = input.nextInt();
switch (choice) {
//...other cases
case 0:
System.out.println("Thank you for using a Mobile arraylist");
}
}
Collections.sort(...);
System.out.println(...);
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);
Comparator<Mobile> comparator = Comparator.comparing(new Function<Mobile,
@Override
public String apply(Mobile t) {
return t.getBrandName();
}
}).thenComparingInt(new ToIntFunction<Mobile>() {
@Override
public int applyAsInt(Mobile t) {
return t.getModelNumber();
}
}).thenComparingInt(new ToIntFunction<Mobile>() {
@Override
public int applyAsInt(Mobile t) {
return t.getInternalMemoryS();
}
});
Collections.sort(themobile, comparator);