array[i] is one-dimensional array
beaciues array is two dimensional array
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;
}
}
}
}
array[i] is one-dimensional array
beaciues array is two dimensional array
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;
}
Title | # Comments | Views | Activity |
---|---|---|---|
base64 decode encode | 12 | 93 | |
advertisement module in core php | 4 | 76 | |
backtracking recursion code | 19 | 39 | |
(URGENT) Help HTML and CSS Resizing Images | 2 | 19 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
10 Experts available now in Live!