Results 1 to 13 of 13
Like Tree1Likes
  • 1 Post By jim829

Thread: ArrayList

  1. #1
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default ArrayList

    When an "index out of bound "exception can occur in an ArrayList?!!
    Thanks...

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: ArrayList

    whenever you try to access and index in the ArrayList that is out of range.

    IndexOutOfBoundsException (Java Platform SE 6)

  3. #3
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: ArrayList

    I am using arraylist.get(int i) method until i<arraylist.size()
    So I can't understand what is problem...

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: ArrayList

    Actually, your statement does not make sense. It would be "... while i < arraylist.size()." In any event, provide a small compilable example that demonstrates the problem. Without the code and error messages it is difficult to help.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  5. #5
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: ArrayList

    OK.
    But it is wonderful,because sometimes this exception occur.
    I think netbeans has confused:D

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: ArrayList

    I doubt it is a netbeans problem. You need to post your code.

    Regards,
    Jim
    sehudson likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: ArrayList

    Thank you so much
    But my program is a game and use applet .It has many classes that all of them is connected to each other...

  8. #8
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    357
    Rep Power
    5

    Default Re: ArrayList

    Quote Originally Posted by faraa View Post
    Thank you so much
    But my program is a game and use applet .It has many classes that all of them is connected to each other...
    That's fine, but your question was about an IndexOutOfBoundsException being thrown while looping over elements in an ArrayList.
    If you post some of your code, we may be able to help you find the problem.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: ArrayList

    Then post the the loop and the code within it.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  10. #10
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: ArrayList

    Java Code:
      public void draw(Graphics g){
            for (int i=0;i<objects.size();i++){
                objects.get(i).draw(g);
        }
        
    }

    !!!!!
    Is it enough?!
    Thank's...

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: ArrayList

    Quote Originally Posted by faraa View Post
    sometimes this exception occur.
    Sounds like a concurrency issue. Does your code respect Swing's single threaded rule?

    Recommended reading: Lesson: Concurrency in Swing (The Java™ Tutorials > Creating a GUI With JFC/Swing)

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

  12. #12
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    268
    Rep Power
    2

    Default Re: ArrayList

    So when you execute, sometimes it occurs and sometimes it doesn't occur, under the same circumstances?

  13. #13
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,923
    Rep Power
    5

    Default Re: ArrayList

    I believe it must be a concurrency issue as Darryl already pointed out. Here is a contrived example demonstrating the effect.

    Java Code:
    public class LoopCheck {
       List<Integer> list = new ArrayList<>();
       public static void main(String[] args) {
         new LoopCheck().begin();
       }
       public void begin() {
          // initialize the list
          for (int j = 0; j < 10;j++) {
             list.add(j);
          }
          // now begin looping thru the list
          for (int j = 0; j < list.size(); j++) {
             if ( j == 9) {
                // at this point, the list was of size 10
                new Thread(new Runnable() {
                   public void run() {
                     list.remove(2);
                   }
                }).start();
                try {
                   // give the thread some time
                   Thread.sleep(40);
                }catch (InterruptedException ie) {}
             }
             // now the list is of size 9 -- oops!
             System.out.println(list.get(j)); // exception thrown here
          }
       }
    
    }

    Regards,
    Jim
    Last edited by jim829; 06-11-2013 at 04:08 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 12
    Last Post: 07-07-2011, 09:49 PM
  2. copying contents of an ArrayList to another ArrayList
    By ankit1801 in forum New To Java
    Replies: 8
    Last Post: 03-27-2011, 07:07 AM
  3. sorting arraylist based on another arraylist
    By busdude in forum New To Java
    Replies: 4
    Last Post: 02-07-2011, 12:48 PM
  4. Replies: 7
    Last Post: 11-22-2010, 02:26 PM
  5. Replies: 2
    Last Post: 04-21-2008, 12:43 PM

Posting Permissions

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