简体   繁体   中英

Java Arraylist Sorting Issue

I am creating an Android app and using Volley to get a webpage by making post request. And using Jsoup for parsing HTML code. I used Jsoup selecter to filter and extract needed data. And then I split data and store in an String array. Finally, I'm looping string arrays and storing all data in Array list.

Now the problem is, the data is form of labels and values and I've inserted data in array list like (at first index "label" and at second index "value") But the array list order is not correct. It outputs:

[Label 1,  Label 2,  Label 3,  Label 4,  Label 5,  Value 5,  Value 4,   Value 3,  Value 2, Value 1]

So the first label is at first index and first value is at last index. Maybe there's something wrong OR missing.

I have created simple example scenario in Java and here is the example code:

public static void main(String[] args) {

    ArrayList<String> arrayList = new ArrayList<>();

    String labels = "Label 1: Label 2: Label 3: Label 4: Label 5";
    String values = "Value 1\n Value 2\n  Value 3\n Value 4\n Value 5\n";

    String[] labelsArray = labels.split(":");
    String[] valuesArray = values.split("\n");

    for(int i = 0; i < labelsArray.length; i++){
        arrayList.add(i, labelsArray[i]);
        arrayList.add(i+1, valuesArray[i]);
    }
    System.out.println(arrayList.toString());      
}

PS: I can apply a small tweak to make it work but I wanted to know; what is the actual problem.

Look where you're adding these items. When i is 0, you add a label at 0, then a value at 1. Then when i is 1, you add a label at 1 (shifting the value to 2), and then another value at 2 (before the previous value). When i is 2, you add a label at 2 (after your two labels and before your two values , which are pushed up to 3), then a value at 3, before all the previous values .

Consider using add(x) instead of add(index, x)

You're shifting values in-place. Essentially, you're trying to use one variable for two different values.

In this critical code block:

for(int i = 0; i < labelsArray.length; i++){
    arrayList.add(i, labelsArray[i]);
    arrayList.add(i+1, valuesArray[i]);
}

When i = 0 , then that makes the added spots in your list 0 and 1. When i = 1 , then your inserted spots become 1 and 2. This effectively maintains the order of "element from list 1", then "element from list 2". \\

To solve this, the simplest approach would be to use add() ; using add() without an index location would accomplish exactly what you're looking for.

Another approach would be to use two variables, and your other variable needs to advance multiple places. You can declare more of them in your for statement.

for(int i = 0, arraySpot = 0; i < labelsArray.length; i++, arraySpot += 2){
    arrayList.add(arraySpot, labelsArray[i]);
    arrayList.add(arraySpot+1, valuesArray[i]);
}

You insert each Label at position i, where i goes from 0 to 4 and the Value in the position i+1, after the Label. This means that in the first iteration you insert the Label 1 at index 0 and the Value 1 at index 1. In the next iteration, i = 1, so you insert the Label 2 at index 1, which automatically pushes the Value 1 to index 2. Then you insert Value 2 at index 2, pushing Value 1 further down to index 3. The list at this point is Label 1 Label 2 Value 2 Value 1 . This continues for all further label / value pairs.

The problem is that you are not using your i variable as you should. This:

for (int i = 0; i < labelsArray.length; i++){
    arrayList.add(i, labelsArray[i]);
    arrayList.add(i+1, valuesArray[i]);
}

Should be:

for (int i = 0; i < labelsArray.length; i++){
    arrayList.add(i*2, labelsArray[i]);
    arrayList.add(i*2 + 1, valuesArray[i]);
}

But, despite this bug with the index, in your case you actually don't need to specify in which position you want to add each string. Instead, just use the version of List.add that adds elements to the end of the list:

for (int i = 0; i < labelsArray.length; i++){
    arrayList.add(labelsArray[i]);
    arrayList.add(valuesArray[i]);
}

To understand why this is happening you need to take apart that for loop.

Step 0: i = 0

 arrayList.add(i, "A"); // arrayList contains ["A"]
 arrayList.add(i + 1, "B");  //arrayList contains ["A", "B"]

Step 1: i = 1

 arrayList.add(i, "C"); // arrayList contains ["A", "C", "B"]
 arrayList.add(i + 1, "D");  //arrayList contains ["A", "C", "D", "B"]

and so on.

You can actually remove the index from the add like this:

  arrayList.add("A");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM