如何在 java 中按字母顺序对字符串数组进行排序?
How do I sort an array of strings alphabetically in java?
我是 programming/coding 的新手,这几天一直在学校做一个项目。目标是获取一个充满单词的数组(每个位置都是一个不同的单词)并按字母顺序对其进行排序。我已经尝试过对堆栈溢出进行一些研究,但是在遵循我发现的一些示例时遇到了一些麻烦。 class 和驱动程序(如果您愿意,我使用的是两部分设置)都可以正常编译,没有问题。当我尝试使用我的驱动程序中的 alphaSort
时出现问题。我收到下面标记的行的空指针异常。过去我在处理这些异常时遇到过一些麻烦,所以我确信这是我忽略的一些小问题。但是,如前所述,我对 java 语法还不够流利,无法捕捉到这样的小错误。
我想我应该只包含整个方法,以防我的错误出现在排序部分之前的开头。到目前为止我所拥有的(我在 Stack overflow 上发现了这个):
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 循环从未中断,我的方法 运行 完成。
问题是您要将 wordList.size()-1
个项目添加到数组中,数组大小为 wordList.size()
,这意味着数组中的最后一个值为 null
您需要更新第一个 while 循环以匹配:
while(count < wordList.size()) {
alphaList[count] = wordList.get(count);
count++;
}
您没有将列表的每个索引都复制到数组中,这意味着当它去检查最后一个索引时,它找不到值(NullPointerException)。
编辑:
这是我的完整测试 class 有效:
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
对于这个 while 循环:
while (count<wordList.size()-1)
{
alphaList[count]=wordList.get(count);
count++;
}
您不需要循环到 wordList.size()-1
,因为您已经执行 <
而不是 <=
。您在倒数第二个索引处停止循环,因此不会将值分配给数组中的最后一个位置。而是 while (count < wordList.size())
或 while (count <= wordList.size()-1)
试试这个...
// 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);
}
}
将列表复制到数组时出错。它在导致 NullPointerException 的列表末尾插入一个 null。这是有效的修订版本。我没有遍历 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++;
}
}
我是 programming/coding 的新手,这几天一直在学校做一个项目。目标是获取一个充满单词的数组(每个位置都是一个不同的单词)并按字母顺序对其进行排序。我已经尝试过对堆栈溢出进行一些研究,但是在遵循我发现的一些示例时遇到了一些麻烦。 class 和驱动程序(如果您愿意,我使用的是两部分设置)都可以正常编译,没有问题。当我尝试使用我的驱动程序中的 alphaSort
时出现问题。我收到下面标记的行的空指针异常。过去我在处理这些异常时遇到过一些麻烦,所以我确信这是我忽略的一些小问题。但是,如前所述,我对 java 语法还不够流利,无法捕捉到这样的小错误。
我想我应该只包含整个方法,以防我的错误出现在排序部分之前的开头。到目前为止我所拥有的(我在 Stack overflow 上发现了这个):
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 循环从未中断,我的方法 运行 完成。
问题是您要将 wordList.size()-1
个项目添加到数组中,数组大小为 wordList.size()
,这意味着数组中的最后一个值为 null
您需要更新第一个 while 循环以匹配:
while(count < wordList.size()) {
alphaList[count] = wordList.get(count);
count++;
}
您没有将列表的每个索引都复制到数组中,这意味着当它去检查最后一个索引时,它找不到值(NullPointerException)。
编辑:
这是我的完整测试 class 有效:
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
对于这个 while 循环:
while (count<wordList.size()-1)
{
alphaList[count]=wordList.get(count);
count++;
}
您不需要循环到 wordList.size()-1
,因为您已经执行 <
而不是 <=
。您在倒数第二个索引处停止循环,因此不会将值分配给数组中的最后一个位置。而是 while (count < wordList.size())
或 while (count <= wordList.size()-1)
试试这个...
// 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);
}
}
将列表复制到数组时出错。它在导致 NullPointerException 的列表末尾插入一个 null。这是有效的修订版本。我没有遍历 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++;
}
}