menna menna - 1 year ago 71
Java Question

java looping in 2d array with if conditions statements

im trying to do a certain project and there is this part in which i want to loop in a 2d array and if the certain value of i & j then do something..

to be more specific my 2d array is defined for files and users and the attached photo might explain well, i want for example when index i=0 and j=0 equals 1 then print out that user 1 can write in file, i was able to iterate over the array and find the indexes where 1's occur in the array in the following code:

my 2d array defined

public class test {

public int counteri=0;
public int counterj=0;

public void readpermission (int[][] array)
for(int i=0; i<2; i++)
for(int j=0; j<6; j++)
counterj = j;
System.out.println("found 1's at index =" + counteri +" "+ counterj );






found 1's at index =0 0,

found 1's at index =0 1

found 1's at index =0 4

found 1's at index =1 1

found 1's at index =1 2

found 1's at index =1 3

found 1's at index =1 4

found 1's at index =1 5

when the first value is 0 0 i want an output that user 1 can write in file 1
and when th output is 0 1 i want an output stating that user 1 can read in file 1 and so on ...

Answer Source

Given your last comment:


could be

if (i==0 && j==0 && array[i][j] == 1)

But the point is: that is really basic java syntax; and you should not need to come here and ask about that. Instead: you study tutorials and education materials that tells you how to do work with java syntax, for example here.

But please note: the whole thing that you are describing doesn't make too much sense. You are using int values to model permissions. That is not a good idea. Instead: you could create your own Permission class; or at least: use an enum instead of int values. Assuming that an int with value 1 ... means "write access" is simply a very naive and actually not-at-all intuitive model.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download