2015-04-12 106 views
1

我試圖創建一個程序,它接受用戶輸入並按字母順序排序,因爲它使用compareToString(不是array.sort)並在最後打印最終的已排序數組。我已經掌握了這個問題的大部分內容,但是一旦我進入排序功能就會丟失。有沒有人對我如何能夠完成SortInsert方法有任何想法?將字符串插入到Java中的數組中插入字段

import java.util.*; 
public class SortAsInserted { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     int array_size = GetArraySize(); 
     String[] myArray = new String[array_size]; 
     for (int i = 0; i < array_size; i++){ 
      String nextString = GetNextString(); 
      String[] sortedArray = SortInsert(nextString, myArray); 
     } 
     PrintArray(sortedArray); 
    } 



     input.close(); 
     } 

    } 




    public static String[] SortInsert(String nextString, String[] myArray){ 
     for(int i = 0; i < myArray.length;) 
      if (nextString.compareToIgnoreCase(myArray[i]) > 0) { 
       i++; 
       //if current text is less(alphabetically) than position in Array 
      }else if (nextString.compareToIgnoreCase(myArray[i]) < 0){ 

      } 

     } 

    public static int GetArraySize(){ 
     Scanner input = new Scanner(System.in); 
     System.out.print("How many items are you entering?: "); 
     int items_in_array = input.nextInt(); 
     return items_in_array; 


    } 

    public static void PrintArray(String[] x) { 
     for (int i = 0; i < x.length; i++){ 
      System.out.print(x[i]); 
     } 

    } 

    public static String GetNextString(){ 
     Scanner input = new Scanner(System.in); 
     System.out.println("Enter the next string: "); 
     String next_string = input.nextLine(); 
     return next_string; 

     } 


} 
+0

爲什麼要按照您的要求進行排序?最後一次排序意味着您不必每次都移動所有後來的字符串,甚至可以在原地進行排序。如果您確實需要隨時對其進行排序,則應使用鏈接列表或其他一些不需要移動插入內容的其他數據結構。 –

+0

這是我正在上課的任務。我不希望這樣做,因爲它很麻煩,但它是要求的一部分。 – Efie

回答

1

此代碼有許多問題。首先我會回答你的直接問題,然後列舉一些其他問題。

SortInsert方法需要一個String[],它將用null值進行初始化,因此您需要考慮這一點。 for循環看起來像這樣。 (我使用的評論,而不是寫實際的代碼,因爲我不是在做項目)

for (int i=0; i<myArray.length; ++i) { 
    if (myArray[i] == null) { 
     // we found a blank spot. use it to hold nextString. 
     break; 
    } else if (nexString.compareToIgnoreCase(myArray[i]) < 0) { 
     // nextString should be in spot i, so make room for it 
     // by shuffling along whatever is in the array at "i" and later 
     // by one place, then put nextString into position "i" 
     break; 
    } 
    // otherwise we'll just move to the next position to check 
} 

現在的其他問題。

  • 您有一個Scanner對象在main從未使用過。如果你的其他方法是自己做的,那麼最終結束它並沒有意義。
  • myArray將始終是排序後的數組,因此在創建名爲sortedArray的局部變量並從SortInsert返回時沒有意義。請注意,您嘗試打印sortedArray無論如何都會失敗,因爲該局部變量僅在for循環範圍內。
  • 打印時應該將myArray傳遞給PrintArray
0

如果您打算按照您的要求進行排序,那麼您應該使用TreeMap數據結構,而不是數組。但是,如果你想在數組中進行排序,則需要在SortInsert的else if子句中添加一些行(應該是sortInsert,BTW)。 (另一個問題:爲什麼它是其他而不是其他?)

行應該創建一個比現有數組大1的新數組,將舊數組的第一個i-1元素複製到新數組,將新元素置於位置i,然後將舊數組的其餘元素複製到新數組中更大的位置。

+0

它看起來像數組意味着一個足夠大的固定大小來容納每個元素,所以創建一個新數組只是額外的開銷。 –

+0

@MatthewRead我明白了......你是對的。但是,我們必須跟蹤到目前爲止使用的最大索引,並將其傳遞給sortInsert以及傳遞新項目和數組...... gah。 –

0

一旦找到想要插入的位置,您必須將所有以下元素向下移一位。類似以下內容:

String temp = array[position]; 
for (int j = position+1; j < array_size-1; j++) { 
    String temp2 = array[j]; 
    array[j] = temp; 
    temp = temp2; 
} 
array[array_size-1] = temp;