|
Author |
Topic: Object naming and manipulation (Read 538 times) |
|
spectre_240sx
|
Object naming and manipulation
« on: Mar 27th, 2004, 3:49am » |
|
I'm fairly new to object oriented programming, and I'm having trouble figuring out how to name groups of objects. Currently, in one of my programs, each object is being created as part of an array during the setup loop, but this has limited me as far as what I can do after they've all been created. I can obviously still create new objects, but the question becomes how to name and refer to them. Up until now I have been just looping through the array that I set them up with. That's not going to work when I start having objects outside of that array... I'd be extremely grateful if anyone could shed some light on this or point me towards some useful texts / books on the matter. Thanks!
|
|
|
|
arielm
|
Re: Object naming and manipulation
« Reply #1 on: Mar 27th, 2004, 8:26am » |
|
actually, using arrays to store references to multiple objects is the most used method and generally it works very well. in addition, there's the java "Vector" class which is a growable array: it can be useful when you need to create new objects dynamically and you don't know in advance how many of them are needed (+ it's also possible to remove objects easily...) on Mar 27th, 2004, 3:49am, spectre_240sx wrote:I can obviously still create new objects, but the question becomes how to name and refer to them. Up until now I have been just looping through the array that I set them up with. That's not going to work when I start having objects outside of that array... |
| it's not clear why "that's not going to work"... can you give more details
|
Ariel Malka | www.chronotext.org
|
|
|
spectre_240sx
|
Re: Object naming and manipulation
« Reply #2 on: Mar 28th, 2004, 7:14pm » |
|
Well, the programs loop right now is: Code: for (int i=0; i < arrayArea; i++){ objAnt[i].mtdDrawAnt(objAnt[i].antX, objAnt[i].antY, antSize); //Draw ant with X, Y, and Size info objAnt[i].mtdAntDirection(); // Set the direction of the ants for the next loop objAnt[i].mtdAntMotion(); // Set the position of the ants for the next loop if (loopnum % 200 <= 0) { objAnt[i].antAge ++; //println(objAnt[i].antAge); } } |
| If I've killed an object from the array, isn't it going to throw me an error? As far as making it grow, however, is there a way to get the maximum number of the array? (If I use the vector/ growable array that you mentioned)
|
|
|
|
arielm
|
Re: Object naming and manipulation
« Reply #3 on: Mar 28th, 2004, 10:26pm » |
|
on Mar 28th, 2004, 7:14pm, spectre_240sx wrote:If I've killed an object from the array, isn't it going to throw me an error |
| why should java throw you an error afterall, in your array: you're only removing a reference to an object, if it's the only reference to that object, it will be finalized (i.e. killed), otherwise it will stay alive. in general: all these things should'nt make you some troubles, because you're the one who is in control here (you create new objects and decide when to act on them...) concerning the use of "Vector": since it's a growable/shrinkable array, yes, it can let you know what its size is, but they (vectors) are a headache to work with...
|
Ariel Malka | www.chronotext.org
|
|
|
spectre_240sx
|
Re: Object naming and manipulation
« Reply #4 on: Mar 29th, 2004, 12:41am » |
|
Mmm, I'm finding they're even a pain to read about. I haven't been able to find a single site that gives an example of how to use them, lol. Thanks for your help though, you've cleared up a lot for me!
|
|
|
|
|