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];
}
}
}
}
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;
}
Open in new window