Results 1 to 6 of 6
 03292012, 06:07 PM #1Member
 Join Date
 Mar 2012
 Posts
 4
 Rep Power
 0
adding certain numbers from multidimensional array
hi, just registered...first post.
first i have this array
Java Code:int secondarray[][]={{0,0,0},{0,0,0},{0,0,0}};
and i was wondering if there is an easier way of writing this code...
Java 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){
thanks in advance for any helpLast edited by sirganon; 03292012 at 06:52 PM.

Re: adding certain numbers from multidimensional array
Just to clean things up so others can read your code, you're asking about:
Java 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){
 03292012, 06:51 PM #3Member
 Join Date
 Mar 2012
 Posts
 4
 Rep Power
 0
Re: adding certain numbers from multidimensional 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 tictactoe 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?
 03292012, 07:34 PM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,422
 Blog Entries
 7
 Rep Power
 29
Re: adding certain numbers from multidimensional 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,
JosBuild a wall around Donald Trump; I'll pay for it.
 03292012, 07:40 PM #5Member
 Join Date
 Mar 2012
 Posts
 4
 Rep Power
 0
Re: adding certain numbers from multidimensional 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.
 03312012, 10:11 PM #6Member
 Join Date
 Mar 2012
 Posts
 4
 Rep Power
 0
Re: adding certain numbers from multidimensional 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?
Java 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(); } }
Similar Threads

adding values from file to multidimensional ArrayList
By sebo in forum New To JavaReplies: 7Last Post: 09272011, 12:29 AM 
multi or two dimensional array
By maya700 in forum New To JavaReplies: 4Last Post: 07122010, 07:52 PM 
Multi dimensional Array
By Preethi in forum New To JavaReplies: 1Last Post: 07092008, 04:34 PM 
Adding numbers in a 2 dimensional array
By j0shizabeast in forum New To JavaReplies: 2Last Post: 11272007, 05:31 AM 
Help with array multidimensional
By barney in forum New To JavaReplies: 1Last Post: 07312007, 09:00 PM
Bookmarks