Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By JosAH

Thread: adding certain numbers from multi-dimensional array

  1. #1
    sirganon is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default adding certain numbers from multi-dimensional array

    hi, just registered...first post.

    first i have this array
    Java 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...
    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){
    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
    Last edited by sirganon; 03-29-2012 at 05:52 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: adding certain numbers from multi-dimensional 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){
    Consider using for loops for this.

  3. #3
    sirganon is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default 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?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default 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
    Fubarable likes this.
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    sirganon is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default 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.

  6. #6
    sirganon is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default 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?
    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();
    	}
    }
    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.

Similar Threads

  1. Replies: 7
    Last Post: 09-26-2011, 11:29 PM
  2. multi or two dimensional array
    By maya700 in forum New To Java
    Replies: 4
    Last Post: 07-12-2010, 06:52 PM
  3. Multi dimensional Array
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 07-09-2008, 03:34 PM
  4. Adding numbers in a 2 dimensional array
    By j0shizabeast in forum New To Java
    Replies: 2
    Last Post: 11-27-2007, 04:31 AM
  5. Help with array multi-dimensional
    By barney in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 08:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •