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

Thread: Out of bounds error, 2d irregular array of integers

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Location
    Omaha, NE
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Out of bounds error, 2d irregular array of integers

    Hello,

    The code initializes a irregular array of integers, prints the length of each row in the array. Then I attempted to print the table[i][j], but I run into an out of bounds exception. In the for loops, the conditional statements are based on the value of table.length, which changes depending upon the row your looking at. I believe this has to do with the out of bounds exception.

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3. at hslengthdemoarray2.HSLengthDemoArray2.main(HSLengt hDemoArray2.java:32)
    Line 32 is: System.out.print(table[i][j] + " ");
    Output: 2d table array: 1 2 3

    The program should output the following:
    2d table array: 1 2 3 4 5 6 7 8 9
    2d table array with the one value changed: 1 66 3 4 5 6 7 8 9

    When I turn this into a regular 2d array, where each row has 3 elements, the code runs fine and output is as expected. I think this is because the nested for loop only acknowledges one value of table.length for all of i and j. For example; I thought when i = 0, table[0].length would be 3, in j loop, table.length = 3 based on the i loop. Then, when i = 1, table[1].length = 2, in j loop, table.length = 2 based on the i loop.. On the third step of the outerloop, when i = 2, table[2].length = 4, in j loop, table.length = 3 based on i loop. This appears not to be the case. It appears that table.length either holds a value based on i throughout both for loops or table.length changes based on i and j. It would be good if it only depended on i, I think.
    package hslengthdemoarray2;
     
    public class HSLengthDemoArray2 {
        public static void main(String[] args) {
            int[][] table = {
                {1,2,3},
                {4,5},
                {6,7,8,9},
            }; // A variable length table.
     
            // Print the length of each row within the array.
            System.out.println("length of table[0] is " + table[0].length);
            System.out.println("length of table[1] is " + table[1].length);
            System.out.println("length of table[2] is " + table[2].length);
            System.out.println();
     
            System.out.print("2d table array: ");
            for(int i=0; i<table.length; i++) {
                for(int j=0; j<table.length; j++) {
                    System.out.print(table[i][j] + " ");
                }
            }
     
            table[0][1] = 66;
     
            System.out.print("2d table array with the one value changed: ");
            for(int i=0; i<table.length; i++) {
                for(int j=0; j<table.length; j++) {
                    System.out.print(table[i][j] + " ");
                }
            }
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Out of bounds error, 2d irregular array of integers

    the for loops, the conditional statements are based on the value of table.length
    Try table[i].length to get the length of the subarray you are working with

  3. #3
    Junior Member
    Join Date
    Jul 2011
    Location
    Omaha, NE
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Out of bounds error, 2d irregular array of integers

    Thank you for your help Norm. Everything works well now, however, I do have another question related to this site. I am unable to post my messages as solved. When I initially post my problems, I can select whether or not the problem is solved but at that time the problems are unsolved. Then when I go back to post a follow up message after a solution has been posted, like right now, there is no option that I can find that posts the message as "solved". Any thoughts would be greatly appreciated. The option must work for some because I see other problems listed as solved.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Out of bounds error, 2d irregular array of integers

    No idea and it's something I should know, but don't.

Similar Threads

  1. problems getting past the out of bounds error
    By dbdny in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 28th, 2011, 05:57 PM
  2. Array exception out of bounds
    By gabberlt in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 17th, 2011, 08:05 AM
  3. [SOLVED] Writing Integers to .txt File; Returning Random Characters Instead of Integers
    By verbicidalmaniac in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 8th, 2011, 09:42 PM
  4. Array index out of bounds
    By fortune2k in forum Collections and Generics
    Replies: 1
    Last Post: November 30th, 2010, 07:11 AM
  5. String index out of bounds error 5???
    By stealthmonkey in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 26th, 2010, 07:11 PM