Creating a 2D Array and filling the array with random numbers - java

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("");
}

Related

Iterate throught 3D space without nesting loops

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);
}
}
}

Merging 2 Array and Removing Duplicate inputs

im having a problem on how to remove duplicate value on an array,can anyone help me?
I have this code made to just merge 2 arrays and displaying the result but i dont know how to remove the duplicate inputs. (im sorry i just started learning java.)
public static void main(String... args) {
int[] x = new int[3];
int[] y = new int[3];
int[] xy = new int[6];/***new array to hold the integers of arrays x and y ****/
int temp, c = 0;
Scanner myInput = new Scanner(System.in);
/*** input of array x at the same time storing the integers to array xy ***/
System.out.println("enter 3 integers for array x:");
for (int a = 0; a < 3; a++) {
x[a] = myInput.nextInt();
xy[c] = x[a];
c++;
}
/*** input of array y at the same time storing the integers to array xy ***/
System.out.println("enter 3 integers for array y:");
for (int b = 0; b < 3; b++) {
y[b] = myInput.nextInt();
xy[c] = y[b];
c++;
}
/*sorting...*/
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 5 - i; j++) {
if (xy[j] > xy[j + 1]) {
temp = xy[j];
xy[j] = xy[j + 1];
xy[j + 1] = temp;
}
}
}
/*printing of array xy sorted*/
for (int w = 0; w < 6; w++)
System.out.print(xy[w] + " ");
}
Since you have sorted your merged array a simplified solution is to not remove the duplicate but instead filter them out when printing by comparing the current value in the loop to the previous
System.out.print(xy[0] + " ");
for (int w = 1; w < 6; w++) {
if (xy[w] != xy[w - 1]) {
System.out.print(xy[w] + " ");
}
}

using for loop to create gridmap

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);
}

put everything from a 2d array into a normal array

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);
}
}

Out of Bounds Error in 2D Array with Dice Program

So I am creating a program that rolls z die x times with y sides, and I keep getting an out of bounds error at the first line in the first for loop. However I'm not sure why this is, the loop counts from 0 to (z-1). I'm basically in the home stretch of this program and I need the help of the stackoverflow community.
public class Ass11f {
public static void main(String[] args) {
EasyReader console = new EasyReader();
System.out.print("Enter how many times you want to roll the die: ");
int x = console.readInt();
System.out.print("Enter the amount of sides: ");
int y = console.readInt();
System.out.print("Enter the amount of die: ");
int z = console.readInt();
int[][] dice = new int[x][z];
int row = 0;
for (int i = 0; i<z; ++i){
dice[row][i] += ((int)(Math.random()*y)+1);
if ((i == z-1)&&(row!=x)) {
i = 0;
++row;
}
}
row = 0;
int[] sum = new int[x];
for (int j = 0; j<z; ++j){
sum[row]+=dice[j][row];
if ((j == z-1)&&(row!=x)) {
j = 0;
++row;
}
}
int[] counter = new int[2*y];
int k = 0;
while (k<sum.length){
for (int l = 0;l<((2*y)-1);++l){
if (sum[k]==l) ++counter[l];
if (l==((2*y)-1)) {
++k;
}
}
}
for (int m = 0;m<sum.length;++m) System.out.println(sum[m]+"'s: "+counter[m]+"times, "+((((double)counter[m])/x)*100)+"%");
}
}
first loop:
for (int i = 0; i<z; i++){
dice[row][i] += ((int)(Math.random()*y)+1);
if ((i == z-1)&&(row!=x-1)) {
i = -1;
++row;
}
}
second loop:
for (int j = 0; j<z; j++){
sum[row]+=dice[j][row];
if ((j == z-1)&&(row!=x-1)) {
j = -1;
++row;
}
}
Third loop: runs forever. I'm not sure what this is trying to achieve, so I can't fix it for you...
There are x rows but you are using z as the row loop
int[][] dice = new int[x][z]; <-- x is the row count
int row = 0;
for (int i = 0; i < z; ++i){ <--- The outer loop is iterating the rows (x),
Here's how to iterate through a 2D array
int[][] dice = new int[x][z];
for (int i = 0; i < x; i++){
for (int j = 0; j < z; j++){
// do something with dice[i][j]
}
}

Categories

Resources