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]);
}
}
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.
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 sure the answer is fairly simple, but I'm not getting it. Here we go with my example:
int matrix [][] = new int [rows][columns];
for (int i = 0; i < matrix.length; i++)
{
for(int j = 0; j < matrix[0].length; j++)
{
mata[i][j] = Integer.parseInt (args[j]);
}
}
How can I make j count further upwards after the programm goes from the inner loop to the outer loop and back to the inner one? Usually, it would start from zero again, which is not intended, as I need the next command line argument. I tried a few things, can't get it to work, though.
You can add an outer variable:
int k = 0;
then replace the innermost line by:
mata[i][j] = Integer.parseInt(args[k]);
k += 1;
Or compute it:
mata[i][j] = Integer.parseInt(args[i * matrix.length + j]);
Just use a separate variable (argc below):
int matrix[][] = new int[rows][columns];
for (int i = 0, argc = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++, argc++) {
mata[i][j] = Integer.parseInt(args[argc]);
}
}
I want to make a loop on Two-dimensional array in Java.
How I do that? I wrote:
for (int i = 0; i<=albums.size() - 1; i++){
for (int j = 0; j<=albums.size() - 1; j++){
But it didn't work. Thanks.
Arrays have a read-only field called length, not a method called size. A corrected loop looks like this:
for(int i = 0; i < albums.length; i++ ) {
for (int j = 0; j < albums[i].length; j++) {
element = albums[i][j];
You have to recognize that a 2-D array is just an array whose element type happens to be another array. So the i loop iterates over each element in albums (which is an array) and the j loop iterates over that child array (with a potentially different size).
A more transparent way would be like this:
String[][] albums;
for(int i = 0; i < albums.length; i++ ) {
String[] childArrayAtI = albums[i];
for (int j = 0; j < childArrayAtI.length; j++) {
String element = childArrayAtI[j];
}
}
Try this if you are working with Java 1.5+:
for(int [] album : albums) {
for(int albumNo : album) {
System.out.print(albumNo + ", ");
}
System.out.println();
}
First of all, a two-dimensional array looks like this in Java:
int[][] albums = new int[10][10];
Now, for iterating over it:
for (int i = 0; i < albums.length; i++) {
for (int j = 0; j < albums[i].length; j++) {
int value = albums[i][j];
}
}
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.
}
}