I am currently starting understanging Java. So while I've been trying to develop some minesweeper application I noticed, that when trying to add coordinates to a "Mines[]" array, the debugging window opens and my application doesn't continue displaying the purposed Minefield.
So that's my code:
package com.ochs.minesweeper;
public class MineField {
public Mine[] mines;
public MineField(int xMines, int yMines) {
mines = new Mine[xMines*yMines];
int xCounter = 0;
int yCounter = 0;
for(int i = 0; i < yMines; i++) {
for(int j = 0; j < xMines; j++) {
mines[i*j].setX(xCounter);
mines[i*j].setY(yCounter);
xCounter += 100;
}yCounter += 100;
}
}
}
Even when I just try something like:
for(int i = 0; i < xMines*yMines; i++) {
mines[i].setX(2);
}
or something like that it seems like I can't use the variable of the for-loop in my array to process...
Does anyone has an idea what I am doing wrong? I just want my MineField to have it's Mine[] array. This Mines are all created in the for loop with different coordinates so that they can be displayed in a grid on my surfaceview.
Does anyone has an idea? Or maybe another solution how I can create a simple grid of objects, in my example mines?
Thanks in advance!
Why not use 2 dimensional arrays? You can define Mine[][] mines and then in the loop:
for(int i = 0; i < yMines; i++) {
for(int j = 0; j < xMines; j++) {
mines[i][j].setX(xCounter);
mines[i][j].setY(yCounter);
xCounter += 100;
}yCounter += 100;
}
There is a problem with where you are setting the coordinates in this bit of code:
mines[i*j].setX(xCounter);
mines[i*j].setY(yCounter);
For example, the coordinates (x=2, y=3) and (x=3, y=2) refer to different locations on the grid, however 2*3=6 and 3*2=6. You need slightly more complicated logic to get a unique index for every coordinate (semihyagcioglu's approach is much better):
public MineField(int xMines, int yMines) {
mines = new Mine[xMines*yMines];
int xCounter = 0;
int yCounter = 0;
for(int i = 0; i < yMines; i++) {
for(int j = 0; j < xMines; j++) {
mines[i+(j*yMines)].setX(xCounter);
mines[i+(j*yMines)].setY(yCounter);
xCounter += 100;
}yCounter += 100;
}
}
The reason the application crashes is that you need to instantiate each mine object in your Mine[] array before trying to call setX() and setY() on them.
for (int i=0; i< (xMines*yMines); i++)
Mine[i] = new Mine();
Related
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'm confused on how to make a java method to append two different 2D arrays without using/importing the arrays class. This is what I have so far:
private Cell[][] appendArrays(Cell[][] first, Cell[][] second) {
Cell[][] third = new Cell[first.length][first[0].length + second[0].length];
for (int i = 0; i < first.length; i++) {
for (int j = 0; j < first[i].length; j++) {
third[i][j] = first[i][j];
}
}
for (int i = 0; i < second.length; i++) {
for (int j = 0; j < second[i].length; j++) {
// don't know what to do here
}
}
return third;
}
A Cell is just an object, but I'm trying to understand the logic behind appending arrays so any help would be appreciated!
Also, I know there is a similar question found here (How do you append two 2D array in java properly?), but the answer is given on appending arrays from the top down (given both parameter arrays have the same amount of columns), while I am looking for appending arrays from side to side (assuming both parameter arrays have the same amount of rows).
You're almost there. I think something like this is what you're looking for.
private Cell[][] appendArrays(Cell[][] first, Cell[][] second) {
Cell[][] third = new Cell[first.length][first[0].length + second[0].length];
for (int i = 0; i < first.length; i++) {
for (int j = 0; j < first[i].length; j++) {
third[i][j] = first[i][j];
}
for (int j = first[i].length; j < first[i].length + second[i].length; j++) {
third[i][j] = second[i][j-first[i].length];
}
}
return third;
}
I am really new to java and having a bit of trouble with this. I have looked at other code on here and other places with similar problems but I do not understand the library files, etc. I'm trying to understand the basics now. Any help would be appreciated. My current code is:
public static void main(String[] args) {
double[][] father = new double[25][25];
for (int i = 0; i < 25; i++){
father[i] = Math.random();
for (int j = 0; j < 25; j++){
father[j] = Math.random();
}
}
I dont know java but a 2d array should work like this
public static void main(String[] args) {
double[][] father = new double[25][25];
for (int i = 0; i < 25; i++){
for (int j = 0; j < 25; j++){
father[i][j] = Math.random();
}
}
You are trying to set an array of doubles to a double. when trying to specify a certain item in the 2d array, always use arrayName[index1][index2].
public static void main(String[] args) {
double[][] father = new double[25][25];
for (int i = 0; i < 25; i++){
for (int j = 0; j < 25; j++){
father[i][j] = Math.random();
}
}
A two-dimensional array, such as you have, requires both indexes in order to refer to a specific element. For example father[3][6] is an element of the array (a double, because that's the type of the array), but father[i] is not.
Also, you should use the array lengths, not hard-coded values, as the iteration limits. That way, if the array's size changes, you don't need to change the limits as well. Instead of for (int i = 0; i < 25; i++) you should use for (int i = 0; i < father.Length; i++) so that if the length of the array changes you still iterate over the whole thing without overflowing.
All in all:
double[][] father = new double[25][25];
for (int i = 0; i < father.Length; i++) {
for (int j = 0; j < father[i].Length; j++) {
father[i][j] = Math.random();
}
}
This question may be a bit a tricky. I tried, but I am not really even sure what to search for, so I did not find any documentation on this.
I have a 7x10 grid, and each grid has its own image view. The ImageViews for the grids are named grid00, grid01, ..., grid79, grid710. I want to change the image for each grid in a nested for loop. so where it says in my code:
grid04.setImageResource(R.drawable.walllr);
What I realy want it to do is:
gridij.setImageResource(R.drawable.walllr);
where i and j after the word grid are the i and j from the nested for loop.
I am trying to change the image for all 70 items without writing code for each of the 70 items. Is this possible? Here is the code:
public void initialize() {
map = DM.getMap();
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 10; j++) {
if (map[i][j].isUsed()) {
grid04.setImageResource(R.drawable.walllr);
}
}
}
}
You don't mention where are those ImageViews named gridxx. If gridxx represents the id of that ImageView in the xml layout then you could use:
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 10; j++) {
if (map[i][j].isUsed()) {
int id = getIdentifier("grid" + i + j, "id", getPackageName());
((ImageView)findViewById(id)).setImageResource(R.drawable.walllr);
}
}
}
Keep in mind that the getIdentifier() method is a bit slow.
If for some odd reason, those are names used in the java class, the solution is to use an array like normal people.
Imageview[][] grid = new Imageview[7][10];
for(int i = 0; i <= 7; i++)
for(int j = 0; j <= 10; j++) //from 0 to 10, dunno if you want it from 1 to 10
{
grid[i][j]=new Imageview(....)
}
This is the only way to change all grids, like you want to do. First, create the grids. They will no longer be called grid04 or so, but grid[0][4].
You can create an array of ImageViews.
Imageview[] myImageViewArray = new ImageView[70];
int imageViewCounter=0;
and in your function
public void initialize() {
map = DM.getMap();
for (int i = 0; i < 7; i++) {
for (int j = 0; j < 10; j++) {
if (map[i][j].isUsed()) {
myImageViewArray[imageViewCounter]=new ImageView();
myImageViewArray[imageViewCounter].setImageResource(R.drawable.walllr);
imageViewCounter++;
}
}
}
}
You can use this way. But you should modify it before you use for your function. Hope this helps.
Create an 2D array of ImageView[7][10].
I am trying to build an 2D array with the size of from 1D array and pupolate it with random numbers. Why don't I get an output when I run this code below? There is no errors in my IDE:
public void raggedArray(){
int maxRows = 3;
int maxCols = 4;
int [] onedArray = new int [maxRows];
for (int i = 0; i < maxRows; i++){
onedArray[i] = (int) ((Math.random() * 100) * maxCols);
}
int [][] twodArray = new int[maxRows][];
for (int i = 0; i < maxRows; i++){
twodArray[i] = new int[onedArray[i]];
}
for (int i = 0; i < twodArray.length; i++){
for (int j = 0; j < twodArray[i].length; j++){
twodArray[i][j] = (int) (Math.random() * 100);
}
}
System.out.println("2 - The 2D array: ");
for (int i = 0; i < twodArray.length; i++){
for (int j = 0; j < twodArray[i].length; j++){
System.out.print(twodArray[i][j] + " ");
}
System.out.println("");
}
}
}
Your code compiles correctly, runs and outputs some numbers. Maybe raggedArray() isn't called like Giacomo mentioned?
It might also be that the two-dimensional array is created incorrectly. I suppose this:
twodArray[i] = new int[onedArray[i]];
should be replaced with:
twodArray[i] = new int[maxCols];
I just copied your code .. compiled it , ran it and got it to print something .. not really sure what we are trying to do here, but it does print something.
I don't know what the code does, and if it's correct or not, but it should print something, absolutely. At least System.out.println("2 - The 2D array: "); is guaranteed to be executed.
Are you sure raggedArray() is ever called?
Are you sure you're looking to the right spot in your IDE? Try without the IDE, running the code from a terminal.