Java ArrayList in ArrayList - java

I have a question about an ArrayList inside an Arraylist. It's about multiple worlds with multiple spawns. I want to check every world one by one and save all the spawns of that world in an ArrayList. At the end I have an ArrayList with on every position (every position is a world) another Arraylist containing the spawns of that world.
How can I do this if I don't know how many spawns or worlds there are going to be? I thought of this:
Looking to just one world:
public ArrayList<Location> Spawnpoints = new ArrayList<Location>();
//ArrayList containing all the spawns of this world).
Looking to every single spawn in the world
Spawnpoints.add(new Location(spawnX , spawnY , spawnZ));
//Adding every spawn to the ArrayList spawnpoints.
So after I looked at a world I have filled the ArrayList spawnpoints with locations. Now I want to add the ArrayList spawnpoints to a new ArrayList worlds.
After that I will repeat the code above for the next world, untill I have had all the worlds.
EDIT.
I think it's working. I have troubles with getting the size of the list when I only have the name.
So let's say I did this: allSpawnpoints.put("yourWorld",Spawnpoints);
Now I want to get the size of the Spawnpoints list for the string yourWorld. Any idea how I can do this?
I tried: int number = allSpawnpoints.get("yourWorld").size();
it looks like that isn't working.
I hope someone can help me! Thanks for reading.
Regards.

Instead of an ArrayList or World objects try using a Map of World to List of Location. Something like this:
Map<World,List<Location>> allSpawnpoints = new HashMap<World,List<Location>>();
Then, after you have created your list of spawnpoints for a World put it into your Map like this:
allSpawnpoints.put(yourWorld,Spawnpoints);
... and then move onto the next World.
I would also suggest renaming Spawnpoints as spawnPoints.

Related

How can I create Shuffle play (with GUI) in java

I have to create an application for audio stream with different functionalities. I am stuck with the shuffle play action. Because on my previous project were GUI wasnt included I created this method where I created a new playlist with a different order.
But now where I have the GUI part and I need to show the playlist in a JList I cant change the original playlist. How am I supposed to get random songs when I press Next without changing the original playlist? Thank you.
public ArrayList<String> playRandom(ArrayList<String> songsPath, Player p) {
ArrayList<String> newPlaylist = new ArrayList<>();
while(songsPath.size()>0) {
String song = songsPath.get(randomIndex(songsPath.size()));
songsPath.remove(song);
newPlaylist.add(song);
}
return newPlaylist;
}
That's what I used to do but it changes the original playlist which is a problem if someone wants to go back to playing the songs in the original row.

How to move/iterate forward to next value in Array (java/android)

Spent the last 1-2 hours looking for the answer and haven't quite got it, I'm hoping it's something simple.
I have created an array of a custom class with just a small number of values like so (where Person is a Class):
Person person[] = new Person[2];
(Note I'll be replacing the 2 with a variable like numberOfPeople)
I need to create a method that I can call when needed (on a button press for example) that will move from the current person to the next in the array (going back to the start when it reaches the end).
In other words: On button press - move from player0 to player1.
I've been looking at how to use For, ForEach, If etc to do this but can't work it out.
SOLVED VIA Vincent Ugenti's ANSWER.
In case other noobs come across this to solve their problem, had a few subsequent noob issues where I was trying to define the number of values in the array with a variable which wasn't given a value until after the array was made (causing instant crashing) and then I forgot doing nothing more with the code than this won't run each objects Constructor which needs to be done separately.
Create a variable such as "currentPerson"
In your event handler (whenever you want to advance to the next person), currentPerson = (currentPerson + 1) % numberOfPeople
The current person is then always person[currentPerson]
Keeping track of the current index and hooking a button event is what you want to do.
On button press check your variable, increment it by 1 and check if that value is not greater than the length of the array.
If its not greater than the array length you can grab the element, else reset the count to 0 and do what you need to do.
If youre only doing this on button press (event handler) you shouldnt need to use any loops.

Placing one actor on top of another java

I am creating a program that is a recreation of the old game Frogger. A key part of the game is when the frog jumps across logs and to the other side of a river. However, when I make the frog move from the road to a log, the frog is positioned under the image of log instead of on top of the log. How can I make it so that the frog image lies above the log image?
I don't have an onDraw() method, at least I didn't write one. This is for an assignment in my class and we use a program called Greenfoot4Sofia. I created a subclass of Actor called Log, and I just used an option on the program to set the image of the actor to an image of a log, similarly with the frog.
Greenfoot is obviously not set up to make this sort of thing easy, but is intended for new programmers... curious.
So, I'm not familiar with Greenfoot, but looking over the API docs, I notice that World objects have a setPaintOrder(Class...) method. Perhaps you can use that? If you haven't already, I would make the log its own class that extends Actor, i.e.-
public class Log extends Actor {
// whatever code you have for your log
}
and do the same for your frogger.
Then (maybe) you can call the setPaintOrder() method of your World, i.e.-
myWorld.setPaintOrder( Log.class, Frogger.class );
This is obviously not the kind of code you can copy/paste, but hopefully it will get you on the right path. There will probably be other things you need to draw as well, so you can just create an array of Class to pass to setPaintOrder:
ArrayList<Class> paintOrderList = new ArrayList<Class>();
// add the things you need in the order you need them to paintOrderList
Class[] paintOrder = paintOrderList.toArray( new Class[ paintOrderList.size() ] );
myWorld.setPaintOrder( paintOrder );
I hope that helps.

How can I combine multiple ACM graphics objects in Java into one object?

I have finished writing a Hangman game, but I want to move the hangman out of the canvas when the game is over. I create that hangman with any partition of his body. When I move the object it can move only one object at a time. How can I bunch them together?
You have to create an object of the class GCompound. This class of object allows you to create new object that can be manipulated like GOval and so. In the Stanford course, there is an example called GFace.
Probably you can refactor your code to make the whole hangman one object through the whole implementation and whenever needed make different parts visible. When the time comes to remove him, just dispose of the whole thing either by resetting them to non-visible or try making a new object I guess... If you cna post the code of your implementation I may be able to give you some more help...

Clearing a Double Array (I think I am doing it wrong)

Basically, I am building my own pokemon game, however whenever I go into a new "region" the tiles should reset and refresh, however, instead of clearing the old ones out, they just add onto the existing ones, thus causing a lot of issues.
I know that once I can successfully clear the old tiles, the game will refresh nice and neat for me.
I thought all i had to do was
buttonPanels = new JButton[row][col]
to make a new (clean) instance of the button array?
The whole board is a just a JButton array.
Two images below, one of before and one of after I go into a new region. You can see how how in the after photo all the new tiles just add onto the existing ones, which causes a lot of problems.
http://i421.photobucket.com/albums/pp296/rskom/before.png
http://i421.photobucket.com/albums/pp296/rskom/after.png
THANK YOU!! :)
First time at trying a rpg type thing, so don't be too critical of it so far.
Without seeing more of your code it is hard to answer your problem, but I think I know what the problem is. You show code where you do create a new array of JButton references. However, this does not destroy or remove the buttons you had in the previous array. If the buttons were visible somewhere (usually the case for having buttons) then they should first be removed from that container. This is probably the sequence you want if won want to replace all buttons.
For each button that is referenced from your array, remove it from the container where it is visible. Then you can replace your array, fill it with new buttons and finally add the buttons to the container where they should be visible again.
But then the real question is if you really do need to create a new array with new buttons. Can you not just reuse your existing one?

Categories