如何在java中对整数的ArrayList进行排序?

时间:2013-11-10 18:16:05

标签: java sorting arraylist

我在java中有一个整数的数组列表,我正在尝试排序,我使用的是collection.sort方法,由于某种原因,我在每个数字的排序时得到2。这是代码

import java.util.ArrayList;
import java.util.Scanner;
import java.io.*;
import java.util.Collections;

public class ArraySort{
  static ArrayList<Integer> numberList = new ArrayList<Integer>();
  static ArrayList<Integer> numberList2 = new ArrayList<Integer>();
  static ArrayList<Integer> numberList3 = new ArrayList<Integer>();

  public static void main(String[] args){
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the name of the file that contains the first list");
    String file1 = input.nextLine();
    System.out.println("Enter the name of the file that contains the second list");
    String file2 = input.nextLine();
    ArraySort x = new ArraySort(file1, file2);
    ArrayList<Integer> mergedlist = x.merge();
    System.out.print("The first list is ");
    print(x.numberList);
    System.out.println();
    System.out.print("The second list is ");
    print(x.numberList2);
    System.out.println();

    System.out.print("The new combine list is ");
    print(mergedlist);
    System.out.println();

    System.out.print("The sorted combine list is ");
    print(bubbleSort(mergedlist));
    System.out.println();

    System.out.println("Enter the key for the split: ");
    int key = input.nextInt();
    System.out.println("The list has been split into 2 lists");
    System.out.print("List 1 is "); 
    print(numberList);
    System.out.println();

    System.out.print("List 2 is "); 
    print(numberList2);
  }
  public ArraySort(){
      Scanner s = new Scanner(System.in);
      try {
            s = new Scanner (new File ("list1.txt")).useDelimiter("\\s+");
        } catch(FileNotFoundException fnfe) { 
            System.out.println("file not found");
        } 

      while (s.hasNext()) {
        if (s.hasNextInt()) { // check if next token is an int
          numberList.add(s.nextInt());
        } else {
          s.next(); // else read the next token
          }
      }
      try {
            s = new Scanner (new File ("list2.txt")).useDelimiter("\\s+");
        } catch(FileNotFoundException fnfe) { 
            System.out.println("file not found");
        } 

      while (s.hasNext()) {
        if (s.hasNextInt()) { // check if next token is an int
          numberList2.add(s.nextInt());
        } else {
          s.next(); // else read the next token
          }
      }
  }
  public ArraySort(String x, String y){
      Scanner s = new Scanner(System.in);
      try {
            s = new Scanner (new File (x)).useDelimiter("\\s+");
        } catch(FileNotFoundException fnfe) { 
            System.out.println("file not found");
        } 

      while (s.hasNext()) {
        if (s.hasNextInt()) { // check if next token is an int
          numberList.add(s.nextInt());
        } else {
          s.next(); // else read the next token
          }
      }
      try {
            s = new Scanner (new File (y)).useDelimiter("\\s+");
        } catch(FileNotFoundException fnfe) { 
            System.out.println("file not found");
        } 

      while (s.hasNext()) {
        if (s.hasNextInt()) { // check if next token is an int
          numberList2.add(s.nextInt());
        } else {
          s.next(); // else read the next token
          }
      }
  }
  public static ArrayList<Integer> bubbleSort(ArrayList<Integer> numbers) { 
     Collections.sort(numbers);
     return numbers;
    }
  public static void print(ArrayList<Integer> numbers){

    System.out.println(); 
      for (int i = 0; i < numbers.size(); i++){ 
        System.out.print(numbers.get(i) + "  "); 
      }
  }

  public static ArrayList<Integer> merge(){

      for( int i = 0; i < numberList.size(); i++) {
        numberList3.add(numberList.get(i));
      }
      for( int j = 0; j < numberList2.size(); j++) {
        numberList3.add(numberList2.get(j));
      }   
      return numberList3;
    }

  public static void splitList(int x){
    numberList.clear();
    numberList2.clear();
      for(int i=0;i<numberList3.size();i++){
        if(numberList3.get(i)<=x){
          numberList.add(numberList3.get(i));
        }
        else{
          numberList2.add(numberList3.get(i));
        }
      }
    }
}

以下是运行时的输出:

Enter the name of the file that contains the first list
list1.txt
Enter the name of the file that contains the second list
list2.txt

The first list is 
13  25  34  67  56  10  20  27  2  5  1  45  59  

The second list is 
73  29  14  87  72  100  200  127  22  15  19  145  159  78  

The new combine list is 
13  25  34  67  56  10  20  27  2  5  1  45  59  73  29  14  87  72  100  200  127  22  15  19  145  159  78  

The sorted combine list is 
1  2  5  10  13  14  15  19  20  22  25  27  29  34  45  56  59  67  72  73  78  87  100  127  145  159  200  

Enter the key for the split: 
19

The list has been split into 2 lists
List 1 is 
13  25  34  67  56  10  20  27  2  5  1  45  59  

List 2 is 
73  29  14  87  72  100  200  127  22  15  19  145  159  78  

我无法弄清楚为什么会这样做

1 个答案:

答案 0 :(得分:0)

您正在拨打x.merge()两次。每次都会添加numList1numLlist2

System.out.print("The new combine list is ");
print(x.merge());
System.out.println();

System.out.print("The sorted combine list is ");
print(bubbleSort(x.merge()));

使merge()方法返回一个ArrayList。然后你可以这样做,而不是上面的

ArrayList<Integer> newList = new ArrayList<Integer>(x.merge);

System.out.print("The new combine list is ");
print(newList);
System.out.println();

System.out.print("The sorted combine list is ");
print(bubbleSort(newList));