PDA

Click to See Complete Forum and Search --> : Array class -- like c++ vector? memory + pointers


Halsafar
Sep 28th, 2005, 05:32 PM
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?

hYph3n
Oct 1st, 2005, 07:48 AM
you can use java vector class.

for more info try to study the collections of java

pieterblomme
Oct 1st, 2005, 10:09 AM
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.

Halsafar
Oct 1st, 2005, 10:55 AM
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.

CornedBee
Oct 2nd, 2005, 04:04 AM
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.)