how to get the index of an element in an array of arrays

1

I have a matrix and I need to find the index of a specific element in a column .

I have seen the index of method but only used in normal arrays of strings, ints etc not in arrays of arrays or arrays.

for example I have String tablero[][]= new String[15][35];

and I want to do something like

tablero.indexOf(tablero[columna], "*");// claramente esto no funciona

int indice=tablero[columna].indexOf("*")// esto tampoco

I'm trying to do it following this example

import java.io.*;
public class Test {

public static void main(String args[]) {
    String Str = new String("Welcome to Tutorialspoint.com");
    System.out.print("Found Index :" );
    System.out.println(Str.indexOf( 'o' ));
 }
}
    
asked by zedsdeath 15.10.2017 в 23:16
source

2 answers

0

Having a 3 x 3 matrix as an example

String arreglo[][] = new Int[3][3];

if in boxes [2] [1] we have an "*" value, it is advisable to search for a nested for

for(int fila = 0; fila < arreglo.length; fila++)
   for(int columna = 0; columna < arreglo[fila].length; columna++)
      if(arreglo[fila][columna]).equals("*")
         System.out.printf("El asterisco esta en las casillas [%d][%d]", 
                            fila, columna );
    
answered by 16.10.2017 / 01:36
source
0

The simplest thing to do would be to use 2 nested loops (one to go through the rows and one for the columns).

Scanner sc = new Scanner(System.in);
System.out.println("A encontrar: ");
String elemento = sc.nextLine();

for(int i=0;i<filas;i++){
    for(int j=0;j<columnas;j++){
        if(tablero[i][j].equals(elemento)){
            System.out.println("Elemento encontrado en fila " + i +" columna " + j);
            break;
        }
    }
}

It would also be convenient to save the number of rows and columns in variables to be used when creating the matrix.

int filas = 15;
int columnas = 35;

String tablero[][]= new String[filas][columnas];
    
answered by 16.10.2017 в 01:26