0

I have an instance of ArrayList named array. When I parse some JSON data it will store it all in array. When I do a System.out.println(array); it will list a long list of items, around 30, but when I write System.out.println(array.size); it will give the value one.

How come it only gives me the value 1 when the list contains at least 30 values?

My code for this:

public void setLocationName (String name) {
 array = new ArrayList<String>();
 array.add(name);
 System.out.println(array); //This return a long list
 System.out.println(array.size()); //But this only return the value 1
}
public String[] getLocationName() {
 String tArray[] = null;
 for (int i = 0; i < array.size(); i++){
 System.out.println(i);
 tArray = array.toArray(new String[i]); 
 } 
 return tArray;
}

}

The long list :

[Brunnsparken, Göteborg]
[Brunnsgatan, Göteborg]
[Brunnslyckan, Lerum]
[Brunnsbotorget, Göteborg]
[Brunnsnäs, Ulricehamn]
[Brunnshult, Mellerud]
[Brunnsdal, Skövde]
[Brunns skola, Ulricehamn]
[Brunnsgården, Kungälv]
[Brunns kyrka, Ulricehamn]
[Boråsparken, Borås]
[Stadsparken, Ulricehamn]
[Lysekilsparken, Lysekil]
[Mössebergsparken, Falköping]
[Dalaborgsparken, Vänersborg]
[Rösparken, Åmål]
[Lillhagsparken Norra, Göteborg]
[Lillhagsparken Södra, Göteborg]
[Sylte Ryrbäcksparken, Trollhättan]
[Skogstomtsparken, Borås]
[Svinesundsparken, Norge]
[Håjumsparken, Trollhättan]
[Eriksdalsparken, Bollebygd]
[Fridhemsparken, Lidköping]

My result will be that only one item from the list will be returned in the tArray but I wanna return the whole list.

How to solve this?

tehlexx
2,85718 silver badges29 bronze badges
asked Mar 27, 2013 at 15:10
7
  • 8
    You're creating a new array and adding 1 element to it - therefore, it's always, at the point you're printing out the size, going to only have 1 element in it. Commented Mar 27, 2013 at 15:13
  • What do you mean by This return a long list? You create a new ArrayList then add one item. There is no way it can contain a long list. Please post an example of name. Commented Mar 27, 2013 at 15:15
  • When I printe System.out.println(array); it gived a long list I posted it above Commented Mar 27, 2013 at 15:17
  • Please provide an example. Commented Mar 27, 2013 at 15:17
  • What is the content of the string parsed to the setter? Commented Mar 27, 2013 at 15:18

3 Answers 3

4

Java doesn't understand Json and basically what you're doing is add a string to an array

this.array.add(name); ---> add one value to the array, therefore the size is just one

you may need to use a specific Json library to parse the data in to an java arraylist.

regards

Alya'a Gamal
5,64822 silver badges34 bronze badges
answered Mar 27, 2013 at 15:16

2 Comments

So I have to split the String
yes, but you might have to do it by hand. Welcome to world of java =)
0

Look like you need to parse the String into pairs.

Looks to me like a Map might be the most appropriate structure to store the data in - I presume the first part from the value is unique.

Regex is probably the best approach to parsing the data:

public static void main(String[] args) {
 final String data = "[Brunnsparken, Göteborg]\n"
 + "[Brunnsgatan, Göteborg]\n"
 + "[Brunnslyckan, Lerum]\n"
 + "[Brunnsbotorget, Göteborg]\n"
 + "[Brunnsnäs, Ulricehamn]\n"
 + "[Brunnshult, Mellerud]\n"
 + "[Brunnsdal, Skövde]\n"
 + "[Brunns skola, Ulricehamn]\n"
 + "[Brunnsgården, Kungälv]\n"
 + "[Brunns kyrka, Ulricehamn]\n"
 + "[Boråsparken, Borås]\n"
 + "[Stadsparken, Ulricehamn]\n"
 + "[Lysekilsparken, Lysekil]\n"
 + "[Mössebergsparken, Falköping]\n"
 + "[Dalaborgsparken, Vänersborg]\n"
 + "[Rösparken, Åmål]\n"
 + "[Lillhagsparken Norra, Göteborg]\n"
 + "[Lillhagsparken Södra, Göteborg]\n"
 + "[Sylte Ryrbäcksparken, Trollhättan]\n"
 + "[Skogstomtsparken, Borås]\n"
 + "[Svinesundsparken, Norge]\n"
 + "[Håjumsparken, Trollhättan]\n"
 + "[Eriksdalsparken, Bollebygd]\n"
 + "[Fridhemsparken, Lidköping]";
 final Pattern pattern = Pattern.compile("\\[([^,]++),\\s++([^\\]]++)\\]");
 final Matcher matcher = pattern.matcher(data);
 final Map<String, String> items = new TreeMap<>();
 while (matcher.find()) {
 items.put(matcher.group(1), matcher.group(2));
 }
 for (final Entry<String, String> entry : items.entrySet()) {
 System.out.println(entry);
 }
}

Output from this:

Boråsparken=Borås
Brunns kyrka=Ulricehamn
Brunns skola=Ulricehamn
Brunnsbotorget=Göteborg
Brunnsdal=Skövde
Brunnsgatan=Göteborg
Brunnsgården=Kungälv
Brunnshult=Mellerud
Brunnslyckan=Lerum
Brunnsnäs=Ulricehamn
Brunnsparken=Göteborg
Dalaborgsparken=Vänersborg
Eriksdalsparken=Bollebygd
Fridhemsparken=Lidköping
Håjumsparken=Trollhättan
Lillhagsparken Norra=Göteborg
Lillhagsparken Södra=Göteborg
Lysekilsparken=Lysekil
Mössebergsparken=Falköping
Rösparken=Åmål
Skogstomtsparken=Borås
Stadsparken=Ulricehamn
Svinesundsparken=Norge
Sylte Ryrbäcksparken=Trollhättan

You can the access the items by looping (as above) or by getting values from the Map by key. The TreeMap I have used will sort the data by key, you can also use a LinkedHashMap to store the data in insertion order.

You could also store the items in a List of tuple like structures.

answered Mar 27, 2013 at 15:38

Comments

0
public void setLocationName (String name) {
 array = new ArrayList<String>();
 array.add(name);
 System.out.println(array); //This return a long list
 System.out.println(array.size()); //But this only return the value 1
}

You are creating a new ArrayList each time you call this method:

array = new ArrayList<String>();

You could just remove the above line, however I suggest you rename the method as this is no longer a setter and you are in fact now adding to an existing list each time you call this method.

I suggest what you want to do is build your List before parsing to the setter, perhaps using a foreach loop (I'm not sure what kind of object you are working with) and simplify your setter (setLocationName) to accomodate.

So it would become:

public void setLocationName(ArrayList<String> names)
 {
 this.array = names;
 System.out.println(array); //This return a long list
 System.out.println(array.size()); //But this only return the value 1
 }
answered Mar 27, 2013 at 15:34

3 Comments

Actually just moving the arrayList solved the whole problem :-) Now my combobox display all the items from my JSON. You solved it.
Problem by doing that is that you no longer have a traditional setter as you are actually now contributing to an existing list each time you call this method. So perhaps you should rename the method to something else at least.
Perhaps you should give this person credit for solving your problem.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.