Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Adding an object to an Arraylist before it's big enough  (Read 1944 times)
0 Members and 1 Guest are viewing this topic.
Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Posted 2012-07-09 01:12:33 »

So in my game, each room has it's own arraylist containing a bunch of entities
I'm dealing with this specific room (randomly generated) that has two entities (the exit and the entrance)
Now, it's VERY important that the objects are in a specific order in the arraylist, because the room-changing-code depends on it
The entrance must be at index 0, and the exit must be at index 1

It's not normally a problem, but because the room is randomly generated, I can't control which one (the exit or entrance) is created first
So half of the time, I get an error that I can't add the exit at index 1 because the size isn't big enough yet

But here's the thing, the API ( http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html ) says that when you create an arraylist through the parameter-less constructor (the one I use) it ensures a capacity of ten. So what gives? How can I make sure I can add an object at index 1 before I add one at index 0?

Thanks
Offline jammas615

Junior Member


Projects: 1



« Reply #1 - Posted 2012-07-09 01:22:26 »

Surely if you added your entrance and exit entities to the list before you generated the rest of the room you could solve that problem? Or not...

EDIT:
I didn't really understand your problem at first but here is what I see:
You generate the room, each entity gets generated randomly and added to the ArrayList and your entrance and exit are not necessarily generated at a time where there are added at index 0 and 1.
Is that right?

Offline sproingie

JGO Kernel


Medals: 202



« Reply #2 - Posted 2012-07-09 01:29:07 »

The capacity is just how much "slack" you have til it has to reallocate the underlying array.  It's purely an implementation detail you shouldn't worry about, and it doesn't mean you can index elements that don't exist.  Nor will it create intervening null elements if you try to assign to an index past the end.

Why aren't the entrance and exit simply fields on your class?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #3 - Posted 2012-07-09 01:31:51 »

Surely if you added your entrance and exit entities to the list before you generated the rest of the room you could solve that problem? Or not...

EDIT:
I didn't really understand your problem at first but here is what I see:
You generate the room, each entity gets generated randomly and added to the ArrayList and your entrance and exit are not necessarily generated at a time where there are added at index 0 and 1.
Is that right?

Yup, that's it
Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #4 - Posted 2012-07-09 01:34:14 »

The capacity is just how much "slack" you have til it has to reallocate the underlying array.  It's purely an implementation detail you shouldn't worry about, and it doesn't mean you can index elements that don't exist.  Nor will it create intervening null elements if you try to assign to an index past the end.

Why aren't the entrance and exit simply fields on your class?

Prooooobably because I don't know what a "field" is
:/
I'm learning all of this as I go along
I'll be sure to look up what that is
Offline Cero
« Reply #5 - Posted 2012-07-09 01:56:56 »

adding exit:
if  arraylist.size() == 0//, entrance wasnt added yet
save exit temporarily somewhere else, and add it AFTER arraylist is size 1 or greater
this all depends on your structure...

Offline nhmllr

Senior Member


Medals: 1
Projects: 3


slow and steady...


« Reply #6 - Posted 2012-07-09 01:59:48 »

adding exit:
if  arraylist.size() == 0//, entrance wasnt added yet
save exit temporarily somewhere else, and add it AFTER arraylist is size 1 or greater
this all depends on your structure...

To be honest, that's what I did, but I was kinda curious if there was another way that would be slightly simpler and cleaner (other than storing the location and other properties in ints and making it at the end), especially for bigger rooms with more entities but it's not that big a deal

But thanks to everyone for all their help, though! (Fields seem useful!)
Offline Cero
« Reply #7 - Posted 2012-07-09 12:28:03 »

adding exit:
if  arraylist.size() == 0//, entrance wasnt added yet
save exit temporarily somewhere else, and add it AFTER arraylist is size 1 or greater
this all depends on your structure...

To be honest, that's what I did, but I was kinda curious if there was another way that would be slightly simpler and cleaner (other than storing the location and other properties in ints and making it at the end), especially for bigger rooms with more entities but it's not that big a deal

But thanks to everyone for all their help, though! (Fields seem useful!)
well yeah, but it really depends on your structure
if you write it so that, naturally the entrance is generated first, problem solved.

Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #8 - Posted 2012-07-12 05:29:13 »

I'd recommend a new structure.

Make a class called "Entity" which stores the fields (global values which every entity needs; position, image etc.), and give it all the getters and setters you'll need for an entity. Then add a field: int type
If it's an entrance, then it has type=0, and if it's an exit it has type=1.
Then you can easily distinguish between the two, and make easy checks as to what's what, and what has already been added to your list.
AND, then you can just iterate through that list, and check which entity is an entrance and which entity is an exit by checking the type-field, so you don't need to have them on a specific spot in the list.

What I'd REALLY recommend, is that you read up on inheritance, because that can really save you time when having a lot of different things in your game-world. That Entity-class can be simple enough, to let you have other classes extend it (inherit all its methods and fields). That way you could also have a class called InteractableEntity, extending Entity, which holds an extra field String text, which you could use to display some information about the entity, if the player activates it somehow. Or a trap maybe.

- Jonas
Offline sproingie

JGO Kernel


Medals: 202



« Reply #9 - Posted 2012-07-12 06:08:10 »

Quote
If it's an entrance, then it has type=0, and if it's an exit it has type=1.

Oh Jesus Crack Smoking Christ, no.  Just no.  Use Classes.  Write it in Java, not some ad-hoc bastardized dynamic language embedded in Java.  Even if you litter your code with instanceof, it'd be better than this kind of nonsense.  This "entity system" insanity has got to stop, especially among people who aren't already experts in OOP to know what they're trying to replace.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Ultroman

JGO Knight


Medals: 25
Projects: 1


Snappin' at snizzes since '83


« Reply #10 - Posted 2012-07-12 06:20:59 »

Quote
If it's an entrance, then it has type=0, and if it's an exit it has type=1.

Oh Jesus Crack Smoking Christ, no.  Just no.  Use Classes.  Write it in Java, not some ad-hoc bastardized dynamic language embedded in Java.  Even if you litter your code with instanceof, it'd be better than this kind of nonsense.  This "entity system" insanity has got to stop, especially among people who aren't already experts in OOP to know what they're trying to replace.
Well, OP didn't know what 'fields' were, so I thought I'd give him an easy option for this game, and tell him that he should look into inheritance if he felt up to it. We're not all on the same level.

What would you suggest? I'm not a fan of doing instanceOf-checks, but what're you gonna do when you have a list of many objects in a room or inventory? You could have books the player can read, weapons you can equip, potions you can drink. Would you rather juggle several lists containing just one type of item each?

- Jonas
Offline sproingie

JGO Kernel


Medals: 202



« Reply #11 - Posted 2012-07-12 06:28:15 »

What I would suggest is learning the java language.  If you don't know what "fields" are, the last thing you should be doing is dinking around with entity systems.

The problem of finding applicable actions on an equipment list is somewhat tricky ... and is going to be made a whole lot more difficult if one doesn't understand the most basic things like what fields are.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (21 views)
2014-09-12 09:08:26

Norakomi (55 views)
2014-09-10 13:57:51

TehJavaDev (66 views)
2014-09-10 06:39:09

Tekkerue (33 views)
2014-09-09 02:24:56

mitcheeb (54 views)
2014-09-08 06:06:29

BurntPizza (38 views)
2014-09-07 01:13:42

Longarmx (24 views)
2014-09-07 01:12:14

Longarmx (30 views)
2014-09-07 01:11:22

Longarmx (28 views)
2014-09-07 01:10:19

mitcheeb (36 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!