How to do sorting in array list without using collections in Java

陌路散爱 提交于 2020-05-26 09:54:46

问题


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.


回答1:


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 :

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

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

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




回答2:


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:

Collections.swap(arraylist, i, j);



回答3:


  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);
   }



回答4:


You can't directly assign value to ArrayList Index. You have to use the set() method. change your code as follow :

  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);
      }



回答5:


Problem in swapping:

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



回答6:


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());



    }


}



回答7:


`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);
    }
}



回答8:


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);


}

}


来源:https://stackoverflow.com/questions/31377448/how-to-do-sorting-in-array-list-without-using-collections-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!