Slick Forums

Discuss the Slick 2D Library
It is currently Thu Jun 20, 2013 10:43 am

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Mon Oct 29, 2012 6:10 am 
Offline

Joined: Tue Mar 06, 2012 1:15 am
Posts: 25
Hi all,
Just wanna ask how to loop through the ImmutableBag.

right now im using
Code:
for (int counter = walls.size(); counter > 0; counter--) {
         Entity wall = walls.get(counter);
         checkCollision(wall);
      }


but im having null result for the entity.

Best regards


Top
 Profile  
 
PostPosted: Mon Oct 29, 2012 8:11 pm 
Offline
Game Developer
User avatar

Joined: Tue Nov 21, 2006 4:46 am
Posts: 620
Location: Iceland
Iterating an array is straight forward using a for loop, like you do, although I usually iterate by doing:
for(int i = 0; bag.size() > i; i++) {
...
}
But your way works too.

I need more information to answer you about the NPE.


Top
 Profile  
 
PostPosted: Mon Oct 29, 2012 11:52 pm 
Offline

Joined: Sun Aug 05, 2012 3:37 pm
Posts: 23
wakkana12 wrote:
Hi all,
Just wanna ask how to loop through the ImmutableBag.

right now im using
Code:
for (int counter = walls.size(); counter > 0; counter--) {
         Entity wall = walls.get(counter);
         checkCollision(wall);
      }


but im having null result for the entity.

Best regards


By the looks of it, you are starting beyond the end of the bag. Let's say the size of the ImmutableBag is 3. You should only be indexing from 0 through 2. The code above starts with a counter value of 3.

--tim


Top
 Profile  
 
PostPosted: Tue Oct 30, 2012 6:49 pm 
Offline

Joined: Fri Jul 13, 2012 9:59 pm
Posts: 25
tescott wrote:
By the looks of it, you are starting beyond the end of the bag. Let's say the size of the ImmutableBag is 3. You should only be indexing from 0 through 2. The code above starts with a counter value of 3.
--tim


Exactly. Also, if you want a literal java iteration with Iterable, you might want to implement the Iterable interface.


Top
 Profile  
 
PostPosted: Wed Dec 12, 2012 1:32 pm 
Offline

Joined: Sat Jun 23, 2012 4:14 pm
Posts: 74
Location: Germany
Shirkit wrote:
Exactly. Also, if you want a literal java iteration with Iterable, you might want to implement the Iterable interface.


Yeah, would be awesome to being able to iterate like that:
Code:
for (Entity fish : fishes) // fishes is a Bag
{
   // do something with element
}


Top
 Profile  
 
PostPosted: Mon Mar 18, 2013 10:49 pm 
Offline

Joined: Mon Mar 18, 2013 10:42 pm
Posts: 4
Yes, I was looking at this as well.

The ImmutableBag should be extending Iterable like other collections.

I guess the knock on affect would mean that each concrete instance which uses it would need an iterator implementation. But it'll allow for the use of enhanced loops.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group