• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
TheChubu

Iterating through a collection

2 posts in this topic

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) :

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

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:

[code]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");
}[/code]

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.
0

Share this post


Link to post
Share on other sites
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
1

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0