0

I am trying to save some values in a arraylist but somehow they all get overwritten ending up with only 1 value in the arraylist (200).

final String[] titles = new String[urls.length];

    for (int i = 0; i < titles.length; i++){
        ArrayList<Integer> valuesList = new ArrayList<Integer>();
            valuesList.add(page.getTopicCount()); // returns 4 values (50,100,150,200)
                System.out.println("Element: " + valuesList.toString()); 
                    // returns only value 200
    }   

The code page.getTopicCount() returns the 4 values in 1 line (50 100 150 200) only the last one (200) gets added to the arraylist but i am trying to find a way to save them all 4 seperately.

What options do i have? (SharedPreferences, saving to file, do i have to build another loop)?

I already did some research and ended up on this page but i don`t know if this will work.

Ps: the 4 values are constantly changing, thus it is no option to add them as:

valuesList.add("50");
valuesList.add("100");
etc..

Edit: getTopicCount is part of a Saxparser class, see below code snippet:

public void endElement(final String uri, final String localName, final String qName)
        throws SAXException {
//

} else if (localName.equals("topiccount")) {
        in_topiccount = true;
        sb = new StringBuilder();
    }
//

  else if (localName.equals("topiccount")) {
        in_topiccount = false;
        forumPage.setTopicCount(Integer.parseInt(sb.toString())); //returns 50 100 150 200
        sb = null;
    }
// 
}
Community
  • 1
  • 1
Bjorn
  • 135
  • 5
  • 12

1 Answers1

2

Inside the for loop, you are initializing the valuesList ArrayList. So each iteration, you create a new ArrayList and add one element to it and then discard it. As a result, at the end you only have a reference to the last ArrayList you created, which can only have 200 in it.

Presumably what you want to do is something like:

 ArrayList<Integer> valuesList = new ArrayList<Integer>();
 for (int i = 0; i < titles.length; i++){
            valuesList.add(page.getTopicCount()); // returns 4 values (50,100,150,200)
  }    
  System.out.println("Element: " + valuesList.toString()); 

Edit: Essentially, create the ArrayList once, and then insert elements into it in the for loop.

A J
  • 1,080
  • 7
  • 11
  • Hi La Dee Dah, already tried that same issue, 200 gets only added, rest overwritten. – Bjorn Jan 03 '15 at 00:33
  • Are you sure that title.length and page.getTopicCount() are returning what you want? http://ideone.com/sNIeSH shows how this code would work if those two aren't some crazy values. – A J Jan 03 '15 at 00:38
  • This to me means the problem is twofold; 1: your for loop is incorrect. 2: If, as you say, fixing the for loop still gives you incorrect output then either titles or page.getTopicCount() are not behaving as you want. – A J Jan 03 '15 at 00:40
  • Hi titles.length = 4, so it loops 4 times, the values i want are also 4. getTopicCount returns these 4 values but 1 one single stroke. It`s kind of a 'stream' which ends when all 4 pages are parsed. – Bjorn Jan 03 '15 at 00:43
  • Sorry, you say "page.getTopicCount() returns the 4 values in 1 line". What do you mean by 'in 1 line'? If I do System.out.println(page.getTopicCount())) just once in the beginning, what is the output? '50', right? – A J Jan 03 '15 at 00:56
  • No problem, no, it is 200. I see it like that 50 gets added, then 50 gets replaced by 100, then 100 replaced by 150 ending up with the last value 200. As it said it`s kind of stream. – Bjorn Jan 03 '15 at 01:01
  • Ah, so the issue is not in the loop at all- the problem is entirely in getTopicCount. From what you say, it always returns the last entry. Unfortunately I can't help without getTopicCount's code. – A J Jan 03 '15 at 01:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/68112/discussion-between-la-dee-dah-and-bjorn). – A J Jan 03 '15 at 01:04
  • i need your help, are you there? – Bjorn Jan 03 '15 at 10:16