How can I clear a 6x6 "table", so that anything in it is cleared?
(I made the clearbutton already with ActionListener...etc)
//other code above that creates window, below is the code that creates the table I need to clear
square = new JTextField[s][s];
for (int r=0; r!=s; r++) {
symbols[r] = new JTextField();
symbols[r].setBounds(35+r*35, 40, 30, 25);
win.add(symbols[r], 0);
for (int c=0; c!=s; c++) {
square[r][c] = new JTextField();
square[r][c].setBounds(15+c*35, 110+r*30, 30, 25);
win.add(square[r][c], 0);
}
}
win.repaint();
}
Loop over the array and and set each element to null. You can use the java.utils.Arrays utility class to make things cleaner/neater.
for( int i = 0; i < square.length; i++ )
Arrays.fill( square[i], null );
Here is one line solution:
Arrays.stream(square).forEach(x -> Arrays.fill(x, null));
Something like...
for (int index = 0; index < square.length; index++) {
square[index] = null;
}
square = null;
Will do more then the trick (in fact the last line would normally be enough)...
If you're really paranoid...
for (int index = 0; index < square.length; index++) {
for (int inner = 0; inner < square[index].length; inner++) {
square[index][inner] = null;
}
square[index] = null;
}
square = null;
Related
So I'm trying to get an array to be passed into a separate method and then return a new sized array but the program has just one incorrect value. For example, I have an array
int [] myInches = {89,12,33,7,72,42,76,49,69,85,61,23};
That I'm trying to pass into my createLowerArray method
public static int [] createLowerArray(int maxParam, int [] myInchesParam) {
int [] betterInches = {0,0,0,0,0,0,0,0,0,0};
int count = 0;
for (int i = 0; i < myInchesParam.length; i++) {
if (myInchesParam[i] < maxParam) {
count++;
}
betterInches = new int [count];
int newCount = 0;
for (int q = 0; q < betterInches.length; q++) {
if (myInchesParam[newCount] < maxParam) {
betterInches[q] = myInchesParam[newCount];
}
newCount++;
}
}
return betterInches;
}
With maxParam just being whatever the user inputs. So let's say they put in 40, the second method will see that only 4 elements, (12,33,7, and 23) are less than 40 and create an array of length 4 with position 0 being 12, [1] = 33, [2] = 7, and [3] = 23, but for some reason my program makes it so. Position 0 in the new array is 0, [1] = 12, [2] = 33, and [3] = 7. the length is correct but the values aren't in the right positions. I got help with this earlier and thought I had it, it feels bad to be back so fast but I just can't seem to figure it out. Thank you to anyone who helps. I know this can be made easier with lists, streams and the like but I need practice with loops.
Expected output should be
int length = 4
[0] = 12
[1] = 33
[2] = 7
[3] = 23
Current output is
int length = 4
[0] = 0
[1] = 12
[2] = 33
[3] = 7
The immediate problem is here:
for (int q = 0; q < betterInches.length; q++) {
if (myInchesParam[newCount] < maxParam) {
betterInches[q] = myInchesParam[newCount];
}
newCount++;
}
You are always incrementing newCount, even if you didn't copy the value. Also, you need to loop over myInchesParam, not betterInches:
for (int j = 0, q = 0; j < myInchesParam.length; j++) {
if (myInchesParam[j] < maxParam) {
betterInches[q] = myInchesParam[j];
q++;
}
}
Additionally, you are doing a lot more work than necessary - your current code is quadratic in the size of the input array. You create the new betterInches array on each iteration of the outer loop, and then discard that and create it again on the next iteration.
Move the inner loop out of the outer loop:
for (int i = 0; i < myInchesParam.length; i++) {
if (myInchesParam[i] < maxParam) {
count++;
}
}
betterInches = new int [count];
for (int i = 0, q = 0; i < myInchesParam.length; i++) {
if (myInchesParam[i] < maxParam) {
betterInches[q++] = myInchesParam[i];
}
}
Try this
for (int q = 0; q < betterInches.length; q++) {
if (myInchesParam[q] < maxParam) {
betterInches[newCount] = myInchesParam[q];
newCount++;
}
}
Updated
Actually you are not iterating on whole array for getting your params. changed second loop to this.
int newCount = 0;
for (int q = 0; q < myInchesParam.length; q++) {
if (myInchesParam[q] < maxParam) {
betterInches[newCount] = myInchesParam[q];
newCount++;
}
}
seprate first loop and second loop. your second loop is inside the first loop. take a look on code below.
public static int [] createLowerArray(int maxParam, int [] myInchesParam) {
int [] betterInches = {0,0,0,0,0,0,0,0,0,0};// Dont need to initialize
int count = 0;
for (int i = 0; i < myInchesParam.length; i++) {
if (myInchesParam[i] < maxParam) {
count++;
}
} // First loop end here
betterInches = new int [count];
int newCount = 0;
for (int q = 0; q < myInchesParam.length; q++) {
if (myInchesParam[q] < maxParam) {
betterInches[newCount] = myInchesParam[q];
newCount++;
}
} // Second Loop ends heer
return betterInches;
}
I have an array of buttons declared as a class variable that is added to a gridPane. When I try to remove the buttons of the array from the gridPane the event handler is being recognized but the node is not being removed.
Any ideas of how to accomplish this?
for(int i = 0; i < Rows; i++) {
for(int j = 0; j < Cols; j++) {
bArray = new Button[Rows][Cols];
bArray[i][j] = new Button();
bArray[i][j].setMinSize(20,20);
bArray[i][j].setMaxSize(25,25);
gridBoard.setVgap(1); //vertical gap in pixels
gridBoard.add(bArray[i][j], j, i);
bArray[i][j].setOnMouseClicked(e->checkNeighbors());
}
}
// Local Method
public static void checkNeighbors() {
// This print out statement is met, but the removal does not occur
System.out.println("Action is called");
gridBoard.getChildren().remove(bArray[0][1]);
gridBoard.getChildren().remove(bArray[0][2]);
gridBoard.getChildren().remove(bArray[0][3]);
}
You are overriding bArray in each iteration and the only valid references that are left at the end of the for loops are from [Rows][0] to [Rows][Cols].
As an example if Rows = 4 and Cols = 4, that would be that at the end the only valid references are [3][0], [3][1], [3][2] and [3][3], and you are trying to remove [0][1], [0][2], [0][3].
You should move the initialization of bArray before the loops start.
bArray = new Button[rows][cols];
for(int i = 0; i < rows; i++) {
for(int j = 0; j < cols; j++) {
bArray[i][j] = new Button();
// ......
}
I don't know, if I forgot how, or I just can't figure it out how.
For example :
Object[][] data = {
{"id", "projectname","valueid", "value"},
};
And this is how they should be added, but in loop:
Object[][] data = {
{"id", "projectname","valueid", "value"},
{"id2", "projectname2","valueid2", "value2"},
{"id3", "projectname3","valueid3", "value3"},
};
And so on..
I need a tip only, like a skeleton how it should be. I tried to figure it out, but had no idea how.
Thanks!
You can add a new array to another array like this :
data[1] = new Object[]{"id_1", "projectname_1","valueid_1", "value_1"};
...
data[n] = new Object[]{"id_n", "projectname_n","valueid_n", "value_n"};
You can use this way in any loop for example :
int length = 5;
Object[][] data = new Object[length][];
for(int i = 0; i < length; i++){
data[i] = new Object[]{...some information};
}
for (int i = 1; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
int line = i+1;
data[i][j] = data[0][j]+ line;
}
}
Is there a better way to insert an Element between each pair of elements into a List in Java than iterating through it
List<Integer> exampleInts = new ArrayList<>(Arrays.asList(1,2,3,5,8,13,21));
for (int i = 1; i < exampleInts.size(); i++) {
int delimiter = 0;
exampleInts.add(i, delimiter);
i++;
}
No, there is no ready utils for this in standard java libraries.
BTW, your loop is incorrect and will work infinitely until memory end. You should increment i variable one more time:
for (int i = 1; i < exampleInts.size(); i++) {
int delimiter = 0;
exampleInts.add(i, delimiter);
i++;
}
or change loop conditions to for (int i = 1; i < exampleInts.size(); i+=2) {
Try this solution it is working correctly.
List<Integer> exampleInts = new ArrayList<>(Arrays.asList(1, 2, 3, 5,
8, 13, 21));
int size = (exampleInts.size()-1)*2;
for (int i = 0; i < size; i+=2) {
int delimiter = 0;
exampleInts.add(i+1, delimiter);
}
System.out.println(exampleInts);
So I'm working with a two-dimensional array of JTextFields for a Sudoku program.
public JTextField[][] userInputArray = new JTextField[9][9];
Now. I'm getting a continual null pointer exception, and can't discern how to fix it. It comes from firing this method:
public void showTextFields()
{
int rowCounter = 0;
int columnCounter = 0;
for (rowCounter = 0; rowCounter <= 8; rowCounter += 1)
{
for (columnCounter = 0; columnCounter <= 8; columnCounter += 1)
{
pane.setLayout(new GridLayout(9, 9));
//pane.add(userInputArray[rowCounter][columnCounter]);
//userInputArray[rowCounter][columnCounter].setColumns(1);
//userInputArray[rowCounter][columnCounter].setVisible(true);
}
}
}
Everything commented out will throw the nullpointerexception.
Optimally, my goal is to display the JTextFields on screen, assigning them on the grid.
NullPointerException occurs at pane.add(...)
While you have created the 2D array to house your fields, you need to instantiate the JTextField components in your array.
for (int i =0; i < userInputArray.length; i++) {
for (int j =0; j < userInputArray[0].length; j++) {
userInputArray[i][j] = new JTextField();
}
}
When you declare array it is initialized with default values. Default value for Object is null so you need to instantiate the objects first before using them
userInputArray[rowCounter][columnCounter] = new JTextField();
So now your code should look like below
for (columnCounter = 0; columnCounter <= 8; columnCounter += 1)
{
pane.setLayout(new GridLayout(9, 9));
userInputArray[rowCounter][columnCounter] = new JTextField();
pane.add(userInputArray[rowCounter][columnCounter]);
userInputArray[rowCounter][columnCounter].setColumns(1);
userInputArray[rowCounter][columnCounter].setVisible(true);
}