Jump to content

  • Log In with Google      Sign In   
  • Create Account


Iterating through a collection


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 TheChubu   Crossbones+   -  Reputation: 3771

Like
0Likes
Like

Posted 03 October 2012 - 09:40 PM

Hi! I have a simple issue with collections right now.

See, I've run into the same rock twice. There is a collection with things in it, and I have to do something with the first thing that appears of that collection that meets certain condition, then stop it right there.

I just don't know how to do that.

Today I had an OOP exam, we use Java. I had to iterate through an ArrayList of airstrips to see if they were empty. If I find an empty airstrip, then I assign to it a plane that is waiting to land. I just didn't knew how to do that.

First I tried a for with an iterator (the code did more stuff but the essential is this) :

for (AirstripClass airstrip : airstripArray)
	 {
     if (airstrip.getPlane == null)
               airstrip.addPlane(plane);
      else
               System.out.println("There are no empty airstrips");
}

That didn't worked. It ended up assigning the same plane to all the empty airstrips. Then I tried a common for loop with an extra boolean condition:

int i;
boolean foundEmptyStrip = false;

for (int i = 0; i < airstripArray.size();  foundEmptyStrip != true; i++ )
        {
         if (airstripArray[i].getPlane == null)
                {
                airstripArray[i].addPlane(plane);
                foundEmptyStrip = true;
                }
         else
                 System.out.println("There are no empty airstrips");
}

Couldn't get that one working either. Wait... I'm so stupid, I've should initialized the boolean inside the for right? Anyway it wouldn't have worked, it would print "There are no empty airstrips" each time it finds a filled airstrip (which could happen several times until it finds an empty airstrip in the array).

I had a similar issue in a final a month ago. In that course we used Ada (similar to Pascal). I had to traverse a character array for a specific character, replace it, and then stop it right there. Made a for loop (in Ada you can't use several conditions on the same for loop as far as I know) and used an exit in the middle. It worked fine but the teacher said it was wrong using an exit because it doesn't fits in the paradigm (pretty much like a goto).

Gah! I just can't code in such time restraints like the ones we have in exams. If you say to me "Have it ready for next week", there is no problem, even if I end up actually coding it in a few hours, now if you tell me "Have it ready in three hours, by 8pm" then I couldn't code to save my life.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator


Sponsor:

#2 Ashaman73   Crossbones+   -  Reputation: 6763

Like
1Likes
Like

Posted 03 October 2012 - 11:40 PM

Sure, you could use break to exit the loop, but if your teacher don't like it (it is infact essentially a goto), then your second is almost right:

[source lang="java"]int i;boolean foundEmptyStrip = false;for (int i = 0; i < airstripArray.size() && foundEmptyStrip != true; i++ ) { if (airstripArray[i].getPlane == null) { airstripArray[i].addPlane(plane); foundEmptyStrip = true; }}if(i== airstripArray.size() ){ System.out.println("There are no empty airstrips");}[/source]

Edited by Ashaman73, 03 October 2012 - 11:41 PM.


#3 TheChubu   Crossbones+   -  Reputation: 3771

Like
0Likes
Like

Posted 04 October 2012 - 12:14 AM

Thanks! That one worked. Also tried with the for each but with a flag in the middle. Essentially, once it found an empty strip, it assigned to it the plane, assigned true to the flag, the for kept running but did nothing once the flag was set to true. Trows a ConcurrentModificationException in a subsequent method call that also uses a for each (the iterators are multi threaded or something?).

The one you posted is better since it stops iterating right when I want it to stop, but I also get a ConcurrentModificationException on a for each for some reason... Any ideas?

EDIT: NVM, the for each I was using was meant to land a plane on the waiting list (ArrayList) once another plane landed and its airstrip became empty. I used a for each in the airstrip to try to land every plane that was waiting, only the first one was meant to actually land (since this method is called every time an airstrip becomes empty), the others would fail.

So I removed the for each and just tried to make land the first plane of the waiting list and it worked. No more exceptions and the console output its the correct.

Edited by TheChubu, 04 October 2012 - 12:30 AM.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS