Arrays.sort () removes many elements 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); 

Oddly enough, calling Arrays.sort() from java.util.Arrays Arrays.sort() many items 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 about what is happening here. Why are only 11 prints printed? Delete Arrays.sort() items? Why Arrays.sort() do this? Why is the array size still 50 ? Are items thrown away or something else?

I assume my readFile() method works fine as an unsorted array prints 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: https://nofile.io/f/8TO3pdnmS3W/States.txt MD5 starts with 8b961b5

+5
source share
4 answers

The newline at the end of the file, especially after the last entry in the " HI " file, seems to cause a problem. It can be solved in the readFile function using:

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

+3
source

Confirmed the behavior of the "artifact" through 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 conclusion:

 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 truncated items printed after the sorted item with an interrupt character.

+1
source

Your readFile method is readFile accurate. You declare a buffer array char[] cbuf = new char[200]; with 200 elements.

It looks like your file is formatted with a state in each line:

 FL GA SC NC 

You read the entire file in your buffer, but you do not fill the buffer, so the final 50 elements are still initialized with the default value of the null character \u0000 (see this question )

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

Then you convert cbuff to string:

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

Then you split the string to convert it to an array:

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

So you can see that there is a bunch of useless characters in your final array, because your buffer is larger than the file you are reading.

I cannot replicate your missing states on my machine, but you can clear your file reader and I think this will work for you. Use BufferedReader , after which you can read the file one at a time, and this will save you from manual splitting. I would also recommend using List<String> instead of the String[] array, so you don't need to handle the size of the array.

Consider this:

 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; } 
+1
source

you can use BufferedReader to read a line from FileReader like this

  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]); } 
0
source

Source: https://habr.com/ru/post/1276078/


All Articles