# 2d array help

• 01-30-2012, 03:43 AM
nweid1
[Solved] 2d array help
I have to write a program for my programming class prints out a table filled with "-"s and randomly fills them with "#"s. So first, I have to create a 2d array where the number of rows and columns are given by the user. Then, I have to print the initial table with all "-"s. So, I created a boolean array with the number of rows and columns as the user input, and make a for loop to set every spot in the array as false. Then I may a loop that says if the spot is false, print out a "-". If I enter a number over 1 for the column or row, it won't work. If I put 1, it runs but doesn't print out. Please help! What am I doing wrong? A sample of the output would be:

Rows: 5
Columns: 8

- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -
- - - - - - - -

Heres the code

Code:

```public class Matrix2 {   import java.util.Random; import java.util.Scanner;             public static void main(String[] args) {                   Scanner console = new Scanner(System.in);                 System.out.println("Number of rows?");                 int rows = console.nextInt();                   System.out.println("Number of columns?");                 int columns = console.nextInt();                   boolean board[][] = new boolean[rows][columns];                   for (int i = 0; i < rows; i++) {                         for (int j = 0; j < columns; j++) {                                   board[rows][columns] = false;                           }                 }                   for (int i = 0; i < rows; i++) {                         for (int j = 0; j < columns; j++) {                                   if(board[rows][columns] == false) {                                         System.out.print("-");                                   }                           }                         System.out.println();                 }         } }```
• 01-30-2012, 04:22 AM
diamonddragon
Re: 2d array help
Why i and j are initialized to 1?

Code:

```for (int i = 1; i < rows; i++) {             for (int j = 1; j < columns; j++) {```
I don't see any random value in code above.
• 01-30-2012, 04:31 AM
nweid1
Re: 2d array help
Good call. I switched them to 0 but it still gives me this error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
• 01-30-2012, 04:48 AM
diamonddragon
Re: 2d array help
How do You access element of an array?
• 01-30-2012, 05:18 AM
Rameshwar Soni
Re: [Solved] 2d array help
Quote:

Originally Posted by nweid1

Code:

```for (int i = 0; i < rows; i++) {                         for (int j = 0; j < columns; j++) {                                   board[rows][columns] = false;                           }                 }```

Firstly it is not always possible that the user is going to input same number of rows and columns i.e. it can be a irregular 2-dimensional array, therefore
you cannot write your inner for-loop the way you have written. More better way is
Code:

```for(int i=0; i< rows; i++)           {               for(int j=0; j<board[i].length; j++)               {                          //code               }           }```
Secondly as said previously by dragon you are accessing your array elements incorrectly inside the for loop, it has to be
Code:

`board[i][j]`
Code:

`board[rows][columns]`
• 01-30-2012, 05:36 AM
diamonddragon
Re: [Solved] 2d array help
Quote:

Originally Posted by Rameshwar Soni
Firstly it is not always possible that the user is going to input same number of rows and columns i.e. it can be a irregular 2-dimensional array, therefore
you cannot write your inner for-loop the way you have written. More better way is

It's called rugged array.
• 01-30-2012, 06:23 AM
Rameshwar Soni
Re: [Solved] 2d array help
The question originally posted by user will not have the irregular 2 dimensional array. So the for-loops used by the original user will work but will fail
if he goes for irregular multi-dimensional array.

Quote:

Originally Posted by diamonddragon
It's called rugged array.

Do you mean to say Jagged array, which is there in C-sharp?:)-: