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

Thread: ArrayList initial capacity problem (Index out of bounds Exception)

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default ArrayList initial capacity problem (Index out of bounds Exception)

    package newpackage;
     
    import java.util.ArrayList;
     
    public class NewClass {
     
        public static void main(String[] args) {
     
            ArrayList list = new ArrayList(5);
     
            list.add(1, "ASD");
     
            System.out.println(list.size());
     
        }
    }

    whats wrong with the initialization of the arraylist object? its a basic assumption to anyone who will read the code,
    a new ArrayList object is initialized with an initial capacity of 5, but why it doesnt have an initial capacity? i printed out the size of the array but the size was 0 , whats wrong?


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: ArrayList initial capacity problem (Index out of bounds Exception)

    Take a look at the API: ArrayList (Java Platform SE 6)

    public void add(int index, E element)
    Throws:
    IndexOutOfBoundsException - if the index is out of range (index < 0 || index > size())

    Basically, capacity != size.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    chronoz13 (June 20th, 2011)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList initial capacity problem (Index out of bounds Exception)

    You need to read the API doc for the ArrayList's add method. It lists the exceptions that can be thrown and gives the reasons they are thrown. Your case is described there on the last line.

  5. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: ArrayList initial capacity problem (Index out of bounds Exception)

    i guess the problem is how i look on the "words" itself (grammar)

    capacity = limit of what it can hold (if im right)
    size = physical dimension

    i just took a little bit look on the API, and i cant find a direct solution for the problem i need to deal with,

    actually, the code i posted is the problem i encountered on the part of a program im writing at the moment

    i need to initialize an array with a specific size and insert something on the middle or somewhere in an index

    is there a simple way to do this?

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: ArrayList initial capacity problem (Index out of bounds Exception)

    insert something on the middle
    The class you are using does not support that. It doesn't like empty slots.
    If you use an array not an ArrayList, you can add items anywhere you want.

  7. The Following User Says Thank You to Norm For This Useful Post:

    chronoz13 (June 20th, 2011)

  8. #6
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: ArrayList initial capacity problem (Index out of bounds Exception)

    The class you are using does not support that. It doesn't like empty slots
    oh so theres the answer!! thanks it clears out the confusion , thanks so much!
    i still have a way to insert something in a middle of an arraylist, i was planning when i was waiting for a clarification thanks thanks!!

Similar Threads

  1. 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
  2. Array index out of bounds
    By fortune2k in forum Collections and Generics
    Replies: 1
    Last Post: November 30th, 2010, 07:11 AM
  3. 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
  4. applet + swing + arraylist = exception??
    By wolfgar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 1st, 2010, 11:38 PM
  5. Index Out Of Bounds
    By chronoz13 in forum Collections and Generics
    Replies: 1
    Last Post: December 28th, 2009, 12:19 PM