1

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.

1
  • I'm surprised your seeing Values at all, besides your last one. Every time you iterate your are adding over your value. add(0, Label); add(1, Value) then you do add(1, Label); add(2, Value); then add(2, Label); add(3, Value); etc. Commented May 24, 2017 at 20:35

5 Answers 5

2

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)

Sign up to request clarification or add additional context in comments.

1 Comment

thank you, I'll prefer to use it without specifying index!
2

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

Comments

0

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.

Comments

0

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

2 Comments

yup Federico, that was the issue. I spent whole day is parsing html and selecting data from a very lengthy html. So, I was kind of confused and didn't notice this issue. thanks for clarification!
@Atlas_Gondal Glad I could help.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.