1
import java.util.ArrayList;

public class BubbleSort {

  // the sort method takes in an ArrayList of Strings, sorts 
  // them in ascending number of characters, and returns a new
  // ArrayList of Strings which is already sorted. This method 
  // does NOT modify the original ArrayList passed in.

  public ArrayList<String> sort(ArrayList<String> a){
    ArrayList<String> sortingList = new ArrayList<String>();
    sortingList = a;
    String test = "";
    String test2 = "";
    int length = 0;
    int length2 = 0;
    for(int j =0; j<a.size(); j++){
        for (int i =0; i<sortingList.size()-1; i++){
            test = a.get(i);
            test2 = a.get(i+1);
            length = test.length();
            length2 = test2.length();
            if(length2<length){
                sortingList.set(i,test2);
                sortingList.set(i+1,test);
            }
        }
    }
    return sortingList;
 }
}

=================================MAIN METHOD=====================================

import java.util.ArrayList;
import java.util.Scanner;

public class BubbleSortTest {

  public static void main(String[] args) {
    ArrayList<String> inputs = new ArrayList<String>();

    // get inputs from user
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter number of Strings to enter: ");
    int no = sc.nextInt();
    sc.nextLine(); // clears buffer in Scanner

    for (int i = 0; i < no; i++){
      System.out.print("Enter String number " + i + ": ");
      inputs.add(sc.nextLine());  // add input into ArrayList
    }

    // invoke the sort method to see if it works
    BubbleSort bs = new BubbleSort();
    ArrayList<String> sortedInputs = bs.sort(inputs);

    // print out the Strings in sortedInputs
    System.out.println("Sorted sequence:");
    for (int i = 0; i < sortedInputs.size(); i++){
      System.out.println(sortedInputs.get(i));
    }

    // print out the Strings in the original inputs
    System.out.println("Original sequence:");
    for (int i = 0; i < inputs.size(); i++){
      System.out.println(inputs.get(i));
    }
  }
}

input example

121234256464534

1123123

123141243124124

123

my sorting sequence & original sequence are both amended in ascending sequenceenter image description here even though I created a new arraylist to return while ensuring I did not make any changes to the original one.

Thanks in advance

4

3 に答える 3

5
sortingList = a;

sortingList と a の両方の参照が同じ ArrayList オブジェクトを指しています

この質問をチェックして、リストを複製する方法を確認してください: How to clone ArrayList and also clone its contents?

于 2013-11-08T13:54:21.460 に答える
1

アクセルが指摘したように、次を使用します。

ArrayList<String> sortingList = new ArrayList<String>(a);

この変更により、BubbleSort クラスでは、引き続き "a" 配列から文字列を参照しているため、適切に並べ替えられません。修正するには、次を変更します。

test = a.get(i);
test2 = a.get(i+1);

test = sortingList.get(i);
test2 = sortingList.get(i+1);

完全なコード:

public ArrayList<String> sort(ArrayList<String> a){

    //As Axel pointed, use:
    ArrayList<String> sortingList = new ArrayList<String>(a);

    String test = "";
    String test2 = "";
    int length = 0;
    int length2 = 0;

    for(int j =0; j<sortingList.size(); j++){
        for (int i =0; i<sortingList.size()-1; i++){

            //reference "sortingList" array instead of "a" array
            test = sortingList.get(i);
            test2 = sortingList.get(i+1);

            length = test.length();
            length2 = test2.length();

            if(length2<length){
                sortingList.set(i,test2);
                sortingList.set(i+1,test);
            }
        }
    }
    return sortingList;
}
于 2013-11-08T14:43:28.947 に答える