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

# Thread: 2D matrix like array (rows and columns) giving ArrayIndexOutOfBoundsException - please help

.

2. ## Re: 2D matrix like array (rows and columns) giving ArrayIndexOutOfBoundsException - please help

Originally Posted by javaiscool
.
I've created a 2D array, and am using a class object called ArrayOps to call a method called getColumnTotal (within another class, from my main method) get the sum of all values in a "column" of a matrix-like 2D array.
Here's your array in the example that failed.

```{ 5, 10, 7, 7,  2,  6, 7, 7},
{10,  6, 2, 7,  3,  7, 9, 7},
{ 1,  4, 3, 2,  9,  8, 3, 3},
{ 2,  8, 5, 5, 10, 10, 5, 9},
{ 2,  1, 4, 9,  3,  4, 2, 7},
{ 5, 10, 5, 7,  7,  4, 8, 6}```
1. How many rows does the array have? How many columns?

2. What is the value of array[0].length?

3. In the getColumnTotal() method, what is the largest value of i and what is the largest value of col in the following statement?
`            answer+=array[i][col];`

4. What parameter of the array can tell the getColumnTotal() method the number of rows in the matrix so that the loop can correctly add the values of the elements in a given column?

Originally Posted by javaiscool
8x5 matrix, and the program completed successfully, without error!
Really? You got the correct answers for the sums of the columns? Did you actually check them?

Here's the array for your second example
```{ 7, 8, 7, 3, 5},
{ 9, 8, 5, 9, 3},
{10, 3, 4, 2, 2},
{ 5, 7, 9, 3, 6},
{ 4, 7, 6, 7, 9},
{ 8, 9, 3, 7, 2},
{ 5, 2, 5, 4, 1},
{ 5, 2, 7, 4, 3}```

Originally Posted by javaiscool;97512, with rebuttal by Zaphod_b
The sum of the elements in column 0 is: 35 <--- I don't think so
The sum of the elements in column 1 is: 33 <--- Ditto
The sum of the elements in column 2 is: 31 <--- Ditto
The sum of the elements in column 3 is: 24 <--- Ditto
The sum of the elements in column 4 is: 25 <--- Ditto

Cheers!

Z

3. ## The Following User Says Thank You to Zaphod_b For This Useful Post:

javaiscool (February 22nd, 2013)

.

5. ## Re: 2D matrix like array (rows and columns) giving ArrayIndexOutOfBoundsException - please help

Originally Posted by javaiscool
.... what would be the best way....
Now we get down to a matter of style. Once the functionality is implemented, style is important, but it's a matter of taste. There is no "best way."

First of all, I can't imagine how to present all of the information you are calculating in a single, aesthetically pleasing, table that suits my sensibilities.

I'm thinking I would have one table for the rows and one for the columns, and print overall values (sum of elements in entire table and average of all elements) separately.

First of all I would use a text editor to create a sample output for values that I would expect to see. As a first pass, I would just make it a fixed format for "something" that might be suitable for arrays that I am likely to be using.

For example, suppose, for the time being that elements will always have positive values 1, 2, ..., 10. Then put some things in your text editor that would be something you would want to see.

I might want the table of column values for your second example to look like the following:

``` Col | Sum  | Low  | High |  Avg
-----+------+------+------+-------
0  |  53  |  4   |  10  | 6.625
1  |  46  |  2   |   9  | 5.750
2  |  46  |  3   |   9  | 5.750
3  |  39  |  2   |   9  | 4.875
4  |  31  |  1   |   9  | 3.875```

Maybe you would want the individual field widths and placement to be different. For example if you would be dealing with arrays for which the sum could be a three digit number, you might want some of fields to be wider. Maybe you want all of the fields to have the same width. Stuff like that. For now, let your imagination soar. How would you make it pretty enough for your mom to put it up on the refrigerator so that all of her friends could admire your creativity?

Rules of thumb from my typographer friend.
1. The '|' fences between columns of the table might not be necessary for small tables. Try it without them. It might look better.

2. Use enough white space between the columns of the table so that it doesn't look crowded. Don't use so much that the individual entries look lonesome.

3. If you do use "fences" between columns, use some white space between the element values and the fences.

4. Do NOT (sorry for shouting, but sometimes I can't help myself) put "--------" row separators between all of the rows. A single line of that stuff between the header and the rows is OK, and maybe adds to the readability, but more than that just clutters it up and really makes it look amateurish.

Start with those suggestions. Experiment. Just because a professional typographer expresses her opinion (based on the experience of a career in design), printed output doesn't mean you have to slavishly follow the generally accepted "rules of thumb."

Look in well-designed textbooks or other literature. Of course your ASCII output can't match the beauty of properly designed "real" typesetting, but you should have an idea of what looks good to you.

Anyhow, once you have created the tableau that you think is OK, now think about how you would create that printout.

For me, the simplest way is to learn to use printf with formatted output.

Here's how I might print the rows of my example table:
```        int cols = array[0].length;
for (int col = 0; col < cols; col++) {
// Declare variables for the sum of column number j
// and the lowest and highest element values in column j
int sumCol ...
int lowest ...
int highest...
//
// Calculate the values, somehow
//
// Then...
//
double avgCol = (double)sumCol / (double)array.length;
System.out.printf("%3d  | %3d  | %2d   |  %2d  | %5.3f\n",
col, sumCol, lowest, highest, avgCol);
}
// Now do something similar for the "row" table
//
// Do something for the overall sum and average
//
//  Etc.```

Note that you can calculate each line in the table and print the results for each line without actually storing the stuff in a new array. Just calculate and print on the fly.

Bottom line: Adjust the field widths to make it look pleasing. Enough white space but not too much; that's the ticket! What's most important here? Do the design first, then do the program.

Similarly for the row table.

Now how do you print the header and the separator line (the "-----+----" stuff)? Well for starters you can just brute-force them with a single println() statement for each. Count the required number of '-' and the placement of '+' from your text editor design (or just do it with trial and error if you aren't particularly good at counting).

For future considerations: Maximum flexibility might be obtained by generating format strings with variable spacing that can "automatically" adjust the field widths according to maximum values in a given application.

My conclusion:
There is no limit to the amount of time you can spend creating tableau outputs. Do it and test it for different sizes of arrays and array elements. When you are really tired of that, go on to bigger and better things, but leave the program in a state that generates acceptable output for all of your test cases. You can come back and enhance it later when inspiration or necessity dictates.

Cheers!

Z

6. ## The Following User Says Thank You to Zaphod_b For This Useful Post:

javaiscool (February 22nd, 2013)

G