I am doing sort numbers from a CSV file. I import the numbers and step into an Array List and convert the ArrayList<String>
to ArrayList<Integer>
. It happens that I am having a logic problem when sorting the numbers. The numbers have even been organized, but the largest number is coming out in front of everyone, and only from the next number that starts the ascending order, as below:
Methodtopassthenumbersfromthe.CSVfiletotheArrayList:
publicvoidPassVector(){StringLineFile=newString();FilefileCSV=newFile(LocalFile.getText());try{Numb=(ArrayList<String>)Files.lines(Paths.get(LocalFile.getText())).flatMap(Pattern.compile(",")::splitAsStream)
.collect(Collectors.toList());
System.out.println(Numb);
Scanner reader = new Scanner(fileCSV);
while (reader.hasNext())
{
LineFile = reader.nextLine();
NumbLines+=1;
}
NumbNumbers = NumbLines * 2;
//System.out.println(NumbLines);
//ConvertArrayList(Numb);
}
catch (IOException e)
{
System.out.println("Não foi possível carregar o vetor. \n\n Código do erro: " + e);
}
}
Method to convert ArrayList <String>
to ArrayList<Integer>
public static ArrayList<Integer> getIntegerArray(ArrayList<String> ArrayConvert) {
ArrayList<Integer> result = new ArrayList<Integer>();
for(String stringValue : ArrayConvert) {
try {
//Convert String to Integer, and store it into integer array list.
result.add(Integer.parseInt(stringValue));
} catch(NumberFormatException nfe) {
//System.out.println("Could not parse " + nfe);
//Log.w("NumberFormat", "Parsing failed! " + stringValue + " can not be an integer");
}
}
return result;
}
Ordering Method BubbleSort (With Logical Error):
public static void Bubble(ArrayList<Integer> BubbleVector)
{
for (int i=0; i<BubbleVector.size();i=i+2)
{
for (int j=2; j<(BubbleVector.size()-1);j=j+2)
{
if (BubbleVector.get(i) < BubbleVector.get(j))
{
Auxiliar1 = BubbleVector.get(i);
Auxiliar2 = BubbleVector.get(i+1);
BubbleVector.set(i, BubbleVector.get(j));
BubbleVector.set(i+1, BubbleVector.get(j+1));
BubbleVector.set(j, Auxiliar1);
BubbleVector.set(j+1, Auxiliar2);
}
}
}
System.out.println(BubbleVector);
}
CSV file with numbers to be sorted