Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arrays.sort() removes many items from my array

In my Java program, I have the following code:

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

Strangely enough, calling Arrays.sort() from java.util.Arrays causes many items to be removed from the list. When I run the code above, this is the output:

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

I am very, very confused as to what's going on here. Why are only 11 items printed out? Is Arrays.sort() removing items? Why would Arrays.sort() do this? Why is the size of the array still 50? Are the items being blanked out or something?

I assume that my readFile() method works fine as the unsorted array prints out fine...

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

The file I am reading from: https://nofile.io/f/8TO3pdnmS3W/States.txt MD5 starts with 8b961b5

like image 552
Aaron Franke Avatar asked Mar 26 '18 04:03

Aaron Franke


2 Answers

The newline character at the end of the file, specifically after the last entry in the file "HI", seems to be causing the problem. It can be solved in the readFile function by using:

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

like image 79
Arjun Kay Avatar answered Oct 17 '22 08:10

Arjun Kay


Confirmed the 'artifact' behavior via the Online Java Compiler :

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

And the Output:

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

Apparently the log used by @Arjun Kay had truncated the elements printed after the sorted element with the break-line character.

like image 30
Rann Lifshitz Avatar answered Oct 17 '22 09:10

Rann Lifshitz