adding certain numbers from multi-dimensional array

hi, just registered...first post.

first i have this array

Code:

`int secondarray[][]={{0,0,0},{0,0,0},{0,0,0}};`

(obviously the numbers in this array get changed)

and i was wondering if there is an easier way of writing this code...

Code:

`if(secondarray[0][0]+secondarray[0][1]+secondarray[0][2] == 12 ||`

secondarray[1][0]+secondarray[1][1]+secondarray[1][2] == 12 ||

secondarray[2][0]+secondarray[2][1]+secondarray[2][2] == 12 ||

secondarray[0][0]+secondarray[1][0]+secondarray[2][0] == 12 ||

secondarray[0][1]+secondarray[1][1]+secondarray[2][1] == 12 ||

secondarray[0][2]+secondarray[1][2]+secondarray[2][2] == 12 ||

secondarray[0][0]+secondarray[1][1]+secondarray[2][2] == 12 ||

secondarray[0][2]+secondarray[1][1]+secondarray[2][0] == 12){

this code works but i was wondering if there is a more simplified way of adding various values from multi-dimensional arrays. i tried a few ideas off the top of my head but none of them worked.

thanks in advance for any help

Re: adding certain numbers from multi-dimensional array

Just to clean things up so others can read your code, you're asking about:

Code:

`if(secondarray[0][0]+secondarray[0][1]+secondarray[0][2] == 12 || `

secondarray[1][0]+secondarray[1][1]+secondarray[1][2] == 12 ||

secondarray[2][0]+secondarray[2][1]+secondarray[2][2] == 12 ||

secondarray[0][0]+secondarray[1][0]+secondarray[2][0] == 12 ||

secondarray[0][1]+secondarray[1][1]+secondarray[2][1] == 12 ||

secondarray[0][2]+secondarray[1][2]+secondarray[2][2] == 12 ||

secondarray[0][0]+secondarray[1][1]+secondarray[2][2] == 12 ||

secondarray[0][2]+secondarray[1][1]+secondarray[2][0] == 12){

Consider using for loops for this.

Re: adding certain numbers from multi-dimensional array

sorry and thanks for cleaning that up. ill edit my op too.

im not sure i understand how to use a for loop for this. its a tic-tac-toe game i made and that checks the 8 lines for a win. i just started learning java maybe two weeks ago from youtube videos. while i am understanding some things im not totally aware of all the ways some of these codes can be implemented. could you maybe give an example of how a for loop can be used for this?

Re: adding certain numbers from multi-dimensional array

Maybe this can be of help: given a cell of your matrix with indexes (x,y); you can 'walk' any straight line through the matrix by repeatedly adding a value (dx, dy) to (x,y); e.g. start at (0,2) and add (1,-1) to the index values; the index values are (0,2), (1,1), (2, 0); you have walked over the diagonal from top/right to bottom/left. For suitable values of (x,y) and (dx,dy) you can walk over rows, columns or diagonals. While 'walking' over that line you can register anything you want, given the (x,y) value and m[x,y] value for a matrix m.

kind regards,

Jos

Re: adding certain numbers from multi-dimensional array

that makes sense. thank you. ill see if i can get something like this working and ill be back if i have anymore questions. if anyone has anymore good ideas though i would be more than glad to hear them.

Re: adding certain numbers from multi-dimensional array

ok im still having trouble trying to get JosAH's suggestion working. is there any way i could add it to the code below or should i just keep it separate?

Code:

`public static void display(String x[][]){`

for(int row=0; row<x.length; row++){

for(int column=0; column<x[row].length; column++){

System.out.print(x[row][column]+" ");

}

System.out.println();

}

}

this code is already in my program to display the 3x3 grid. this is for a tic-tac-toe program that i have working perfectly but i was just wanting to find better ways to write code, more simplified and stream-lined. my code is currently around 200 lines long and im pretty sure it doesnt need to be. some lines are empty lines or have comments but im pretty positive there are better ways to write some of the code i already have. i can post the whole thing if anyone would be willing to look through it and offer suggestions.