2017-01-25 18 views
0

無法使用此通用選擇排序。算法看起來很好,只是在討論bing過程中遇到了麻煩。我需要能夠通過整理,雙打或字符串的Arraylist進行排序然後才能打印出來。我認爲問題在於我的泛型方法的標題,但我對泛型是全新的,所以我可能是錯的。任何幫助表示讚賞。選擇對多個圖元排序

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

public class FunwithJava { 
    public static void main(String[] args) 
     { 
     ArrayList<Integer> arrInts = new ArrayList<Integer>(); 
     ArrayList<Double> arrDoubles = new ArrayList<Double>(); 
     ArrayList<String> arrString = new ArrayList<String>(); 

     System.out.println("Enter any number of strings, ints or doubles" + "\n" + "Terminate with an empty entry"); 

     getStrings(arrInts, arrDoubles, arrString); 
     selectionSort(arrString); 
     selectionSort(arrInts); 
     selectionSort(arrDoubles); 

     System.out.println("Total number of items entered: " + arrString.size() + " Type of items: String "); 
     System.out.println("Total number of items entered: " + arrInts.size() + " Type of items: Integer"); 
     System.out.println("Total number of items entered: " + arrDoubles.size() + " Type of items: Doubles"); 
     System.out.println("All Strings entered:" + PrintArrays(arrString)); 
     System.out.println("All Integers entered: " + PrintArrays(arrInts)); 
     System.out.println("All Doubles entered: " + PrintArrays(arrDoubles)); 
    } 

    public static void PrintArrays(ArrayList<Integer> arrInts, ArrayList<Double> arrDoubles, ArrayList<String> arrString) { 
     int count = 0; 
     for (int i = 0; i < arrInts.size(); i++) { 
      System.out.print(arrInts.get(i) + ","); 
      count++; 
      if (count == 10) { 
       System.out.println(); 
       count = 0; 
      } 
     } 
    } 

    public static void getStrings(ArrayList<Integer> arrInts, ArrayList<Double> arrDoubles, ArrayList<String> arrString) { 
     Scanner in = new Scanner(System.in); 

     while (in.hasNextLine()) { 
      String oneline = in.nextLine(); 
      Scanner str = new Scanner(oneline); 

      if (oneline.equals("")) 
       break; 

      while (str.hasNext()) { 
       if (str.hasNextDouble()) { 
        arrDoubles.add(str.nextDouble()); 
       } 
       if (str.hasNextInt()) { 
        arrInts.add(str.nextInt()); 
       } else { 
        arrString.add(str.nextLine()); 
       } 
      } 
     } 
    } 

    public static <Anytype extends Comparable<ArrayList<Anytype>[]>> void sort(ArrayList<Anytype> anytypeArrayList[]) { 
     for (int i = 0; i < anytypeArrayList.length - 1; i++) { 
      int ilow = i; 

      for (int j = i + 1; j < anytypeArrayList.length; j++) { 
       if (anytypeArrayList[ilow].compareTo((anytypeArrayList[j])) > 0) { 
        ilow = j; 
       } 
      } 
      Anytype itemp = anytypeArrayList[ilow]; 
      anytypeArrayList[ilow] = anytypeArrayList[i]; 
      anytypeArrayList[i] = itemp; 

     } 
    } 
} 
+0

你怎麼能有一個'PrintArray(列表,列表,列表)',但只有一個參數調用它?這不是泛型(你似乎稱之爲)的工作原理。由於您只需要'toString()'方法(在print語句中調用),您可以簡單地使用'PrintArray(ArrayList )' – AxelH

+0

考慮將問題標題更改爲像_Java泛型方法聲明與List of Comparable_ –

回答

0

有幾個問題:

  1. 你通過了ArrayLists陣列作爲PARAM
  2. 你的方法泛型聲明是錯誤的
  3. printArrays方法是多餘的,可以被替換簡單.toString on ArrayList System.out.println("All Strings entered:" + arrString);
  4. ArrayList.size()
  5. ArrayList.set(i, obj).get(i)代替[i]

這裏的排序方法固定:

private static <T extends Comparable<T>> void selectionSort(final ArrayList<T> anytypeArrayList) { 

      for (int i = 0; i < anytypeArrayList.size() - 1; i++) { 
       int ilow = i; 

       for (int j = i + 1; j < anytypeArrayList.size(); j++) { 
        if (anytypeArrayList.get(ilow).compareTo(anytypeArrayList.get(j)) > 0) { 
         ilow = j; 
        } 
       } 
       final T itemp = anytypeArrayList.get(ilow); 
       anytypeArrayList.set(ilow, anytypeArrayList.get(i)); 
       anytypeArrayList.set(i, itemp); 

      } 
     }