gpt4 book ai didi

java - 将对象添加到按字母顺序排列的数组 List

转载 作者:行者123 更新时间:2023-11-29 05:53:38 25 4
gpt4 key购买 nike

此代码要求用户输入具有名称、型号年份、上市价格和折扣百分比的车辆对象。这里出现的问题是,当用户输入上述所有信息时,汽车对象被添加到数组列表的底部,而不是按字母顺序排列。请注意,该列表之前是按字母顺序排列的。

while (!valid) {
String str = scan.nextLine();
try {
boolean found = false;
System.out.println("Enter car name: ");
name = scan.nextLine();
System.out.println("Enter car model year: ");
modelYear = scan.nextLine();
System.out.println("Enter car list price: ");
listPrice = scan.nextDouble();
System.out.println("Enter car percent discount: ");
percentDiscount = scan.nextDouble();

int i = 0;
loc = 0;
while (!found && i < carList.size()) {
String nameRetrievedFromCarList = carList.get(i).getName();
String nameToAdd = "";
if (nameToAdd.compareToIgnoreCase(nameRetrievedFromCarList) < 0) {
loc++;
found = true;

}
i++;

}// end while

Proj1CarData newCar = new Proj1CarData(name, modelYear, listPrice, percentDiscount,
discountAmount, netPrice);
carList.add(loc, newCar);

valid = true;

}// end try

catch (NumberFormatException nfe) {
System.out.println("Wrong entry: Try again");
}// end catch

}

最佳答案

虽然有点偏离主题,但您可以使用 Collections.binarySearch确定应在何处插入新值...

来自 Java 文档

Returns: the index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size() if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

Proj1CarData newCar = new Proj1CarData(name, modelYear, listPrice, percentDiscount, discountAmount, netPrice);

int index = Collections.binarySearch(carList, newCar);
if (index < 0) {
index = (index * -1) - 1;
}

carList.add(index, newCar);

这假设 Proj1CarDataComparable,否则您需要提供自己的 Comparator

Proj1CarData newCar = new Proj1CarData(name, modelYear, listPrice, percentDiscount, discountAmount, netPrice);

int index = Collections.binarySearch(carList, newCar,
new Comparator<Proj1CarData>() {
public int compare(Proj1CarData car1, Proj1CarData car2) {
return car1.getName().compareToIgnoreCase(car2.getName());
}
});

if (index < 0) {
index = (index * -1) - 1;
}

carList.add(index, newCar);

已更新

List<String> names = new ArrayList<String>(25);
names.add("Hurzdiirn");
names.add("Alydriira Talabdiira");
names.add("Urlidil Sineth");
names.add("Quavyraen Belarral");
names.add("Belarayne'bryn Agh'Quarbryn");
names.add("Alakgos");
names.add("Sszoj'hrae Laelraema");
names.add("Szornet");
names.add("Filojafay");
names.add("Lltril'net Chaszhrae");

Collections.sort(names);

for (int index = 0; index < names.size(); index++) {
String name = names.get(index);
System.out.println("[" + index + "] " + name);
}

int insertAt = Collections.binarySearch(names, "Luke");
if (insertAt < 0) {
insertAt = (insertAt * -1) - 1;
}

names.add(insertAt, "Luke");

for (int index = 0; index < names.size(); index++) {
String name = names.get(index);
System.out.println("[" + index + "] " + name);
}

Collections.sort(names);
for (int index = 0; index < names.size(); index++) {
String name = names.get(index);
System.out.println("[" + index + "] " + name);
}

关于java - 将对象添加到按字母顺序排列的数组 List,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13022073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com