public static int whichShip(int[][] array, int row, int col){
//find which ship got hit (find coordinate in compBoard array, and return the index)
int i = 0;
for (i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++){
if (array[i] == row && array[i][j] == col){
return i;
}
}
}
}
public static int whichShip(int[][] array, int row, int col){
//find which ship got hit (find coordinate in compBoard array, and return the index)
int i = 0;
for (i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++){
if (i== row && j == col){
return arry[i][j];
}
}
}
}
array[i] == row
beacuse array[i] is one-dimensional int array int[]
public static int whichShip(int[][] array, int row, int col){
//find which ship got hit (find coordinate in compBoard array, and return the index)
int result = 0;
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++){
if (i == row && array[i][j] == col){
result = i;
}
}
}
return result;
}
int value = 12;
Coordinates coords = getCoords(array, value);
System.out.println(coords.getRow() + " ," + coords.getColumn());
public Coordinates getCoords(int [][] array, int value){
for(int j=0;j<array.length; j++){
for(int i=0; i<array[j].length; i++){
if(array[j][i] == value){
Coordinates coords = new Coordinates(j, i);
return coords;
}
}
}
return null;
}
}
public class Miscellaneous {
static int[][] array = new int[][]{{1,2,3},{4,5,6} };
public static Coordinates getCoords(int [][] array, int value){
for(int j=0;j<array.length; j++){
for(int i=0; i<array[j].length; i++){
if(array[j][i] == value){
Coordinates coords = new Coordinates(j, i);
return coords;
}
}
}
return null;
}
public static void main(String[] args) {
int value = 6;
Coordinates coords = getCoords(array, value);
System.out.println(coords.getRow() + " , " + coords.getColumn());
}
}
class Coordinates {
int row;
int col;
public Coordinates(int row, int col){
this.row = row;
this.col = col;
}
public int getRow() { return row; }
public int getColumn() { return col; }
}
1 , 2
public static int whichShip(int[][] array, int row, int col){
//find which ship got hit (find coordinate in compBoard array, and return the index)
int result = 0;
for (int i = 0; i < array.length; i++){
for (int j = 0; j < array[i].length; j++){
if(array[i][j] == (row * 10) + col){
result = i;
break;
}
}
}
return result;
}
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
css issues with IE 11 | 8 | 35 | |
Java: How do I open the default windows program for a given file type | 3 | 29 | |
jboss wildfly 10.1 | 10 | 97 | |
How can I make the input automatically take the width of the column? | 1 | 17 |
Join the community of 500,000 technology professionals and ask your questions.