Ok, so I'm planning on using ArrayList<> to add a PlayerRocket every time a rocket is fired.

I know that I can add them by using the add(Object o) method of the ArrayList<>.

Now what my problem is, is that my PlayerRocket has a boolean which says whether the rocket is active or not, and when that equals false, i need to remove that specific rocket from the arraylist. When i read the documentation, it says that using the method remove(Object o) removes the first one of that object off the list, how can i remove specific ones.

OR

is there an alternative, maybe where i can create my own ArrayList<> type class because i will need to iterate through the list as well?

Thanks for the help so far, I think I can get to answer from here, what I'm looking for is once the active boolean equals false, then the rocket needs to be removed from the list, but I'm sure I can simply do an if check in the loop, my only problem is, i'll need it to iterate through this every time i call the repaint method will this slow down my game?Because the rocket shouldnt be rendered if it isnt active, that way i want to remove it from the screen and list.

It sounds like pretty standard bullet removal. My standard approact is as follows (not in code, just example)

on each render(re paint)

loop through bullets (as in previous post)check if the bullet can be remove (I useally give bullets a 'life time' and when they hit zero (and have not hit anything) remove them).if it needs removing, remove it (as shown in previous post).repeat.

Unless there are thousands of bullets (rockets in this case) or they need a lot of logic ran against them or the game is running on an old/slow device, just do this in the render loop.

Err... Yeah blame RIM. I'm working with the BlackBerry JDE 4.5.0 and when i try and import it, it wont let me, which means I can't use it So now i need to try and create a clone of it, which I'm skeptical of, because I'm not sure what the <> part of the ArrayList means.

Very odd. I have not worked with the blackberry JDT so cannot really comment.

<> is the object type. You can ignore this and get away with some clever tricks (as mentioned by a previous user)

but it just means that if your Array (or ArrayList) if going to hold a bunch of strings, you are telling it in advance. Just tell it the class. in this case, your array holds many PlayerRocket(s) so 'new ArrayList<PlayerRocket>'

I wouldn't use an arraylist in this case. Maybe you could have a look at LinkedList, it's much faster in removing elements.In addition you can't remove an element out of the list while you are iterating that way.

I wrote a really hacky solution for this, the reason is because when you get more complex iterations you cannot use an iterator and predict which elements to remove via it.

Like for example, the UI subsystem receives a mouse-press, the UI subsystem iterates through it's list of buttons and execute the appropriate button's handler. In the button's handler you remove the button from the UI subsytem, thus throwing a concurrent modification exception.

You can create a shallow clone your set as you iterate (not very memory efficient), or you can use CopyOnWriteArrayList . I wrote a solution for this (I am not very familiar with the JDK so it may be a redundant implementation) but you can see it here. It essentially stores all mutations to the list and only applies them when data is required to be retrieved about the arraylist. I.e, inserting an element or removing an element doesn't do anything immediately to the list. Creating an iterator into the list applies the mutations, getting an element from the list applies the mutations, getting the size of the list applies the mutations etc.

// Methods// ConstructorpublicArrayList() {// set the default capacity of the arrayelements = newPlayerRocket[DEFAULT_CAPACITY];

}// Add the object to the arraypublicvoidadd(PlayerRockete) {if(getSize() >= DEFAULT_CAPACITY) {// Do nothing can't have more than 16 on screen at once }else {// add the object at specific pointelements[size++] = e; } }

Jeremy in your StaticSet.java you use ArrayList and Iterator. Neither of these are available to me whereas the NoSuchElement is.Could I possibly use my own ArrayList as a substitute for the ArrayList<>. However the iterator part i@m not too sure on.

Jeremy in your StaticSet.java you use ArrayList and Iterator. Neither of these are available to me whereas the NoSuchElement is.Could I possibly use my own ArrayList as a substitute for the ArrayList<>. However the iterator part i@m not too sure on.

Replace ArrayList with UnsortedReadableList (of course, now they won't be sorted.)

Yeah, they've left out some classes that make things easier. I'll use the unsortedreadablelist. Does sorting matter? and I'll also check the enumeration for the for each, can i not simply use a basic for loop for this?

Yeah, they've left out some classes that make things easier. I'll use the unsortedreadablelist. Does sorting matter? and I'll also check the enumeration for the for each, can i not simply use a basic for loop for this?

Scratch my solution. You need a sorted list to store the mutations (they need to be applied in the proper order.)

Sorry for wasting your time.

That BB JDE seems terribad. Last I checked, BlackBerry (canadian company) was doing terribly and is thinking of selling to an Chinese organization.

The simplest solution for this (psuedo code)

ArrayList garbage;

for(item i : myList){ if(someCondition) garbage.add(i);}

myList.removeAll(garbage);

But it isn't very memory efficient. You could reuse your garbage arraylist which would help a lot. Ofcourse, you might run into the problem I've stated above (in my first post in this thread)

I think it might, if i can create a way to clone it then i could possibly clone the arraylist to another arraylist, remove the object from the temporary arraylist and then at the end set the permanent one the same as the tmp one?

I think it might, if i can create a way to clone it then i could possibly clone the arraylist to another arraylist, remove the object from the temporary arraylist and then at the end set the permanent one the same as the tmp one?

Yeah, cloning should work fine. Just make sure you only do a shallow clone. The only problem with this approach (and if it isn't a problem for you - and it probably isn't) is that you end up creating a lot of shallow clones. Just be clever about when you clone and it shouldn't be a problem.

Yeah, for the cloning I'm thinking of two solutions to it.To clone it everytime i remove an element, or to create another array with each of the positions of the objects to be removed, then clone the arraylist, iterate through the array with the positions, then remove the objects from the temporary one, and then set the original list the same as the temp one.

Multiple rockets will fire, but if i move the ship, therefore moving the starting x position of the rocket im getting the array out of bounds exception. Here is my porjectile, player rocket, array list, and methods that i use that when firing a rocket.

// Add the object to the arraypublicvoidadd(PlayerRockete) {if(getSize() >= DEFAULT_CAPACITY) {// Do nothing can't have more than 16 on screen at once }else {// add the object at specific pointelements[size++] = e; } }

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org