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 5 of 5

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

  1. #1
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

    Default 2D matrix like array (rows and columns) giving ArrayIndexOutOfBoundsException - please help

    .
    Last edited by javaiscool; February 27th, 2013 at 01:09 AM. Reason: solved


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

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

    Quote Originally Posted by javaiscool View Post
    .
    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?


    Quote Originally Posted by javaiscool View Post
    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}

    Quote 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)

  4. #3
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

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

    .

  5. #4
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

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

    Quote Originally Posted by javaiscool View Post
    .... 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)

  7. #5
    Member
    Join Date
    Feb 2013
    Location
    earth
    Posts
    88
    Thanks
    12
    Thanked 9 Times in 9 Posts

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

    G

Similar Threads

  1. Exception in main thread for 2D Array (columns)
    By java_begginer in forum Exceptions
    Replies: 2
    Last Post: December 3rd, 2012, 03:44 PM
  2. My program wont print the sum of the rows in a two-dimensional array
    By cdominguez_2353 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 1st, 2012, 07:29 PM
  3. Search for min and max values as columns position in array
    By susieferrari in forum Java Theory & Questions
    Replies: 3
    Last Post: April 28th, 2011, 07:39 AM
  4. Building Table of Specified Rows and Columns
    By wale89 in forum Java Servlet
    Replies: 1
    Last Post: August 3rd, 2010, 08:57 AM
  5. Need a loop for rows and columns
    By Ceasar in forum Loops & Control Statements
    Replies: 8
    Last Post: October 9th, 2009, 05:47 PM

Tags for this Thread