java - Why I can't access array element? -


i have class named "soldier" , have class named "army" containing array of soldier class objects. army class defined this:

public class army {     array<soldier> army; 

then, in army class constructor i'm creating soldier objects array this:

public army(){     army = new array<soldier>(20); // here } 

so far iterating thru army array this:

for (soldier soldier : army) {     // soldier object } 

and worked well, when wanted nested iteration way (one loop inside one) messed - inner loop destroying outer loop iterator index.

so, want "manual" iteration, simple loop , looks this:

for (int i=0; i< army.army.size; i++) {     soldier soldier = army.army.items[i]; 

but i'm getting run time error (exception):

exception in thread "lwjgl application" com.badlogic.gdx.utils.gdxruntimeexception: java.lang.classcastexception: [ljava.lang.object; cannot cast [lcom.mgsoft.soldier; 

can please explain me what's going on here? why can't access soldier objects way , what's proper way access them?

in manual way :

for (int i=0; i< army.size(); i++) {   soldier soldier = army.get(i); 

if data arraylist can data directly get method. size of arraylist use listname.size()


Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -