gpt4 book ai didi

java - 移动链表中的项目

转载 作者:行者123 更新时间:2023-12-01 18:33:05 24 4
gpt4 key购买 nike

如何将项目在链接列表中的位置上下移动?

文本文件包含保存在链表中的数据如下。我想根据第一项对项目进行排序,即 TY12354、ytpy217、TY12354dsaf...

TY12354, Toyota, TY1257,2100000 SK2344, Skoda, SO2345, 180000 ytpy217,safsadf,asfasf,1241234 TY12354d,sfasdf,asfasf,235123412 TY12354dsaf,asdffasd,asfasfafsd,12344 ABC123,asdffasd,asfasfafsd,12344

我使用以下代码进行排序,但它不起作用:

    for (int x = 0;x < (lstCar.size()-1); x++) {            
c=(Car) lstCar.get(x); //lstCar is the linked list
d = (Car) lstCar.get(x+1);
int compare =d.getRegNumber().compareTo(c.getRegNumber()) ;
if(compare < 0){

temp = d;
lstCar(x)=c; //tried this method but it doesnt work
//lstCar.sort();
c = temp;
}
}

最佳答案

这不是在 LinkedList 中设置元素的正确方法:

  lstCar(x)=c;  //tried this method but it doesnt work

改为使用:

  lstCar.set(x,c);

如果您想通过保持这种方法对列表进行排序(即按照所示的方式进行操作),那么方法 .set(index, element) 将适合您。

关于java - 移动链表中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23268606/

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