排序前删除所有空数组值 [英] Remove all null array values before sorting

查看:152
本文介绍了排序前删除所有空数组值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了一些错误。
我的数组是这样的:

I have been getting some errors. My array looks like this:

 String words[] = new String[50000];

该用户被要求输入值到阵列

The user is asked to input values into the array

words[i]=c.readLine();

该程序将退出,一旦重复值inputed循环无限。现在程序必须按字母顺序排序的数组,但它正试图理清一些空值,并返回错误。

The program will exit the infinite for loop once duplicated values are inputed. Now the program must sort the array in alphabetical order, yet it is trying to sort some of the null values and returns errors.

Arrays.sort(words, String.CASE_INSENSITIVE_ORDER);         
for (int a = 0; a < words.length; a++) {
    c.println(words[a]);
}

这个程序将工作仅当用户输入准确50000值。我无法猜测有多少价值,用户将输入的,我该如何解决这个问题?

This program will work only if the user inputs exactly 50000 values. I am unable to guess how many values the user will input, how do I solve this?

我想我必须按字母顺序排序前以某种方式删除所有空值。任何想法?

I am thinking I have to somehow remove all null values before alphabetically sorting. Any ideas?

推荐答案

您可以指望使用已输入值和排序只有非空值

You could count the inputed Values and Sort only the non null values using

Arrays.sort(words, 0,inputCount, String.CASE_INSENSITIVE_ORDER);

其中inputCount是封装无尽的数量,同时循环。

where inputCount is the count of the encapsulating endless while loop.

您也可以修改您的for循环

You could also modify your for-loop

for (int a = 0; a < inputCount; a++) {

这篇关于排序前删除所有空数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆