Results 1 to 6 of 6
- 03-29-2012, 05:07 PM #1
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
adding certain numbers from multi-dimensional array
hi, just registered...first post.
first i have this array
(obviously the numbers in this array get changed)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...
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.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; 03-29-2012 at 05:52 PM.
-
Re: adding certain numbers from multi-dimensional array
Just to clean things up so others can read your code, you're asking about:
Consider using for loops for this.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){
- 03-29-2012, 05:51 PM #3
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
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?
- 03-29-2012, 06:34 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,398
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-29-2012, 06:40 PM #5
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
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.
- 03-31-2012, 09:11 PM #6
Member
- Join Date
- Mar 2012
- Posts
- 4
- Rep Power
- 0
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?
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.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 multi-dimensional ArrayList
By sebo in forum New To JavaReplies: 7Last Post: 09-26-2011, 11:29 PM -
multi or two dimensional array
By maya700 in forum New To JavaReplies: 4Last Post: 07-12-2010, 06:52 PM -
Multi dimensional Array
By Preethi in forum New To JavaReplies: 1Last Post: 07-09-2008, 03:34 PM -
Adding numbers in a 2 dimensional array
By j0shizabeast in forum New To JavaReplies: 2Last Post: 11-27-2007, 04:31 AM -
Help with array multi-dimensional
By barney in forum New To JavaReplies: 1Last Post: 07-31-2007, 08:00 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks