Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8372141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:20:53+00:00 2026-06-09T14:20:53+00:00

Consider this sample code: List<String> myList = new ArrayList<String>(7); myList.add(5, Hello); myList.removeAll(Collections.singleton(null)); System.out.println(myList.size() +

  • 0

Consider this sample code:

List<String> myList = new ArrayList<String>(7);
myList.add(5, "Hello");
myList.removeAll(Collections.singleton(null));

System.out.println(myList.size() + " objects:" );
for (String s : myList) {
    System.out.println("\t" + s);
}

myList is initialized with an initial capacity of 7, then the next line attempts to add the String “Hello” at position 5. This throws an IndexOutOfBoundsException:

Exception in thread “main” java.lang.IndexOutOfBoundsException: Index: 5, Size: 0

I looked over this question about what “initial capacity” means in terms of an ArrayList. I understand that this particular constructor is allocating room for 7 String elements, and if we try to add 8 elements to the list it’ll have to allocate more room.

What I don’t understand is why it doesn’t create an “empty” list of size 7, with null values at each index, similar to what would happen if we declared String[] myArray = new String[7]. I recall learning that ArrayList is Java’s implementation of a dynamic array, so I’d expect a similar sort of behavior. If I don’t actually have space for 7 Strings allocated when I declare new ArrayList<String>(7), what is actually happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T14:20:55+00:00Added an answer on June 9, 2026 at 2:20 pm

    What I don’t understand is why it doesn’t create an “empty” list of size 7, with null values at each index, similar to what would happen if we declared String[] myArray = new String[7].

    That would be useful in some cases… and not useful in others. Quite often you have an upper bound of the size of list you’re going to create (or at least a guess) but then you populate it… and you don’t want to have a list which then has the wrong size… so you’d have to maintain an index while you “set” values, and then set the size afterwards.

    I recall learning that ArrayList is Java’s implementation of a dynamic array, so I’d expect a similar sort of behavior.

    No, it’s really not. It’s a list which can be resized and uses an array behind the scenes. Try not to think of it as an array.

    If I don’t actually have space for 7 Strings allocated when I declare new ArrayList<String>(7), what is actually happening?

    You do have space for 7 string references. The buffer size (i.e. the capacity) is at least 7, but the logical size of the list is still 0 – you haven’t added anything to it. It’s like you’ve got a sheet of paper that’s long enough for 7 lines, but you haven’t written anything yet.

    If you want a prefilled list, you can easily write a method to create one:

    public static List<T> createPrefilledList(int size, T item) {
        ArrayList<T> list = new ArrayList<T>(size);
        for (int i = 0; i < size; i++) {
            list.add(item);
        }
        return list;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I encountered some troubles with WeakHashMap. Consider this sample code: List<byte[]> list = new
Consider this code sample: public class Human { public string Value { get; set;}
Consider this sample class, class TargetClass { private static String SENSITIVE_DATA = sw0rdfish; private
Consider this code sample: public abstract class Parent { public int val; public Parent()
Consider this code: #include <iostream> using namespace std; typedef int array[12]; array sample; array
Consider the sample code below, i need to convert the list nvlist back to
Consider this sample code: <div class=containter id=ControlGroupDiv> <input onbeforeupdate=alert('bingo 0'); return false; onclick=alert('click 0');return
consider this baby simple piece of code pg.connect( { user: 'hhope', password: '...', port:
consider this simple function def foo(l=[]): if not l: print List is empty else
Consider this simple example - public class Person { private String name; private Date

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.