2015-04-12 78 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;