Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Help with 2 Dimensional Arrays

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help with 2 Dimensional Arrays

    I am trying to create a 3x3 board using 2 dimensional array that randomly fills 0s and 1s into the board and finds the rows, columns, and diagonals with all 1s.

    Please help!

    Thank you


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with 2 Dimensional Arrays

    What's your question? What have you tried? Where are you stuck? Please see the link in my signature on asking questions the smart way, as well as this article: http://www.javaprogrammingforums.com...e-posting.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with 2 Dimensional Arrays

    I have declared to generate a random number and assign to each row and column of the 3x3 array. I'm not sure if the below portion of the code is correct.

    Random randomGenerator = new Random();


    int[] [] array = new int [3][3];
    array [1][1] = randomGenerator.nextInt(1);
    array [1][2] = randomGenerator.nextInt(1);
    array [1][3] = randomGenerator.nextInt(1);
    array [2][1] = randomGenerator.nextInt(1);
    array [2][2] = randomGenerator.nextInt(1);
    array [2][3] = randomGenerator.nextInt(1);
    array [3][1] = randomGenerator.nextInt(1);
    array [3][2] = randomGenerator.nextInt(1);
    array [3][3] = randomGenerator.nextInt(1);

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with 2 Dimensional Arrays

    Well, have you stepped through the program with a debugger, or simply added print statements, to figure out what exactly the code is doing?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with 2 Dimensional Arrays

    I am trying to display the board using JOptionPane and it is giving me an exception error - Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with 2 Dimensional Arrays

    Well, without an SSCCE, I'm only guessing, but I can tell you that arrays are 0-based. That means that an array with 3 indexes will have index 0, 1, and 2. Index 3 is actually the 4th index.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with 2 Dimensional Arrays

    How do I randomly fill with 0s and 1s in the 3x3 board?

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with 2 Dimensional Arrays

    You have the right idea, you just aren't keeping in mind that arrays start at index 0. Zero is the first index, one is the second index, etc.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with 2 Dimensional Arrays

    I understand what you are saying. Index are element - 1. I don't know how to go forward with filling the board with 0s and 1s in the 3x3 position. For example for each number of row and column such at (1 x 1) (1 x 2) (1 x 3) (2 x 1) (2 x 2) ( 2 x 3) so on and so forth.

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with 2 Dimensional Arrays

    If you understand that arrays start at zero, why are you starting your indexes at one? If you understand that they go to array.length - 1, why are you trying to access array.length?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Junior Member
    Join Date
    Jan 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help with 2 Dimensional Arrays

    Can you show me how I can fill the board with 0s and 1s?

  12. #12
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help with 2 Dimensional Arrays

    Quote Originally Posted by s1mmi View Post
    Can you show me how I can fill the board with 0s and 1s?
    This is not a homework service. I've posted a couple links and gave you plenty of hints. Work with them, throw together an SSCCE, and ask a specific question, and we'll go from there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Input in a two Dimensional Array.
    By Mr.777 in forum File I/O & Other I/O Streams
    Replies: 29
    Last Post: December 14th, 2011, 11:40 PM
  2. Two dimensional Arrays
    By masterT in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 19th, 2011, 05:37 PM
  3. help w/ storing/scanning numbers in two dimensional arrays
    By robertsbd in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 1st, 2010, 11:56 PM
  4. Two dimensional arrays - HELP!
    By ecco in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 19th, 2010, 12:32 PM
  5. How to show 2D array using combobox and radiobutton?
    By Broken in forum Loops & Control Statements
    Replies: 1
    Last Post: March 10th, 2009, 06:01 AM