如何在不使用 Java 集合的情况下对数组列表进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31377448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to do sorting in array list without using collections in Java
提问by TNJ
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);
}
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.
交换时出错,LHS 应该是可变的。我明白了。Set 方法在这里有效,但我不想使用。有没有办法在不使用 set 方法的情况下做到这一点?非常感谢帮助。
采纳答案by Eran
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.
您不能为方法调用赋值。正如编译器告诉您的,赋值的左侧必须是一个变量。
Use set
method :
使用set
方法:
arraylist.set(i,arraylist.get(j));
arraylist.set(j,tmp);
Is there a way to do it without using set method?
有没有办法在不使用 set 方法的情况下做到这一点?
No. Unless you wish to convert your ArrayList to an array, sort the array, and update the ArrayList with the sorted array.
否。除非您希望将 ArrayList 转换为数组,否则请对数组进行排序,并使用排序后的数组更新 ArrayList。
回答by Anil Reddy Yarragonda
Problem in swapping:
交换问题:
int tmp = arraylist.get(i);
arraylist.set(i,arraylist.get(j)) ;
arraylist.set(j,tmp);
回答by Kamal Pundir
You can't directly assign value to ArrayList Index. You have to use the set() method. change your code as follow :
您不能直接为 ArrayList 索引赋值。您必须使用 set() 方法。更改您的代码如下:
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.set(i,arraylist.get(j)) ;
arraylist.set(j,tmp);
}
}
}
for (int i: arraylist) {
System.out.println(i);
}
回答by Tagir Valeev
The shortest way to swap to list values is the following:
交换到列表值的最短方法如下:
arraylist.set(i, arraylist.set(j, arraylist.get(i)));
You can utilize the fact that set
method returns the previous value. This algorithm is implemented in Collections.swap
standard method, so you can use it:
您可以利用set
方法返回先前值的事实。该算法以Collections.swap
标准方法实现,因此您可以使用它:
Collections.swap(arraylist, i, j);
回答by Allen
import java.util.ArrayList;
import java.util.Scanner;
class Main{
public static String titleCase(String s){
String is = s;
String ret = "";
ret += is.substring(0, 1).toUpperCase();
ret += is.substring(1).toLowerCase();
return ret;
}
public static ArrayList<String> sort(ArrayList<String> list){
for(int z = 0; z <list.size()-1; z++){
if(list.get(z).compareTo(list.get(z+1)) >0){
list.add(z, list.get(z+1));
list.remove(z+2);
}
}
return list;
}
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
ArrayList<String> names = new ArrayList();
int x = 0;
int count = 0;
String output = "";
while(x != 1){
System.out.println("Enter the next name:");
String temp = scan.next();
temp = titleCase(temp);
if(temp.toLowerCase().equals("stop")){
x = 1;
}
else{
names.add(count, temp);
count++;
}
}
names.equals(sort(names));
System.out.println(names.toString());
}
}
回答by Riyad
Integer[] list={3,5,100,8,17,19};
for(int i=0;i<list.length;i++){
for(int j=i+1;j<list.length;j++){
Integer tempI=list[i];
Integer tempJ=list[j];
if(tempI>tempJ){
list[i]=tempJ;
list[j]= tempI;
}
}
}
for(Integer a:list){
System.out.println(""+a);
}
回答by C k khamari
`import java.util.ArrayList;
import java.util.Collections;
public class SortList {
public static void main(String[] args){
ArrayList<Integer> listToSort = new ArrayList<Integer>();
listToSort.add(10010);
listToSort.add(5);
listToSort.add(4);
listToSort.add(2);
System.out.println("List to SOrt"+listToSort);
for(int i= 0;i<listToSort.size();i++)
{
for (int j=0;j<listToSort.size();j++){
if(listToSort.get(j)>listToSort.get(i)){
int temp=listToSort.get(i);
int temp2=listToSort.get(j);
listToSort.set(j,temp);
listToSort.set(i,temp2);
}
}
}
System.out.println("List SOrted"+listToSort);
}
}
回答by Saurav
public class SortUsingCollection{
public static void main(String[] args){
ArrayList al=new ArrayList();
al.add(32);
al.add(3);
al.add(23);
al.add(80);
al.add(38);
al.add(31);
al.add(90);
al.add(8);
ListIterator i=al.listIterator();
int k=0;
while(i.hasNext()){
int loop=0;
Integer n=(Integer)i.next();
ListIterator j=al.listIterator();
while(loop<k){
if(j.hasNext())
j.next();
loop++;
}
while(j.hasNext()){
Integer m=(Integer)j.next();
if(m<n){
j.set(n);
i.set(m);
n=m;
}
}
k++;
}
System.out.println(al);
}
}