Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

3 lines of strange code - for cycle

3 views
Skip to first unread message

Fender

unread,
Oct 14, 2010, 11:39:01 AM10/14/10
to
Hi all!

during a debugging I encountered the following strange behaviour.
Here's the code:


ArrayList<String> selectedCardsIDs = new ArrayList<String>();

for (int i=0; i<this.selectedCards.size(); i++)
selectedCardsIDs.add(this.selectedCards.get(i).getId());

In my debug I have initially this.selectedCards.size() = 3, after the
first cycle it becomes strangely this.selectedCards.size() = 2!

I have to specify that this.selectedCards.get(i).getId() it's just a
getter...

Anyone can find an answer?
Thank you!

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---

projectmoon

unread,
Oct 14, 2010, 11:43:16 AM10/14/10
to

Is there multithreaded (or unmultithreaded) code elsewhere modifying the
size of the list? Is your debugging tests somehow modifying the size of
the list?

Fender

unread,
Oct 14, 2010, 1:24:05 PM10/14/10
to

The code works, it was just modified by another code via a pointer of an
element of the list.

Fred

unread,
Oct 14, 2010, 2:18:24 PM10/14/10
to
On Oct 14, 8:39 am, Fender <tr...@guessmyemail.com> wrote:
> Hi all!
>
> during a debugging I encountered the following strange behaviour.
> Here's the code:
>
> ArrayList<String> selectedCardsIDs = new ArrayList<String>();
>
> for (int i=0; i<this.selectedCards.size(); i++)                              
>       selectedCardsIDs.add(this.selectedCards.get(i).getId());
>
> In my debug I have initially this.selectedCards.size() = 3, after the
> first cycle it becomes strangely this.selectedCards.size() = 2!
>
> I have to specify that this.selectedCards.get(i).getId() it's just a
> getter...
>
> Anyone can find an answer?
> Thank you!

does selectedCards.get(index) remove the item from the list, thus
reducing the list's size by one?
--
Fred K

projectmoon

unread,
Oct 14, 2010, 5:40:47 PM10/14/10
to

Yup no problem.

Roedy Green

unread,
Oct 21, 2010, 4:42:56 PM10/21/10
to
On Thu, 14 Oct 2010 17:39:01 +0200, Fender <tr...@guessmyemail.com>
wrote, quoted or indirectly quoted someone who said :

>
>Anyone can find an answer?

What you could do is write a wrapper around ArrayList that dumps the
size of the array and a stack trace any time it could be potentially
changed.

You could also simply trace the code in a debugger, and watch where
the size changes unexpectedly.

In IntelliJ there is a feature to find all references to a given
variable. Check there are no strays pointing to your ArrayList.
--
Roedy Green Canadian Mind Products
http://mindprod.com

Microsoft has a new version out, Windows XP, which according to everybody is the "most reliable Windows ever." To me, this is like saying that asparagus is "the most articulate vegetable ever."
~ Dave Barry

0 new messages