简体   繁体   中英

ArrayList One loop sorted, the other unsorted

For this assignment I need to sort one list and also keep the original list, but when I click the button twice, the original list is also sorted.

        String output = "";
    String sortedOutput = "";

    //ORIGINAL OUTPUT
    for (int j = 0; j < song.size(); j++){
        output += song.get(j) + "\n";
    }

    //SORTED OUTPUT
    for (int k = 0; k < song.size(); k++){
        Collections.sort(song);
        sortedOutput += song.get(k) + "\n";

    }

    titleArtistOutput.setText("Original Order\n" + output + "\nSorted Order\n" + sortedOutput);

Is there anything I can put on the first loop to prevent it from getting sorted?

EDIT:

Thank you Jon Skeet!

I made another arraylist

String output = "";
String sortedOutput = "";

List <String> sortedSongs = new ArrayList <String> (song);

 for (int j = 0; j < song.size(); j++){
    output += song.get(j) + "\n";
}

for (int k = 0; k < song.size(); k++){
    Collections.sort(sortedSongs);
    sortedOutput += sortedSongs.get(k) + "\n";

}

titleArtistOutput.setText("Original Order\n" + output + "\nSorted Order\n" + sortedOutput);

Sorry for the easy question, I am new to programming.

I suspect you just want to take a copy of the list, then sort that:

List<Song> sortedSongs = new ArrayList<Song>(song);
Collections.sort(sortedSongs); // Will not affect song

Note that this should be done before your loop, not inside it.

Because when you are clicking the second time,the list is already sorted from the first time process. Do it like below, here I have took 'songDuplicate' list whose sorting doesn't affects the original list.

String output = "";
String sortedOutput = "";

//ORIGINAL OUTPUT
for (int j = 0; j < song.size(); j++){
    output += song.get(j) + "\n";
}
ArrayList<String> songDuplicate = new ArrayList<String>();
//SORTED OUTPUT
for (int k = 0; k < songDuplicate.size(); k++){
    Collections.sort(songDuplicate);
    sortedOutput += songDuplicate.get(k) + "\n";

}

titleArtistOutput.setText("Original Order\n" + output + "\nSorted Order\n" + sortedOutput);

You need to make copy of your original list. Maybe you can create copy when you create output from the original list.

    String output = "";
    String sortedOutput = "";
    List sorted = new ArrayList();

    //ORIGINAL OUTPUT
    for (int j = 0; j < song.size(); j++){
        output += song.get(j) + "\n";
        sorted.add(song.get(j));
    }

    Collections.sort(sorted);

    //SORTED OUTPUT
    for (int k = 0; k < sorted.size(); k++){
        sortedOutput += sorted.get(k) + "\n";

    }

    titleArtistOutput.setText("Original Order\n" + output + "\nSorted Order\n" + sortedOutput);
  1. Use a debugger to understand each step of the computation.
  2. You only have one list. Consequently, after you sorted it, it is sorted. Real.
  3. RTFM: http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort%28java.util.List%29 Sort() does not return a sorted copy of the original list. It modifies it until it is sorted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM