0
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.

2
  • May i ask why you dont want to use set method? Java is pretty much about setters and getters.
    – ouzture
    Jul 13, 2015 at 7:24
  • Thanks for reply ouzture, Actually this question was asked in interview and interviewer asks that no need to use sort, set or any other inbuilt functions.:)
    – TNJ
    Jul 13, 2015 at 18:22

10 Answers 10

8
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
  • Fyi: you could use Reflection to get the content array of the arraylist and then assign the value manually. However the set method if definitely to be preferred. Jul 13, 2015 at 8:32
  • Thanks Eran, I wanted to hear an exact answer.
    – TNJ
    Jul 13, 2015 at 18:27
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);
   }
2

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

Problem in swapping:

            int tmp = arraylist.get(i);
              arraylist.set(i,arraylist.get(j)) ;
              arraylist.set(j,tmp);
0
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());



    }


}
1
  • Welcome to Stack Overflow! While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. Apr 8, 2016 at 3:54
0
`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);
    }
}
0

Try using this:

public static void SortingArrayListAsc(List<Integer> list) {

    for (int i = 0; i < list.size(); i++) {

        for (int j = 0; j < list.size(); j++) {

            if (list.get(i) < list.get(j)) {

                Integer temp = list.get(i);

                list.set(i, list.get(j));

                list.set(j, temp);

            }

        }

    }       

}
0

public int [] sortArrayByType(int[] array,String sortType){

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

        for(int j=i+1;j<array.length;j++){


                if(sortType.equals("asc")) {
                    if (array[i] > array[j]) {
                        swapValues(array, i, j);
                    }
                }else{
                    if (array[i] < array[j]) {
                        swapValues(array, i, j);
                    }
                }
        }
    }
    return array;
}

private void swapValues(int[] array, int i, int j) {
    int temp = array[i];
    array[i] = array[j];
    array[j] = temp;
}
1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jul 13, 2022 at 20:23
-1
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);


}

}
1
  • Trying using proper syntax indentation and also document your code. This code is not readable (by humans) :-)
    – azbarcea
    May 16, 2020 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.