I'm having some problems with Object arrays. I've made up my own object which only has an empty constructor, a constructor which initializes its two instance variables with paramaters and get/set methods.
I initiliaze the array like this:
private Bara[][] card = new Bara[3][4];
and I'm trying to fill in the object's variables like this:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
card[i][j].setName(name);
card[i][j].setSide(side);
}
}
Any help is appreciated.
Thanks!
private Bara[][] card = new Bara[3][4];
It just creates a two-d array for the referrences of Bara, but no Bara objects.
Thus, you need to create actual Bara Objects in your loop:
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
card[i][j] = new Bara(name, side); //If such constructor exists.
}
}
Related
The following code:
// Adds game button container
Object[][] gameButtons = new Object[3][3];
// Adds game buttons to game button container
Arrays.fill(gameButtons, new JButton[3][3]);
for (int i = 0; i < gameButtons.length; i++) {
for (int j = 0; j < gameButtons[i].length; j++) {
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
gameButtons[i][j][k][l] = new JButton();******
}
}
}
}
is throwing me an error:
The type of the expression must be an array type but it resolved to Object. How do I initialize the JButtons?
EDIT: I forgot to clarify. The error was thrown at the line ******
Note that the ****** was not in my code.
EDIT 2: I tried Logan's fix, but it still didn't work:
for (Object[] row : gameButtons)
Arrays.fill(row, new JButton[3][3]);
for (int i = 0; i < gameButtons.length; i++) {
for (int j = 0; j < gameButtons[i].length; j++) {
for (int k = 0; k < 3; k++) {
for (int l = 0; l < 3; l++) {
gameButtons[i][j][k][l] = new JButton();
}
}
}
}
Same error, same place.
gameButtons[i][j] is of type Object, which cannot be indexed as an array. You must first cast it to a JButton[][] type:
for (int i = 0; i < gameButtons.length; i++) {
for (int j = 0; j < gameButtons[i].length; j++) {
// cast it to an array type before accessing
JButton[][] subArray = (JButton[][])(gameButtons[i][j]);
for (int k = 0; k < 3; k++)
for (int l = 0; l < 3; l++)
subArray[k][l] = new JButton();
}
}
I believe you don't need a for loop. If I understand your problem correctly, you can do like this:
//create a sub array
Object[][] subArray = new Object[3][3];
// create game button container
Object[][] gameButtons = new Object[3][3];
// Adds buttons to sub array
Arrays.fill(subArray, new JButton[3][3]);
// Adds sub array to game button container
Arrays.fill(gameButtons, subArray);
The docs helped me a lot with this.
You could also use a GUI maker such as the one in NetBeans IDE it's much simpler than writing the code yourself.
I have to make a 2D array of JCheckBoxes in Java. I'm using the code below, but when I try to set selected true:
checks[0][1].setSelected(true);
it says that checks[0][1] is null.
JCheckBox[][] checks = new JCheckBox[14][14];
for (int i = 0; i < 14; i++) {
for (int j = 0; j < 14; j++)
this.add(new JCheckBox(""));
You have to create each JCheckBox in the array and then add it. I'd also suggest using named constants instead of magic numbers, like so:
final int NUM_BOXES = 14; // named constant
JCheckBox[][] checks = new JCheckBox[NUM_BOXES][NUM_BOXES];
for (int i = 0; i < NUM_BOXES; i++) {
for (int j = 0; j < NUM_BOXES; j++)
checks[i][j] = new JCheckBox("");
this.add(checks[i][j]);
}
}
int count = 0;
for (int i = 0; i < width - 1; i++)
{
for(int j = 0; j < height - 1; j++)
{
new HashSet();
count++;
}
}
I'm creating sets inside of two for loops, but how do I obtain reference to the sets? How would I be able to call "set1" or "set2" for example?
You can try to put them in an Arraylist.
int count = 0;
List<Set<YourClass>> arr = new ArrayList<Set<YourClass>>();
for (int i = 0; i < width - 1; i++)
{
for(int j = 0; j < height - 1; j++)
{
Set s = new HashSet<YourClass>();
arr.add(s);
count++;
}
}
Then you can call whichever one you want:
Set theFirstSet = arr.get(0);
And do things with them:
theFirstSet.add(your_class_instance);
The sets need a name e.g.
HashSet set1;
make them fields at class level so can refer anywhere.
set1 = new HashSet();
I'm confused on how to make a java method to append two different 2D arrays without using/importing the arrays class. This is what I have so far:
private Cell[][] appendArrays(Cell[][] first, Cell[][] second) {
Cell[][] third = new Cell[first.length][first[0].length + second[0].length];
for (int i = 0; i < first.length; i++) {
for (int j = 0; j < first[i].length; j++) {
third[i][j] = first[i][j];
}
}
for (int i = 0; i < second.length; i++) {
for (int j = 0; j < second[i].length; j++) {
// don't know what to do here
}
}
return third;
}
A Cell is just an object, but I'm trying to understand the logic behind appending arrays so any help would be appreciated!
Also, I know there is a similar question found here (How do you append two 2D array in java properly?), but the answer is given on appending arrays from the top down (given both parameter arrays have the same amount of columns), while I am looking for appending arrays from side to side (assuming both parameter arrays have the same amount of rows).
You're almost there. I think something like this is what you're looking for.
private Cell[][] appendArrays(Cell[][] first, Cell[][] second) {
Cell[][] third = new Cell[first.length][first[0].length + second[0].length];
for (int i = 0; i < first.length; i++) {
for (int j = 0; j < first[i].length; j++) {
third[i][j] = first[i][j];
}
for (int j = first[i].length; j < first[i].length + second[i].length; j++) {
third[i][j] = second[i][j-first[i].length];
}
}
return third;
}
I am trying to put the values (integers) from the 2 dimensional array prices[][] into the cost variable of the objects in the array seatArray[][]. I think the problem is that I am trying to put the values from the prices array into nothing because the seatArray array is only full of object references to null. How would I go about fixing this?
line that calls constructor:
SeatChart seatArray = new SeatChart(givenArray);
constructor method:
public SeatChart(int[][] prices)
{
Seat[][] seatArray = new Seat[9][10];
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 10; j++)
{
seatArray[i][j].cost=prices[i][j];
}
}
}
Seat[][] seatArray = new Seat[9][10];
This just declares the array and doesn't initialize the array elements with Seat objects.
for(int i = 0; i < 9; i++)
{
for(int j = 0; j < 10; j++)
{
// I've used a default Seat() constructor to create the object, in your actual case, it may differ.
seatArray[i][j] = new Seat(); // Initializing each array element with a new Seat object
seatArray[i][j].cost=prices[i][j];
}
}
seatArray[i][j] = new Seat();
seatArray[i][j].cost= prices[i][j];
Or maybe for clarity
Seat seat = new Seat();
seat.setCost(prices[i][j]);
seatArray[i][j] = seat;