gpt4 book ai didi

java - 通过增加价格对一组对象进行排序

转载 作者:行者123 更新时间:2023-11-30 06:46:39 25 4
gpt4 key购买 nike

for (int i = 0; i < newBookCatalog.length; i++) {
double currentMin = newBookCatalog[i].getPrice();
int currentMinIndex = i;
for (int j = i + 1; j < newBookCatalog.length; j++) {
if (currentMin > newBookCatalog[j].getPrice()) {
currentMin = newBookCatalog[j].getPrice();
currentMinIndex = j;
}
}
if (currentMinIndex != i) {
BookCatalog2 temp = newBookCatalog[currentMinIndex];
newBookCatalog[currentMinIndex] = newBookCatalog[i];
newBookCatalog[i] = temp;
}
}
//Display the newly sorted array of objects
for (int i = 0; i < newBookCatalog.length - 1; i++) {
System.out.printf("%-8d%-20s%-19s%-18.2f\n", newBookCatalog[i].getItemNumber(), newBookCatalog[i].getProduct(), newBookCatalog[i].getCategory(), newBookCatalog[i].getPrice());
}
break;

这就是我到目前为止所拥有的。我试图通过增加价格对数组进行排序,但是当程序运行时,它显示除最后一项之外的所有项目。我重新设计了几次,但我无法排序来显示最后一项。我是初学者,从我的教科书中复制了这种基本结构并从我的教练那里得到了帮助。我教科书上的例子只是一个 int 数组。所以这有点复杂。

有什么提示或技巧吗?

最佳答案

I reworked it a few times but i cannot get the sort to show the last item.

您通过执行 newBookCatalog.length-1 排除了最后一项相反,它应该是
i < newBookCatalog.length .

更改此:

for (int i = 0; i < newBookCatalog.length-1; i++)

对此:

for (int i = 0; i < newBookCatalog.length; i++)

关于java - 通过增加价格对一组对象进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43575943/

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