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
String[] states = "FL GA SC NC VA MD ...".split(" ");there is no problem. Check ifsplit("\n");apply to all the data in the file (there aren't any\r\ndelimiters for example).