[英]How do I sort an array of strings alphabetically in java?
我是編程/編碼的新手,現在已經在學校停留了幾天。 目標是采用一個充滿單詞的數組(每個位置是一個不同的單詞)並按字母順序排序。 我已經嘗試過對堆棧溢出進行一些研究,但是在我發現的一些例子之后我遇到了一些麻煩。 類和驅動程序(如果你願意,我使用兩部分設置)都可以編譯,沒有問題。 當我嘗試從我的驅動程序使用alphaSort
時,會出現此問題。 我收到下面標記的行的空指針異常。 我過去曾經遇到過這些例外的麻煩,所以我確信這是我忽略的一些小事。 然而,如上所述,我還沒有足夠流暢的java語法來捕獲像這樣的小錯誤。
我想我應該只包括整個方法,以防我的錯誤在開始之前,在排序部分之前。 我到目前為止(我在Stack溢出時發現了這個):
public void alphaSort()
{
String alphaList[] = new String[wordList.size()];
int count=0;
//puts wordList into alphaList for easier sorting
while(count<wordList.size()-1)
{
alphaList[count]=wordList.get(count);
count++;
}
int shortestStringIndex;
//sort begins here
for(int j=0; j<alphaList.length -1; j++)
{
shortestStringIndex = j;
for(int i=j+1; i<alphaList.length; i++)
{
if(alphaList[i].trim().compareTo(alphaList[shortestStringIndex].trim())<0) //null pointer exception points here
{
shortestStringIndex = i;
}
}
if(shortestStringIndex !=j)
{
String temp = alphaList[j];
alphaList[j] = alphaList[shortestStringIndex];
alphaList[shortestStringIndex]=temp;
}
}
//prints out results
count=0;
while(count<alphaList.length)
{
System.out.println(alphaList[count]);
alphaOut.print(alphaList[count]);
count++;
}
}
任何幫助將不勝感激。 請盡可能詳盡地給出答案(正如我所說,我是一個java新手)。 謝謝 :)
編輯:測試空值(我假設我的數組列表中的點是空白的)我做了以下方法:
public void isNull()
{
int count=0;
while(count<wordList.size()-1)
{
if((wordList.get(count)).equals(""))
{
System.out.println("null");
break;
}
else
{
System.out.println("nothing yet");
}
count++;
}
}
while循環從未破壞,我的方法跑完了。
您需要更新第一個while循環以匹配:
while(count < wordList.size()) {
alphaList[count] = wordList.get(count);
count++;
}
您沒有將列表的每個索引復制到數組,這意味着當它檢查最后一個索引時,它找不到值(NullPointerException)。
編輯:
這是我的完整測試類:
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
new Test();
}
private ArrayList<String> wordList = new ArrayList<String>();
public Test() {
wordList.add("Test");
wordList.add("Bee");
wordList.add("Pig");
wordList.add("Dog");
alphaSort();
}
public void alphaSort() {
String[] alphaList = new String[wordList.size()];
int count = 0;
while(count < wordList.size()) {
alphaList[count] = wordList.get(count);
count++;
}
int shortestStringIndex;
for(int j = 0; j < alphaList.length - 1; j++) {
shortestStringIndex = j;
for(int i = j + 1; i < alphaList.length; i++) {
if(alphaList[i].trim().compareTo(alphaList[shortestStringIndex].trim()) < 0) {
shortestStringIndex = i;
}
}
if(shortestStringIndex != j) {
String temp = alphaList[j];
alphaList[j] = alphaList[shortestStringIndex];
alphaList[shortestStringIndex]= temp;
}
}
count = 0;
while(count < alphaList.length) {
System.out.println(alphaList[count++]);
}
}
}
輸出:
Bee
Dog
Pig
Test
試試這個...
// sorting array
if(wordList.size()>0){
String alphaList[] = new String[wordList.size()];
//convert list to String array
alphaList= wordList.toArray(alphaList);
//sorting
Arrays.sort(alphaList);
}
........
// let us print all the elements available in wordList
if(wordList.size()>0){
for (String word: alphaList) {
System.out.println("word= " + word);
}
}
將List復制到陣列時出錯。 它在列表的末尾插入一個null,導致你的NullPointerException。 這是修訂后的版本。 我沒有循環遍歷List並將每個項復制到數組(這是錯誤的),而是使用List上的標准java方法將List轉換為數組。
public static void alphaSort()
{
String alphaList[] = wordList.toArray(new String[]{});
int shortestStringIndex;
//sort begins here
for(int j=0; j<alphaList.length -1; j++)
{
shortestStringIndex = j;
for(int i=j+1; i<alphaList.length; i++)
{
if(alphaList[i].trim().compareTo(alphaList[shortestStringIndex].trim())<0) //null pointer exception points here
{
shortestStringIndex = i;
}
}
if(shortestStringIndex !=j)
{
String temp = alphaList[j];
alphaList[j] = alphaList[shortestStringIndex];
alphaList[shortestStringIndex]=temp;
}
}
//prints out results
int count=0;
while(count<alphaList.length)
{
System.out.println(alphaList[count]);
alphaOut.print(alphaList[count]);
count++;
}
}
問題是你要將wordList.size()-1
個項添加到數組中,數組大小是wordList.size()
,這意味着數組中的最后一個值為null
對於這個while循環:
while (count<wordList.size()-1)
{
alphaList[count]=wordList.get(count);
count++;
}
你不需要循環到wordList.size()-1
因為你已經<
而不是<=
。 您在第二個到最后一個索引處停止循環,因此不會將值分配給數組中的最后一個位置。 而是while (count < wordList.size())
或while (count <= wordList.size()-1)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.