Looping through ArrayList using enums?

I’m having trouble creating a method which loops through an ArrayList of multiple types of objects.

Here’s my failed attempt:

import java.util.ArrayList;

public class DebugObjects {

	public enum Block {
		Solid, Bad, Bounce
	}

	public static void debug(ArrayList<Block> list) {

		for (Block b : list) {

			String n = b.getClass().getName();
			byte i = (byte) Solid.list.indexOf(b);

			if (b.X < 0) {
				System.out.println(n + " " + i + " is outside the left side.");
			}
			if (b.X + b.W > Game.WH) {
				System.out.println(n + " " + i + " is outside the right side.");
			}
			if (b.Y < 0) {
				System.out.println(n + " " + i + " is outside the up side.");
			}
			if (b.Y + b.H > Game.WH) {
				System.out.println(n + " " + i + " is outside the down side.");
			}

		}

	}

}

My goal with this is to be able to call the method like this:
DebugObjects.debug(Solid.list);
This would loop through all Solid objects inside of Solid.list and tell me if any of them exceed the game window.

Thanks in advance!

What issue are you having with your current code? I can see that you are doing b.X and b.Y when the Block enum has no such fields.

Yes, indeed. Also, why make a debug() method? The whole point of debugging is finding the error in some piece of code that your game uses. If it’s in the player class, debug it there. If in the game loop, debug it there. Or even better use a debugger.

Also, your enum code isn’t valid. Should be a semicolon after Bounce.

Your example code is not complete, wouldn’t compile. So I can only throw some random thought at you.
When you create one enum you also create only ONE class. Solid, Bad and Bounce in your example are NOT classes they are simple instances of the Block class.

When you want to print a enum instance you can use the .name() method or just rely on the toString method, like:


println("10 " + Block)
//prints "10 Block"

My goal is not exactly debugging but the name has nothing to do with it. Plus, according to pretty much all my Google searches, the semicolon is optional when using enums.

I don’t know if I misunderstood but Solid, Bad & Bounce are all classes in my game. The printing is not what I want help with, what I need help with is what I actually wrote in the post.

You have not yet defined what exactly is the problem You are having ?

The problem we have with your example is, that the code you show us doesn’t make sense.

You define a Block enum with a Solid INSTANCE, but also use Solid.list. This doesn’t make sense, because the Block enum does not have a “list” field. Is it possible that you also created classes which have the same name as the enum instances?

Perhaps does the following help, your List does NOT hold “multiple types of objects.” it does only hold Block objects. You can switch over enums, so you can give the following a try.


for(Block b: blockList)
{
   switch(b){
     case Solid: foo(); break;
     case Bad: bar(); break;
     case Bounce: zomfg(); break;
  }
}

Technically, it’s not needed. Only if you add methods after the constants.

Ah, my bad. Didn’t know that.