Problems using Vector.

I'm using Vector to hold objetcs from an existing class that I've created before.

But I need to call the methods of that class, and I can't do it.

Like this:

publicclass MyClass exteds OtherClass{

publicvoid setSomething(){

// do something.

}

}

And now using the Vector:

publicclass AnotherClass{

Vector vector =new Vector();

for(int i = 0; i < 8; i++)

vector.addElement(new MyClass());

// Now I need to call the method defined in MyClass, but the objects are inside Vector.

for(int i = 0; i < vector.size(); i++)

vector.elementAt(i).setSomething;// But setSomething is not recognized by compiler!

}

How can I fix that? I'm using j2me and I can't create objects to hold the contents of Vector, since the program is in loop and would be waste of memory and less process power.

Thank for any help.

[1574 byte] By [coffee95a] at [2007-11-27 3:51:11]
# 1

If you're using Java 5 onwards, you can use generics:

Vector < MyClass > vector = new Vector < MyClass > ();

vector.add(new MyClass());

vector.elementAt(0).performMyClassMethod();

if you're not, you have to cast

Vector vector = new Vector();

vector.add(new MyClass());

MyClass myClass = (MyClass) vector.elementAt(0);

myClass.doWhatever();

georgemca at 2007-7-12 8:55:09 > top of Java-index,Java Essentials,New To Java...
# 2

I can't use generics, since I'm using J2ME...

The are problems if I cast the objects of Vector.

I'm using Vector because during the program cycle the size of Array are increased.

But I can't create local variables to cast and hold the contens of Vector because the objects inside Vector must be seen by whole program!

coffee95a at 2007-7-12 8:55:09 > top of Java-index,Java Essentials,New To Java...
# 3
> But I can't create local variables to cast and hold the contens of Vector because the objects inside Vector must be seen by whole program!Try it and you'll see it works.
DrLaszloJamfa at 2007-7-12 8:55:09 > top of Java-index,Java Essentials,New To Java...
# 4

from this

Vector vector = new Vector();

vector.add(new MyClass());

MyClass myClass = (MyClass) vector.elementAt(0);

myClass.doWhatever();

to this.

Vector vector = new Vector();

vector.add(new MyClass());

((MyClass) vector.elementAt(0)).doWhatever();;

you don't have temp variable here

And I don't see why you can't use temp variable.

rym82a at 2007-7-12 8:55:09 > top of Java-index,Java Essentials,New To Java...
# 5

for(int i = 0; i < enemy.size(); i++)

(EnemyClass)enemy.elementAt(i).moveSprite(enemy.elementAt(i).getDirection());

The error is: not a stament.

enemy is the Vector. The enemy Vector holds objects from the Class EnemyClass. So the moveSprite and getDirection are methods from the class EnemyClass.

How fix it?

coffee95a at 2007-7-12 8:55:09 > top of Java-index,Java Essentials,New To Java...
# 6
> But I can't create local variables to cast and hold the contens of Vector because the objects inside Vector must be seen by whole program!Try it and you'll see it works.
GhostRadioTwoa at 2007-7-12 8:55:09 > top of Java-index,Java Essentials,New To Java...
# 7
Check the location where you should put the "(" and ")"You should have your target Object followed by Vector element, all rounded by the "(" and ")". Then make a method call.
rym82a at 2007-7-12 8:55:09 > top of Java-index,Java Essentials,New To Java...
# 8
I've tried:(EnemyClass)enemy.elementAt(i).moveSprite((EnemyClass)enemy.elementAt(i).getDirection());But the same error occurs: Not a stament.
coffee95a at 2007-7-12 8:55:09 > top of Java-index,Java Essentials,New To Java...
# 9

> I've tried:

>

> > (EnemyClass)enemy.elementAt(i).moveSprite((EnemyClass)

> enemy.elementAt(i).getDirection());

>

>

> But the same error occurs: Not a stament.

Simplify it. You're in brackets hell, casting the wrong things. Use some local variables, do it step by step

edit: Not a statement? Post the code in context, that is, the method it's in. Something smells fishy here

Message was edited by:

georgemc

georgemca at 2007-7-12 8:55:09 > top of Java-index,Java Essentials,New To Java...
# 10

From this:

(EnemyClass)enemy.elementAt(i).moveSprite((EnemyClass)enemy.elementAt(i).getDirection());

To this:

EnemyClass e = (EnemyClass) enemy.elementAt(i);

e.moveSprite(e.getDirection());

]

Aahh!

And by the way, why not have a method:

e.MoveSprintInCurrentDirection(); //done

DrLaszloJamfa at 2007-7-12 8:55:09 > top of Java-index,Java Essentials,New To Java...
# 11
Put your target Object Type(TargetClass)Followed by the Vector Element(TargetClass) vector.elementAt(0)All rounded by bracket "(" and ")"((TargetClass) vector.elementAt(0))Then make a method call((TargetClass)
rym82a at 2007-7-12 8:55:09 > top of Java-index,Java Essentials,New To Java...
# 12
Given the size of the whole expression, and the common subexpression,that really does cry out for a local variable to simplify it.
DrLaszloJamfa at 2007-7-12 8:55:09 > top of Java-index,Java Essentials,New To Java...
# 13

Here is the EnemyClass:

public class EnemyClass extends Sprite {

public void moveSprite(char direction) {

// stuff here.

}

public char getDirection() {

return this.direction;

}

}

coffee95a at 2007-7-12 8:55:09 > top of Java-index,Java Essentials,New To Java...
# 14

The point is there are many calls to several methods of EnemyClass, who checks for collisions and so on...

I think everytime it enters on the loop for check collision or move enemies (wich is done all time) create the same number of objetcs to hold the same number of objects inside Vector could be expansive to a game mobile device...

That's why I was trying not to create local variables to hold contents of Vector everytime the loop occurs, and this way I was trying to use de contents of Vector, since they are objects of type EnemyClass, so I thought I could call the methods of EnemyClass directly.

Thanks!

coffee95a at 2007-7-12 8:55:09 > top of Java-index,Java Essentials,New To Java...
# 15

> That's why I was trying not to create local variables to hold contents of Vector

> everytime the loop occurs, and this way I was trying to use de contents of Vector,

> since they are objects of type EnemyClass, so I thought I could call the

> methods of EnemyClass directly.

You are misunderstanding how Java works. The example is the same, with

an array, except that we can avoid getting confused by the cast:

Emeny[ ] enemies = new Emeny[LOTS];

//array gets filled...

//later, we access an enemy:

Emeny e = enemies[offset];

e.someMethod();

The array, like the Vector is populated not with Enemy objects, but with

*references* to Enemy objects. Variable e is a copy of such a reference;

both it and the reference it was copied from point to the same *object*.

This is basically how Java handles object reference. It really is at the

core of the language and it behooves you to understand it forwards and

backwards.

DrLaszloJamfa at 2007-7-21 20:56:46 > top of Java-index,Java Essentials,New To Java...