Hello!
I’m currently builing a game where you have to construct a building. This building has to be contructed by using different types of bricks with different colours etc. Since it’s only the start of the design-phase, I am currently trying to let everything work with only text.
Right now I’m trying to print an array of all available building-bricks. However, when I try print this, the code is printed like [mygame.Bricks@6676284a, mygame.Bricks@100d1e93]
. I’ve heard about Arrays.toString
, but because this is an object, I cannot seem to find a way to get the right command.
Even better, I would like to print a specific variable that is applied to each of there bricks.
Summarizing: I would like to know if there is a way to print a single characteristic from an array with objects with multiple characteristics.
Here is the code that I’m using (I’m pretty new to Java and jME3 so there might be some horrible code right here)
public class Bricks {
protected Integer ID;
protected String name;
protected String colour;
protected Integer length;
protected Integer width;
protected Bricks(Integer brickID, String brickName, String brickColour,
Integer brickLength, Integer brickWidth) {
ID = brickID;
name = brickName;
colour = brickColour;
length = brickLength;
width = brickWidth;
}
protected Integer getID() {
return ID;
}
protected String getName() {
return name;
}
protected String getColour() {
return colour;
}
protected Integer getLength() {
return length;
}
protected Integer getWidth() {
return width;
}
protected static void initBricks() {
ArrayList<Bricks> AvailableBricks = new ArrayList<>();
Bricks redBrick1 = new Bricks(1,"Red brick with 4x2 dimensions","Red",4,2);
AvailableBricks.add(redBrick1);
Bricks redBrick2 = new Bricks(2,"Red brick with 3x2 dimensions","Red",3,2);
AvailableBricks.add(redBrick2);
// This needs a lot of more bricks, but I haven't added them yet.
// This gives the [mygame.Bricks@6676284a, mygame.Bricks@100d1e93] code,
// but I want it to print the String name of each 'Bricks'.
// Like: [Red brick with 4x2 dimensions, Red brick with 3x2 dimensions]
Bricks[] availableBricks = AvailableBricks.toArray(new Bricks[AvailableBricks.size()]);
System.out.println(availableBricks);
}
}
Is this possible?
Thanks in advance!