I really new to java and I've Googled this in every possible phrase i know how.
So I have a table made of 36 rows and 12 columns, I've been trying to write a method that will delete the a row when it becomes full and then move everything down one, I figured I could use a count to see if all spaces add up to 12 then delete the contents but it seems to delete randomly or not at all, can anyone help a java novice
int count = 0;
for (int i = 0; i < 36; i++){
for (int j = 0; j < 12; j++){
if (table[i][j] != null){
count++;
}
if (count == 12){
table[i][j] = null;
}
}
}
EDIT: hmm ive tried all the suggested answers none of them seem to work, what im trying to do it and out put like this
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| 1 . . 3 . . . . 5 . . . | < this line should take its place
| a b c d e f g h i j k l | < this line should delete
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| x y . r f s . . . . . . | < this line should move down one
| 1 2 3 4 5 6 7 8 9 0 . . | < this line should move down one
| A B C D E F G H I J K L | < this line should delete
| . . . . . . . . . . . . |
and output below
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| 1 . . 3 . . . . 5 . . . | < this line just moved down
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| . . . . . . . . . . . . |
| x y . r f s . . . . . . | < this line just moved down one
| 1 2 3 4 5 6 7 8 9 0 . . | < this line just moved down one
| . . . . . . . . . . . . |
I've gotten the output to all work but deleting full rows doesn't work
Use below code to remove entire row and you need to initialize your count
variable inside of first for loop.
for(int i = 0; i < 36; i++)
{
count = 0;
//your procession logic
}
if (count == 12)
{
table[i]= null;// this will delete/nullify row
}
You code
if (count == 12){
table[i][j] = null; // this will delete/nullify element j of i only.
}
Your count
variable is initialized to zero outside the for loops, making count == 12
true whenever it has counted 12 elements in total, and not 12 elements in the same row. Not sure if this is the desired behavior. If it should be true only if there are 12 elements in the same row, you should place count = 0
at the beginning of the outer for -loop.
Also, when count == 12
is true, you set the (i,j) entry to null
and not the whole row. I think you code should look something like:
int count;
for(int i = 0; i < 36; i++)
{
count = 0;
for(int j = 0; j < 12; j++)
{
if(table[i][j] != null)
{
count++;
}
if(count == 12)
{
table[i] = null;
}
}
}
Try this:
int count = 0;
for (int i = 0; i < 36; i++){
for (int j = 0; j < 12; j++){
if (table[i][j] != null){
count++;
}
if (count == 12){
if (i<=35)
{
//Initialize temp to a array like TableType[] temp = new TableType[12];
// move i to i+1 row
table[i+1] = table[i];
table[i] = temp;
}
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.