I just wrote the code below and thought is there any way to do the same without using the nesting loops and how clean and efficient will it be?
for (int y = Y_LEVEL_START; y <= Y_LEVEL_STOP; y++) {
for (int z = 0; z < CHUNK_SIDE; z++) {
for (int x = 0; x < CHUNK_SIDE; x++) {
if (chunk.getBlock(x, y ,z).getType() == ANCIENT_DEBRIS)
report.debrisFoundAt(x, y, z);
}
}
}
Related
double numbers[][];
numbers = new double[22][9];
for(int x = 0; x<22; x++) {
for(int y = 0; y <9; y++)
{
numbers[x][y] = (int)(Math.random()*192)+1;
System.out.print(numbers[x][y]+ "");
System.out.println();
}
Trying to display the array within a table/index but when I do, it just display the random numbers vertically. Idk how to fix it. Sorry for the nooby code.. :(
In java two-dimensional array in java is just an array of array, and a small mistake while iterating it. Add System.out.println(); in outer for loop
for(int x = 0; x< 22; x++) { // for every array in outer array
for(int y = 0; y < 9; y++) { //for every double in each inner array
numbers[x][y] = (int)(Math.random()*192)+1;
System.out.print(numbers[x][y]+ " ");
}
System.out.println();
}
If you separate construction and display, it may be clearer:
double numbers[][] = new double[22][9];
// construction
for(int x = 0; x<22; x++)
for(int y = 0; y <9; y++)
numbers[x][y] = (int)(Math.random()*192)+1;
// display
for(int x = 0; x<22; x++){
for(int y = 0; y <9; y++)
System.out.print(numbers[x][y]+ "\t");
System.out.println("");
}
I have created a for loop to produce a grid map. When I click each grid on the map I get X and Y of the grid. When map width is greater than map length, everything is fine, but when attempt to create a map where length is greater than with, the returned x becomes the y, and y becomes the x. The issue is at the second for loop when creating the map but I cannot figure it out.
if(mapWidth>mapLength) {
for (int i = 0; i < mapWidth * mapLength; i++) {
y = i / mapLength;
for(int j=0; j<i+1; j++) {
x = j % mapLength;
}
GridPanel gb = new GridPanel(x, y);
list.add(gb);
mapPanel.add(gb);
}
} else if(mapWidth<mapLength) { //problematic map is created after this condition
for (int i = 0; i < mapWidth * mapLength; i++) {
x = i / mapLength;
for(int j=0; j < i+1; j++){
y = j % mapLength;
}
GridPanel gb = new GridPanel(x, y);
list.add(gb);
mapPanel.add(gb);
}
}
Maps look like this:
Well, maybe I didn't understand well what you expect but I don't think you have to make a special case for the case where mapWidth< mapLength.
Furthermore, I don't get what you intend to do with your nested loop except using CPU resources?
for(int j=0; j<i+1; j++){
x = j % mapLength;
}
When it is left, you will always have x = i % mapLength
Furthermore, as flkes suggested, why don't you use nested loops?
for (int y=0; y < mapLength; y++) {
for(int x=0; x < mapWidth; x++){
GridPanel gb = new GridPanel(x, y);
list.add(gb);
mapPanel.add(gb);
}
}
Could you try this code:
for (int i = 0; i < mapWidth * mapLength; i++) {
y = i / mapLength;
x = i % mapLength;
GridPanel gb = new GridPanel(x, y);
list.add(gb);
mapPanel.add(gb);
}
I have a 2d array called tiles[x][y] which goes till 9 so has 100 things inside of it.
How can I get another array and put everything from the 2d array into the normal array?
int counter = 0;
for (int x = 0; x < mapWidth; x++) {
for (int y = 0; y < mapHeight; y++) {
tiles[y][x] = new loopVak(Color.WHITE, x*tileWidth, y*tileHeight);
}
}
This is how the 2d array is made, mapwidth and mapheight is 10.
If you want to convert tiles to a new 1D Array then you can simply do something like this:
int k = 0, newArray[] = new loopVak[100];
for(int i = 0; i < mapWidth; i++) {
for(int j = 0; j < mapHeight; j++) {
newArray[k++] = tiles[i][j];
}
}
If you do not want the 2D array in the first place then you can do something like this:
int counter = 0, newArray[] = new loopVak[100];
for(int x = 0; x < mapWidth; x++) {
for(int y = 0; y < mapHeight; y++) {
newArray[counter++] = new loopVak(Color.WHITE, x * tileWidth, y * tileHeight);
}
}
I have an array that I'm trying to blur using a kernel, but the loop doesn't finish for some reason, here's the code:
for (int x = 0; x < 128; x++) {
for (int y = 0; y < 128; y++) {
for (int kx = -2; x <= 6; x++) {
for (int ky = -2; y <= 6; y++) {
nlm2[x][y] += 100 * (int) ((float) nlm[x][y]*(float)kernel[(kx+3)*(ky+3)-1]);
System.out.println(x+" "+y);
System.out.println(kx+" NLM: "+(float) nlm[x][y]);
System.out.println(ky+" Kernel: "+(float)kernel[(kx+3)*(ky+3)-1]);
}
}
}
}
It seems to stop after
x = 0; y = 6
kx = ky = -2
There are no errors in the console, and it shows another print screen after this little loop-de-loop.
This lines might be the problem.-
for (int kx = -2; x <= 6; x++) {
for (int ky = -2; y <= 6; y++) {
You're incrementing x and y respectively, instead of kx and ky.
I'm guessing you really meant.-
for (int kx = -2; kx <= 6; kx ++) {
for (int ky = -2; ky <= 6; ky ++) {
I wrote this method for my java minesweeper game, it is supposed to check spots surrounding a set of coordinates and then calculate how many bombs are near by.
public void numberMines(){
int count = 0;
int x = 0;
int y = 0;
int xMin = x-1;
int xMax = x+1;
int yMin = y-1;
int yMax = y+1;
if (x == 0){
xMin = 0;
}
if (y == 0){
yMin = 0; //these restrictions take care of the spots in the edges
}
if (x == rows){
xMax = rows;
}
if (y == columns){
yMax = columns;
}
//first 2 loops go through every spot on the board
for (x=0; x<rows; x++){
for (y=0; y<columns; y++){
//if the spot selected is not a bomb, for loops check spaces surrounding it
if (mineBoard[x][y] != bomb){
for (int i = xMin; i <=xMax; i++){
for (int j = yMin; j <=yMax; j++){
if (mineBoard[i][j] == bomb){
count++;
}
}
}
}
if (count > 0){ //converts them characters
mineBoard[x][y] = (char)(count + '0');
count = 0;
}
}
}
}
Every time I run this method it returns 3,2,1, or empty so it does count how many bombs are around, but for some reason it is over looping and returning the same thing for every spot that is not a bomb after the first one. I really cant see where I messed up, please help!
Move this block of code:
int xMin = x-1;
int xMax = x+1;
int yMin = y-1;
int yMax = y+1;
if (x == 0){
xMin = 0;
}
if (y == 0){
yMin = 0; //these restrictions take care of the spots in the edges
}
if (x == rows){
xMax = rows;
}
if (y == columns){
yMax = columns;
}
Inside of your for loops:
for (x=0; x<rows; x++){
for (y=0; y<columns; y++){
//Insert code here <---
Because at the moment, you're doing these calculations once, for x=0, y=0.
The code would probably also look cleaner if you moved the setting of count to 0 just before the i,j, loops, and not having it done once before all loops start, and again inside the conditional that displays the result.
Based on your comment - I think your valid indexes range from 0..(rows-1) and 0..(columns-1) - so you have a fencepost error also. Modify these lines:
if (x == rows-1){
xMax = rows-1;
}
if (y == columns-1){
yMax = columns-1;
}
But still have this entire block inside of your x/y loops. You don't get the out of bounds error when they're outside because you never calculate xMax and yMax when x and y are at their maximum values.
Avoid to declare all variables at the beginning of a method, better declare them close to when they are used. To fix your problem, you need to compute count, xMin, xMax, yMin and yMax in the loops like this:
public void numberMines(){
//first 2 loops go through every spot on the board
for (int x=0; x<rows; x++){
for (int y=0; y<columns; y++){
int count = 0;
//if the spot selected is not a bomb, for loops check spaces surrounding it
if (mineBoard[x][y] != bomb){
for (int i = (x == 0 ? 0 : x-1); i <= (x == rows ? rows : x+1); i++){
for (int j = (y == 0 ? 0 : y-1); j <= (y == rows ? rows : y+1); j++){
if (mineBoard[i][j] == bomb){
count++;
}
}
}
}
if (count > 0){ //converts them characters
mineBoard[x][y] = (char)(count + '0');
}
}
}
}
I have inlined the boundary checks, which is not necessary, but makes the code shorter to present here.