| Author |
ArrayList is Picking Up Last Item During Iteration.
|
James Dekker
Ranch Hand
Joined: Dec 09, 2006
Posts: 215
|
|
I created a simple web service which consists of a Person class containing a List of Car classes.
Person class:
Car class:
Created a different object to store my inquries.
Here's my actual client (a Jersey based Restful Web Service):
When I call the getPerson() method as follows:
It returns a 201 containing this JSON Object (which is what I want):
However, when I call the getCar method() as follows:
I get the following JSON object (with the second item in the ArrayList being added!):
I wanted it to be Toyota instead of Ford.
If you can tell, I printed out the value of the car.getMake() String and inside the console / shell, it ways says this:
car.getMake(): Toyota
car.getMake(): Ford
So, it seems that its iterating through the list but adding the last item (or just next item)??
What am I possibly doing wrong? Thank you for taking the time to read this...
|
 |
Suresh Sajja
Ranch Hand
Joined: May 12, 2009
Posts: 34
|
|
James Dekker wrote:
1. If loop is not coded correctly. semicolon is added at the end of if condition.
by doing so, if condition is evaluated and does nothing. and the next two lines are executed for all cars irrespective of if result.
2. you may have to call "name.equalsIgnoreCase(car.getMake)"
If you can tell, I printed out the value of the car.getMake() String and inside the console / shell, it ways says this:
car.getMake(): Toyota
car.getMake(): Corolla // I assume you meant Ford here
|
~Suresh
|
 |
James Dekker
Ranch Hand
Joined: Dec 09, 2006
Posts: 215
|
|
Thanks for responding, Suresh!
Yes, I meant "Ford" instead of "Corolla" and even edited my original post to reflect that.
1. If loop is not coded correctly. semicolon is added at the end of if condition.
by doing so, if condition is evaluated and does nothing. and the next two lines are executed for all cars irrespective of if result.
2. you may have to call "name.equalsIgnoreCase(car.getMake)"
1. Can you elaborate on this? I think Eclipse might have added this (the ";") in... This does look odd... Should the semicolon be removed?
It does seem like the conditional "if" isn't picking up or getting invoked properly because both cars are printed to the console.
2. Will try your suggestion with the "name.ignoreCase(car.getMake)"
With thanks!
|
 |
Jan Hoppmann
Ranch Hand
Joined: Jul 19, 2010
Posts: 99
|
|
James Dekker wrote:
1. Can you elaborate on this? I think Eclipse might have added this (the ";") in... This does look odd... Should the semicolon be removed?
It does seem like the conditional "if" isn't picking up or getting invoked properly because both cars are printed to the console.
In Java, a semicolon ends a statement. So your program evaluates the if, but there is nothing it does afterwards (because of the ;, which is basically an empty statement on the same line). You should remove it, yes.
|
Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
|
 |
James Dekker
Ranch Hand
Joined: Dec 09, 2006
Posts: 215
|
|
Jan & Suresh,
Thank you! The semicolon was the thing that was breaking my code.
Good eyes! Can't believe I missed that.
|
 |
 |
|
|
subject: ArrayList is Picking Up Last Item During Iteration.
|
|
|