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 7496647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:42:38+00:00 2026-05-29T18:42:38+00:00

I am trying to do something like: ArrayList<String> al = new ArrayList(15); al.add(5, test);

  • 0

I am trying to do something like:

 ArrayList<String> al = new ArrayList(15);
 al.add(5, "test");

my case with vector but it doesnt matter. I am missing something trivial.

However I get index out of bound exception.

Method api description:

“Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). ” – so?

My situation is that I get UDP packets and try to place their data them into structure according to header sequence number.

Update: same happens with .set

  • 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-05-29T18:42:48+00:00Added an answer on May 29, 2026 at 6:42 pm

    For beginners the constructor of ArrayList is confusing.
    Putting

     new ArrayList(15);
    

    is basically just an optimization of your program upfront. The class ArrayList is basically just a wrapper around a native Java array. In this example the actual internal array will be created with 15 elements. When you put more than that, the ArrayList class just creates a bigger array and copies over the contents from the original array. Putting an initial capacity does not make sense very often. I only use it when I know from the beginning that I will story a huge number of elements in the ArrayList (say one million).

    I know, this behavior is very confusing, especially if you are coming from a language like PHP, where you can just do:

    al[] = Array();
    al[15] = "I am a fifteen year old UDP package";
    

    Basically the add(index, element) method just works if you have an ArrayList that contains more than index elements, squeezing the new element in after index-1.

    For the solution:

    If you know that you will be receiving 15 elements max, it is perfectly ok to use an ordinary array like so:

    String[] al = new String[15];
    al[5] = "Tralala";
    

    If you don’t know the number of elements I would store them in a List and sort that list after all packages have been received.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying something like this, but this example does not work. jsObj = {};
I've trying to achieve something like this: class Base { public: Base(string S) {
I am trying to create a memory game in Java. Something like this, but
I'm trying something like this Output.py print Hello Input.py greeting = raw_input(Give me the
I'm trying something like this: [ServiceContract ( CallbackContract = typeof (CallbackContract_1), CallbackContract = typeof
As the title goes I am trying something like $(ul li:nth-child(' + xx +
Is it possible to set for(AssociatedControlID) attribute using jQuery? I am trying something like
I'm trying to implement something like this: <div> <table> <thead> <tr> <td>Port name</td> <td>Current
I am trying to so something like Database Design for Tagging , except each
I'm trying to do something like this in Java: public static <T> T foo()

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.