0

Can I call a function in ArrayList without having to use a for loop or is there a anthoer way of doing this I hope my example will make more sense.

   public class Fireball {

      public void draw(GL10 gl){
         draw(gl);
      }

public class Gameview {

   private ArrayList<FireBall> fireBall = new ArrayList<FireBall>();

   public draw(GL10 gl){
     //this is where I what to draw everthing in the ArrayList
       fireBall.getIndex(ALL).draw(gl)
    }
}

the reason I am asking is because I what to be able to add and remove fireball without having to worry about the computer speed thank you

2 Answers 2

1

Iterating through an ArrayList is not that cpu-consuming (although using vectors you could optimize even more), and even if you had a method of drawing multiple fireballs at once, that method would still iterate through the ArrayList just as you would.

Sign up to request clarification or add additional context in comments.

2 Comments

Why would a Vector be faster than an ArrayList? If anything, is it not the other way around (because Vector has synchronization overhead)?
ah sorry, by vector i meant Fireball[]. Edit: I only now realized that what my professor called "vector" everybody else calls an "array" ...
0

Since you're using an arraylist, you could use a while loop.
Just check is the contents of the list isn't null

ArrayList al = new ArrayList();
al.add("a");
al.add("b");
al.add("c");
al.add("d");
al.add("e");

int counter = 1;
while (!al.isEmpty()) {
   System.out.println(counter++ + ": " + al.toString());
   al.remove(0);
}

Output:

1: [a, b, c, d, e]
2: [b, c, d, e]
3: [c, d, e]
4: [d, e]
5: [e]

Advantage is that it uses less memory, since your deleting unused elements, but if you want to reuse the arraylist, you should just loop through the elements:

fireball.getIndex(ALL);
while (Fireball f : fireball) {
     f.draw(gl)
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.