[Solved] Using ArrayList<> adding and removing objects

Have you looked at my solution? It isn’t perfect, but I think it’ll work fine or at least inspire some ideas for you:
https://github.com/JeremyWildsmith/JevaEngineSrc/blob/master/JevaCore/src/jeva/util/StaticSet.java

I get where you’re going with your solution but I don’t think it’s a very good general solution.

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.)

How are for-each loops implemented without iterators D: ? Man that is really awkward… Maybe try Enumeration: http://www.blackberry.com/developers/docs/5.0.0api/java/util/Enumeration.html

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)

Yeah but its the library im most comfortable with, also gives me a market.

Here would this “list” help me at all?
http://www.blackberry.com/developers/docs/4.5.0api/net/rim/device/api/collection/List.html

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.

Now i just need to try and find away to clone it :confused:

Everything seems to be working okay-ish.

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.

ArrayList
http://pastebin.java-gaming.org/b4d17773178

Projectile
http://pastebin.java-gaming.org/4d17781387c

PlayerRocket ( extends projectile )
http://pastebin.java-gaming.org/d1771983c73

The Methods
http://pastebin.java-gaming.org/177180c4371

My error is

			elements[size++] = e;

in the

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

of the projectile.

and it is telling me

size = 2

Hm, i need re read question :wink:
this help?


	for(int i = 0; i < Bullets.size(); i++){
		Bullet bu = Bullets.get(i);	
		bu.blalbalball();
		
		if(bu.isDead()){
			Bullets.remove(i);
			i--;
		}
	}

That doesn’t appear to work either, I am receiving the same problem.

If i try and reuse the rockets, everything is good, up until i get a different error. TooManyThreads. How exactly did i manage to get this o.O? How do I go about approaching it? I google it and it doesnt help me much, just some people posting on forums asking how many threads is too many

Well, do you put your rocket objects on seperate threads?

No, all my rockets are on the one thread, and that thread is basically the game loop

Doesnt matter. I dungoofed. I was initalising a timer that wasnt being used