Iterator on JsonArray issue

1,780 views
Skip to first unread message

Scott Brown

unread,
Jun 11, 2013, 1:30:28 PM6/11/13
to ve...@googlegroups.com
Good afternoon,

I got around this by using a for loop, but I'm confused as how the iterator is supposed to function?


This code with a JsonArray containing one JsonObject results in an infinite loop:

while(jsonArray.iterator().hasNext()) {
   
JsonObject myObject = jsonArray.iterator().next();
}


This code with a JsonArray containing one JsonObject results in an exception on the call to remove:

while(jsonArray.iterator().hasNext()) {
   
JsonObject myObject = jsonArray.iterator().next();
   jsonArray
.iterator().remove();
}

Was I doing it wrong?

Tim Yates

unread,
Jun 11, 2013, 3:15:34 PM6/11/13
to ve...@googlegroups.com
Not sure why you are creating a new iterator every time you try to access it...

Have you tried:

Iterator i = jsonArray.iterator() ;
while( i.hasNext() ) {
    Object myObject = i.next() ;
}

Tim



--
You received this message because you are subscribed to the Google Groups "vert.x" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vertx+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Tim Fox

unread,
Jun 11, 2013, 3:31:54 PM6/11/13
to ve...@googlegroups.com
On 11/06/13 20:15, Tim Yates wrote:
> Not sure why you are creating a new iterator every time you try to access
> it...
>
> Have you tried:
>
> Iterator i = jsonArray.iterator() ;
> while( i.hasNext() ) {
> Object myObject = i.next() ;
> }

+1

or even better

for (Object myObject: jsonArray) {

Mariam Hakobyan

unread,
Dec 4, 2013, 8:27:26 AM12/4/13
to ve...@googlegroups.com
Hi Tim,

I have exactly the same problem, and I am not sure iterator() method works correctly.
I am using JsonArray wich was constructed like this:

JsonArray array = new JsonArray(new String[] {"text1", "text2", "text3"});

and when I try to iterate through it:

Iterator iterator = array.iterator();
if(iterator.hasNext()) {
     nextAddress = (String)iterator.next();
     iterator.remove();
}

I get a java.lang.UnsupportedOperationException exception on remove() method. Am I doing anything wrong here? Is this supposed to work?

Thanks in advance,
Mariam

Stream Liu

unread,
Dec 4, 2013, 9:13:43 PM12/4/13
to ve...@googlegroups.com

Well, you create JsonArray with an Array which make list in
JsonArray as a fixed-size list, so iterator remove dosen't support this
operation.

You could create JsonArray with List<String> which would be work as you
wanna.

but i think this would be a little fault of vert.x
>> > On 11 June 2013 18:30, Scott Brown <scott...@gmail.com <javascript:>>
>> wrote:
>> >
>> >> Good afternoon,
>> >>
>> >> I got around this by using a for loop, but I'm confused as how the
>> >> iterator is supposed to function?
>> >>
>> >>
>> >> This code with a JsonArray containing one JsonObject results in an
>> >> infinite loop:
>> >>
>> >> while(jsonArray.iterator().hasNext()) {
>> >> JsonObject myObject = jsonArray.iterator().next();
>> >> }
>> >>
>> >>
>> >> This code with a JsonArray containing one JsonObject results in an
>> >> exception on the call to remove:
>> >>
>> >> while(jsonArray.iterator().hasNext()) {
>> >> JsonObject myObject = jsonArray.iterator().next();
>> >> jsonArray.iterator().remove();
>> >> }
>> >>
>> >> Was I doing it wrong?
>> >>
>> >> --
>> >> You received this message because you are subscribed to the Google
>> Groups
>> >> "vert.x" group.
>> >> To unsubscribe from this group and stop receiving emails from it, send
>> an
>> >> email to vertx+un...@googlegroups.com <javascript:>.

Mariam Hakobyan

unread,
Dec 5, 2013, 9:55:06 AM12/5/13
to ve...@googlegroups.com
Hi Stream.liu,

Thanks for the answer, that was the cause of the exception. When I use List<Object> (List<String> as an argument of JsonArray is not supported by vertx), it works fine.

Mariam
Reply all
Reply to author
Forward
0 new messages