Results 1 to 4 of 4

Thread: reverse array

  1. #1
    jwood is offline Senior Member
    Join Date
    Jun 2013
    Posts
    136
    Rep Power
    0

    Default reverse array

    Write the statements needed to reverse the elements in the array .

    here is what I have:
    int left = arr[0];
    int right = arr[arr.length-1];


    while(left < right)
    {
    int temp = left;
    left = right;
    right = temp;


    left++;
    right--;
    }


    Why do I get an out of bounds exception? Specific errors are

    Problems Detected:
    ⇒ Exception java.lang.ArrayIndexOutOfBoundsException: 0 occurred
    ⇒ arr is till in the same order not reversed

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,330
    Rep Power
    5

    Default Re: reverse array

    You haven't included all of your code. In any event, have you allocated the array of any size? And the while loop is not doing anything with the contents of the array.

    And please wrap you code in [code][/code] tags.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default Re: reverse array

    With 37 posts over more than a month, hasn't anyone told you about the code tags?

    Guide For New Members
    BB Code List - Java Programming Forum - Learn Java Programming

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,330
    Rep Power
    5

    Default Re: reverse array

    Yep. I did in the previous post just before yours. But you also did at post #17 in draw method for Point2D.Double ?

    Interestingly enough, he was using them on his own (albeit PHP tags) since his first post.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Reverse
    By system5634 in forum New To Java
    Replies: 2
    Last Post: 03-26-2012, 09:55 AM
  2. Replies: 10
    Last Post: 03-15-2012, 10:46 AM
  3. Without using reverse method
    By blindfolded916 in forum New To Java
    Replies: 3
    Last Post: 12-08-2010, 06:18 PM
  4. Array reverse algorithim - reversing elements
    By Adomini in forum New To Java
    Replies: 9
    Last Post: 08-30-2010, 04:13 AM
  5. Math.random in array and reverse order
    By eugenechia in forum New To Java
    Replies: 4
    Last Post: 02-17-2010, 03:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •