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

==================================主要方法=============== =======================

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

输入示例

121234256464534

1123123

123141243124124

123

我的排序顺序和原始顺序都按升序修改,在此处输入图像描述即使我创建了一个要返回的新数组列表,同时确保我没有对原始数组进行任何更改。

提前致谢

4

3 回答 3

5
sortingList = a;

两个引用,sortingList 和 a 都指向同一个 ArrayList 对象

检查此问题以查看如何克隆列表:如何克隆 ArrayList 并克隆其内容?

于 2013-11-08T13:54:21.460 回答
1

只需从您的代码中删除这一行,

   ArrayList<String> sortingList = new ArrayList<String>();
    sortingList = a; //REMOVE THIS LINE

当您在之前但在下一行中分配一个新的arraylist 对象时,您将对原始对象的引用存储在sortingList对象中。

如果您想将所有元素复制到排序列表,请查看Evans Post,但在您的示例中,您不需要它,因为您从提供的原始数组分配testtest2变量。

于 2013-11-08T14:04:40.533 回答
1

正如 Axel 指出的那样,使用:

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 回答