如何在不使用Java中的集合的情况下对数组列表进行排序

编程入门 行业动态 更新时间:2024-10-11 05:31:19
本文介绍了如何在不使用Java中的集合的情况下对数组列表进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 ArrayList < Integer > arraylist = new ArrayList < Integer > (); arraylist.add(10010); arraylist.add(5); arraylist.add(4); arraylist.add(2); for (int i = 0; i < arraylist.size(); i++) { for (int j = arraylist.size() - 1; j > i; j--) { if (arraylist.get(i) > arraylist.get(j)) { int tmp = arraylist.get(i); arraylist.get(i) = arraylist.get(i); arraylist.get(j) = tmp; } } } for (int i: arraylist) { System.out.println(i); }

交换时出现错误,LHS应该是可变的.我明白. 设置方法在这里有效,但我不想使用. 有没有一种方法可以不使用set方法呢? 非常感谢您的帮助.

It is giving error while swapping, The LHS should be variable. I understand it. Set method works here but I do not want to use. Is there a way to do it without using set method? Help is really appreciated.

推荐答案

arraylist.get(i)= arraylist.get(i); arraylist.get(j) =tmp;

您不能为方法调用分配值.正如编译器告诉您的那样,分配的左侧必须是变量.

You can't assign a value to a method call. As the compiler told you, the left hand side of an assignment must be a variable.

使用set方法:

arraylist.set(i,arraylist.get(j)); arraylist.set(j,tmp);

有没有不用set方法的方法吗?

Is there a way to do it without using set method?

不.除非您希望将ArrayList转换为数组,否则请对数组进行排序,然后使用排序后的数组更新ArrayList.

No. Unless you wish to convert your ArrayList to an array, sort the array, and update the ArrayList with the sorted array.

更多推荐

如何在不使用Java中的集合的情况下对数组列表进行排序

本文发布于:2023-11-11 08:53:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577947.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   情况下   如何在   列表   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!