Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Wierd ArrayIndexOutOfBounds

Thread view: 
Sathyaish - 05 Apr 2007 16:50 GMT
This one seems alright to look at but gives me an
ArrayIndexOutOfBoundsException. I can't see where I am crossing the
array bounds.

//package sathyaish.practice.utilities;

/*
I want to see how many expressions I can append to a single for-loop
statement in Java.

        for ( int i = 0; i < args.length; i++, sb.append( args[i] ),
sb.append(delimiter));
*/
public class lfl /* long for loop */
{
    public static void main(String[] args)
    {
        if ( args.length == 0 )
            System.out.println("Usage lfl arg1 [arg2 [arg3...]]");

        java.lang.StringBuffer sb = new StringBuffer();
        for ( int i = 0; i < args.length; i++, sb.append( args[i] ),
sb.append(" "));
        System.out.println(sb.toString());
    }
}

Test:

Prompt> java lfl Sathyaish is a good boy
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
       at lfl.main(lfl.java:14)
Tom Hawtin - 05 Apr 2007 17:03 GMT
> This one seems alright to look at but gives me an
> ArrayIndexOutOfBoundsException. I can't see where I am crossing the
> array bounds.

>         for ( int i = 0; i < args.length; i++, sb.append( args[i] ),

If i was args.length-1, incrementing takes it to args.length whcih you
then use. Remember, the third part of the for loop is executed after the
body, so moving stuff onto the end of it rearranges the order.

> sb.append(" "));

Your code is the equivalent of:

    {
         int i = 0;
         while (i < args.length) {
             i++;
             sb.append(args[i]);
             sb.append(" ");
         }
    }

Tom Hawtin
Sathyaish - 05 Apr 2007 17:25 GMT
Oh, thank you, Tom!

:-)

You made my day. What a profound piece of learning. It is a wonder
that we learn from the smallest of examples, the most valuable
lessons. I've been programming for several years but only just
discovered this. That was because I never wrote such a large for loop
statement in commercial code. In practice sessions, though, one learns
the most.

Thank you once again.
Lew - 05 Apr 2007 22:55 GMT
> Oh, thank you, Tom!
>
[quoted text clipped - 8 lines]
>
> Thank you once again.

You avoid this issue if you put the loop body in, well, the loop body instead
of the header.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.