【问题标题】:How do I sort an array of strings alphabetically in java?如何在java中按字母顺序对字符串数组进行排序?
【发布时间】:2015-12-14 04:12:08
【问题描述】:

我是编程/编码的新手,并且已经在学校的一个项目上停留了几天。目标是获取一个充满单词的数组(每个位置是一个不同的单词)并按字母顺序对其进行排序。我已经尝试过对堆栈溢出进行一些研究,但是在我找到的一些示例之后我遇到了一些麻烦。类和驱动程序(如果你愿意的话,我使用的是两部分设置)都编译得很好,没有问题。当我尝试从我的驱动程序中使用alphaSort 时,就会出现问题。我收到下面标记的行的空指针异常。过去我在处理这些异常时遇到过一些麻烦,所以我确信这是我忽略的一些小问题。然而,如前所述,我还不够流利地使用 java 语法来捕捉这样的小错误。

我想我应该只包含整个方法,以防我的错误出现在排序部分之前的开头。到目前为止我所拥有的(我在堆栈溢出中发现了这个):

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 循环从未提前中断,我的方法运行完成。

【问题讨论】:

  • 有趣的学习方式..sorting-algorithms.com
  • 在调试这类问题时非常有用的一点是当错误发生时您在控制台中获得的堆栈跟踪。它通常会说NullPointerException,并给你一个发生错误的行号。如果你查看你的代码和它提到的行号,通常只有一两个变量在那里起作用,然后你可以推断出哪个是空的,以及为什么会发生这种情况。
  • 空白字符串与null 不同。空引用将是一个根本不包含字符串值的变量。例如String a = null。要测试isNull,只需说if (string == null)
  • 谢谢。不知道这个。

标签: java sorting nullpointerexception alphabetical


【解决方案1】:

你需要更新第一个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

【讨论】:

  • 这行得通。我仍然有错误,但我只测试了你的输入(测试、蜜蜂等),它工作得很好。这让我假设它在我的数组列表中是一个空值。我想明天我会努力解决这个问题。您有什么简单的方法可以从数组列表中删除空值吗?
  • @corvonik 在运行 alphaSort 之前执行此操作 wordList.removeAll(Collections.singleton(null));
  • 看起来很完美。我必须导入什么才能使用 Collections?编译并收到错误:找不到符号
  • import java.util.Collections;
【解决方案2】:

试试这个...

 // 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);
  }
 }

【讨论】:

    【解决方案3】:

    将列表复制到数组时出错。它在导致您的 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 中插入空值。您尚未发布完整代码,因此我无法验证此方法之外的 wordList 发生了什么。
    • 我复制了您的代码并运行了它,但我必须创建自己的 wordList。当我这样做时,它起作用了。如果您认为对您有帮助,我可以发布我的完整代码。
    • 那是我的单词表。现在正在努力删除空值。感谢所有帮助伙伴。
    • 没问题。如果它帮助您解决问题,请不要忘记接受我的回答。谢谢!
    【解决方案4】:

    问题是您将wordList.size()-1 的项目数添加到数组中,并且数组大小为wordList.size(),这意味着数组中的最后一个值是null

    【讨论】:

    • 我很早就认为这是问题所在。再次尝试只是为了仔细检查。没有这样的运气:/
    • 你确定wordList中没有空值吗?
    • @corvonik 如果这不是唯一的问题,那么wordList 列表可能包含null 值。
    【解决方案5】:

    对于这个while循环:

    while (count<wordList.size()-1)
    {
        alphaList[count]=wordList.get(count);
        count++;
    }
    

    您不需要循环到wordList.size()-1,因为您已经使用&lt; 而不是&lt;=。您将在倒数第二个索引处停止循环,因此不会将值分配给数组中的最后一个位置。而是使用while (count &lt; wordList.size())while (count &lt;= wordList.size()-1)

    【讨论】:

    • 我很早就认为这是问题所在。再次尝试只是为了仔细检查。没有这样的运气:/
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 2013-12-21
    • 2013-09-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多