Arrays.sort()从我的数组中删除了很多项

时间:2018-03-26 04:29:38

标签: java arrays string sorting

在我的Java程序中,我有以下代码:

Arrays.sort()

奇怪的是,从java.util.Arrays调用FL GA SC NC VA MD NY NJ DE PA CT RI MA VT NH ME AL TN KY WV OH MI MS AR MO KS NE IN IL WI MN LA TX OK IA SD ND NM CO WY ID AZ UT NV MT CA OR WA AL HI 50 AL AL AR AZ CA CO CT DE FL GA HI 50 会导致许多项目从列表中删除。当我运行上面的代码时,这是输出:

Arrays.sort()

我非常非常困惑这里发生了什么。为什么只打印出11件商品? Arrays.sort()删除项目了吗?为什么50会这样做?为什么数组的大小仍为readFile()?物品是否被清空或是什么?

我认为我的public static String[] readFile(String FileName) { char[] cbuf = new char[200]; String[] array; FileReader fr; try { fr = new FileReader(FileName); try { fr.read(cbuf); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } String all = new String(cbuf); array = all.split("\n"); return array; } 方法工作正常,因为未排序的数组打印得很好......

ROW_NUMBER()

我正在阅读的文件:https://github.com/reactjs/redux/tree/master/examples MD5以8b961b5

开头

4 个答案:

答案 0 :(得分:3)

文件末尾的换行符,特别是在文件“HI”中的最后一个条目之后,似乎导致了问题。可以使用以下方法在readFile函数中解决:

array = all.trim().split("\n");

答案 1 :(得分:1)

通过在线Java编译器确认'工件'行为:

import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        // instead of using readFile() the array is defined here.
        // note the \n on the last element
        String[] states = {"FL", "GA", "SC", "NC", "VA", "MD", "NY", "NJ", "DE", "PA", "CT", "RI", "MA", "VT", "NH", "ME", "AL", "TN", "KY", "WV", "OH", "MI", "MS", "AR", "MO", "KS", "NE", "IN", "IL", "WI", "MN", "LA", "TX", "OK", "IA", "SD", "ND", "NM", "CO", "WY", "ID", "AZ", "UT", "NV", "MT", "CA", "OR",
           "WA", "AL", "HI\n"};

        System.out.println(String.join(" ", states));
        System.out.println(states.length);

        Arrays.sort(states);

        System.out.println(String.join(" ", states));
        System.out.println(states.length);
    }
}

输出:

FL GA SC NC VA MD NY NJ DE PA CT RI MA VT NH ME AL TN KY WV OH MI MS AR MO KS NE IN IL WI MN LA TX OK IA SD ND NM CO WY ID AZ UT NV MT CA OR WA AL HI

50
AL AL AR AZ CA CO CT DE FL GA HI
 IA ID IL IN KS KY LA MA MD ME MI MN MO MS MT NC ND NE NH NJ NM NV NY OH OK OR PA RI SC SD TN TX UT VA VT WA WI WV WY
50

显然@Arjun Kay使用的日志截断了带有折线字符的排序元素后打印的元素。

答案 2 :(得分:1)

您的readFile方法很草率。您声明了一个包含200个元素的缓冲区数组char[] cbuf = new char[200];

听起来您的文件格式化为每行一个状态:

FL
GA
SC
NC

您将整个文件读入缓冲区,但是您没有填充缓冲区,因此尾随的50个元素仍然初始化为默认的空字符值\u0000(请参阅此question

cbuf = [F][L][\n][G][A][\n][S][C][\n][N][C][\n] ... [\u0000][\u0000]

然后将cbuff转换为字符串:

all = "FL\nGA\nSC\nNC\n ... \u0000\u0000\u0000"

然后拆分字符串以将其转换为数组:

array = [FL][GA][SC][NC]...[\u0000\u0000\u0000\u0000\u0000]

所以你可以看到你的最终数组中有一堆无用的字符,因为你的缓冲区大于你读取的文件。

我无法在我的机器上复制您丢失的状态,但您可以清理文件阅读器,我认为它对您有用。使用BufferedReader,然后您可以一次读取一行文件,它将为您节省所有手动拆分。我还建议使用List<String>而不是String[]数组,这样您就不必处理数组的大小。

考虑一下:

public static void main(String[] args) throws IOException {
    String[] states = readFile("States.txt");

    System.out.println(String.join(" ", states));
    System.out.println(states.length);

    Arrays.sort(states);

    System.out.println(String.join(" ", states));
    System.out.println(states.length);

    // now do the same thing but using a list
    List<String> statesList = readFileToList("States.txt");

    System.out.println(String.join(" ", statesList));
    System.out.println(statesList.size());

    Collections.sort(statesList);

    System.out.println(String.join(" ", statesList));
    System.out.println(statesList.size());
}

// read the file to an array
public static String[] readFile(String FileName) throws IOException {
    String[] states = new String[50];
    BufferedReader br = new BufferedReader(new FileReader(FileName));

    String state;
    int index = 0; // keep track of the array index
    // when readLine() returns null there are no more lines to read
    while((state = br.readLine()) != null && index < 50) {
        states[index] = state;
        index++;
    }

    return states;
}

// read the file to a list
public static List<String> readFileToList(String FileName) throws IOException {
    List<String> states = new ArrayList<>(); // no array size to worry about
    BufferedReader br = new BufferedReader(new FileReader(FileName));

    String state;
    while((state = br.readLine()) != null) {
        states.add(state); // no indexes to worry about
    }

    return states;
}

答案 3 :(得分:0)

您可以使用BufferedReaderFileReader这样的

中读取一行
 public static String[] readFile(String FileName) {
        ArrayList<String> stringArrayList = new ArrayList<>();
        BufferedReader bufferedReader;

        try {
            bufferedReader = new BufferedReader(new FileReader(FileName));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringArrayList.add(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringArrayList.toArray(new String[0]);
    }