|
-
Sep 28th, 2005, 05:32 PM
#1
Thread Starter
PowerPoster
Array class -- like c++ vector? memory + pointers
Now I find myself writing programs in Java and finding I hate the language.... Mostly the lacks of memory allocation, pointers, bitwise operations, dereference operator... So maybe someone can help me out here.
I need a container class which can store the reference of many objects of one type. Example:
class Asteroids <-- Simply Asteroid
class AsteroidContanier <-- A class to manage all asteroids via 1 array
Now, I think I must be missing something in this language because I cannot devise a way in my head to write my own array class similar to that of std::vector. Although I want to avoid writing my own array class and hopefully be told there is one within the Java Libraries. However I would like to know how it works.
See I need to be able to push() and pop()... Is this possible...
So my question becomes?
- How can I dynamically resize an array at runtime?
- Any recommended readings on this topic for Java?
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Oct 1st, 2005, 07:48 AM
#2
Member
Re: Array class -- like c++ vector? memory + pointers
you can use java vector class.
for more info try to study the collections of java
-
Oct 1st, 2005, 10:09 AM
#3
New Member
Re: Array class -- like c++ vector? memory + pointers
Create the AsteroidContainer and give it a Vector as atttribute.
Add the Asteroids to the Vector.
on java.sun.com, you can find all the info regarding Vectors, and also the methods it has.
-
Oct 1st, 2005, 10:55 AM
#4
Thread Starter
PowerPoster
Re: Array class -- like c++ vector? memory + pointers
I take it the Vector class works much like the std::vector from c++.
Hmmmm...
I'm still stumped on how I can write my own class to push/pop data, a linked list as well without memory managment and pointers.
Any tutorials? There has to be a way to resize arrays at runtime.
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
-
Oct 2nd, 2005, 04:04 AM
#5
Re: Array class -- like c++ vector? memory + pointers
The normal Java object references are pretty much the same as pointers. You still have memory management - you just don't release objects explicitely.
As for resizing - no. You can only allocate another array and use java.lang.System.arraycopy to copy over the contents.
The source for the standard libraries comes with the SDK. You could take a look at the java.util.ArrayList and java.util.LinkedList classes. (java.util.Vector is an old variant of ArrayList - it is thread-safe, unlike ArrayList, and thus slower.)
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|