简体   繁体   English

按字母顺序对字符串进行排序

[英]sorting string to alphabetical order

hi i am trying to get these fruits in alphabetical order can someone help please as when i try to run the program it closes and says NullPointer Exception java Lang but it compiles no problem i think the problemis with the .compareTo line i am not too sure 您好,我想按字母顺序获取这些结果,有人可以帮忙,因为当我尝试运行该程序时,它关闭并说NullPointer Exception java Lang,但是它编译没有问题,我认为问题与.compareTo行有关,我不太确定

import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
 * Write a description of class rigthOrder here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class rigthOrder
{
    public static void main (String args []) throws IOException
    {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
            int i;

            String fruit [] = new String[1000];
             List<String> fruitsList = Arrays.asList (fruit);

            fruit[0] = "orange";
            fruit[1] = "bananna";
            fruit[2] = "apple";
            fruit[3] = "grape";
            fruit[4] = "plum";

         // for(i = 0; i < fruit.length; i++) 
           {
           //   fruit [i] = fruit [i].toUpperCase();
           }
            Bubble_sort(fruit);
          for(i = 0; i < fruit.length; i++)   
           {
            System.out.println(fruit [i]);  
           }
        }
    public static void Bubble_sort(String [] fruit)
       {
            int i, j, size = fruit.length;
            String temp;

            for(i = 0; i < size-1;  i++)
            {
            for(j = i + 1; j < size;  j++) 
            {
                if (fruit[j].compareTo (fruit[i]) < 0)
                {
                    temp = fruit[i];
                    fruit[i] = fruit [j];
                    fruit[j] = temp;
           }
         }
       }
    }
}
String fruit[] = new String[1000];

The default values of String array's elements are null . String数组元素的默认值为null That's why Arrays.asList(fruit) throws NPE. 这就是为什么Arrays.asList(fruit)抛出NPE的原因。

You made an array of 1000 elements, and only filled in 5 of them. 您制作了一个由1000个元素组成的数组,仅填充了其中5个。

        String fruit [] = new String[1000];

Make this array the proper length and it will stop failing. 将此数组设置为适当的长度,它将停止失败。

you are declaring an array of size 1000 您正在声明大小为1000的数组

String fruit [] = new String[1000];

then filling five of those 1000 slots 然后填充这1000个插槽中的五个

 fruit[0] = "orange";
 fruit[1] = "bananna";
 fruit[2] = "apple";
 fruit[3] = "grape";
 fruit[4] = "plum";

what is in the rest ? 其余是什么?

String fruit [] = new String[5];

这将解决您的问题。

In addition to the rest of the answers, you can also declare the array in a static way: 除了其余答案外,您还可以以静态方式声明数组:

String[] fruit = new String[] {"orange", "bananna", "apple", "grape", "plum"}

That way the size is automatically calculated. 这样就可以自动计算大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM