Use indexOf for 2D array in java -


hello create 2d array , want find position first 2 in array. , after add index in new arraylist. code doesn't work. idea problem?

import java.util.arraylist;  class test {     public static void main(string[] args) {          arraylist<integer> tableau = new arraylist<integer>();         int[][] tab = {                         {1,1,1,1,1,1,2,1,1,2,1,1,1,2,1,2,1,1,1,1,1},                         {1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1},                     };          (int = 0; < tab.length; i++) {             int j = tab[i].indexof(2);             (int k = j ; k < tab[i].length; k++) {                 if (tab[i][j] == 2){                     tableau.add(j);                 }             }          }         (integer row : tableau) {                 system.out.println("row = "+ arrays.tostring(tableau));         }     } } 

as others have mentioned, regular arrays not have indexof method, meaning error when compile.

however, can create own method use substitute.

private static int indexof(int value, int[] array) {     (int i=0; i<array.length; i++)     {         if (array[i] == value)             return i;     }     return -1; } 

then can replace line:

int j = tab[i].indexof(2); 

with one:

int j = indexof(2, tab[i]); 

Comments

Popular posts from this blog

php - Invalid Cofiguration - yii\base\InvalidConfigException - Yii2 -

How to show in django cms breadcrumbs full path? -

ruby on rails - npm error: tunneling socket could not be established, cause=connect ETIMEDOUT -