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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:44:21+00:00 2026-06-04T10:44:21+00:00

Currently I’m using an ArrayList to store a list of elements, whereby I will

  • 0

Currently I’m using an ArrayList to store a list of elements, whereby I will need to insert new elements at specific positions. There is a need for me to enter elements at a position larger than the current size. For e.g:

ArrayList<String> arr = new ArrayList<String>();
arr.add(3,"hi");

Now I already know there will be an OutOfBoundsException. Is there another way or another object where I can do this while still keeping the order? This is because I have methods that finds elements based on their index. For e.g.:

ArrayList<String> arr = new ArrayList<String>();
arr.add("hi");
arr.add(0,"hello");

I would expect to find “hi” at index 1 instead of index 0 now.
So in summary, short of manually inserting null into the elements in-between, is there any way to satisfy these two requirements:

  • Insert elements into position larger than current size
  • Push existing elements to the right when I insert elements in the middle of the list

I’ve looked at Java ArrayList add item outside current size, as well as HashMap, but HashMap doesn’t satisfy my second criteria. Any help would be greatly appreciated.

P.S. Performance is not really an issue right now.

UPDATE: There have been some questions on why I have these particular requirements, it is because I’m working on operational transformation, where I’m inserting a set of operations into, say, my list (a math formula). Each operation contains a string. As I insert/delete strings into my list, I will dynamically update the unapplied operations (if necessary) through the tracking of each operation that has already been applied. My current solution now is to use a subclass of ArrayList and override some of the methods. I would certainly like to know if there is a more elegant way of doing so though.

  • 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-04T10:44:22+00:00Added an answer on June 4, 2026 at 10:44 am

    Your requirements are contradictory:

    … I will need to insert new elements at specific positions.

    There is a need for me to enter elements at a position larger than the current size.

    These imply that positions are stable; i.e. that an element at a given position remains at that position.

    I would expect to find "hi" at index 1 instead of index 0 now.

    This states that positions are not stable under some circumstances.

    You really need to make up your mind which alternative you need.

    • If you must have stable positions, use a TreeMap or HashMap. (A TreeMap allows you to iterate the keys in order, but at the cost of more expensive insertion and lookup … for a large collection.) If necessary, use a "position" key type that allows you to "always" generate a new key that goes between any existing pair of keys.

    • If you don’t have to have stable positions, use an ArrayList, and deal with the case where you have to insert beyond the end position using append.

    I fail to see how it is sensible for positions to be stable if you insert beyond the end, and allow instability if you insert in the middle. (Besides, the latter is going to make the former unstable eventually …)

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

Sidebar

Related Questions

Currently using a MySQL database Wanting to insert a new row into a table
Currently I insert a new relationship by everytime checking, if it doesn't exist: unless
Currently I'm using s3cmd ls s3://location/ > file.txt to get a list of contents
Currently developing an application using the newest version of symfony, obtained through PEAR. This
Currently my query is very heavy, the table (table A) I need to update
Currently i'm having trouble using two functions in my -(void)viewDidLoad , both of these
Currently I am using Amazon Cloudfront to service static objects on my ASP.Net MVC3
Currently, Enum.Parse supports only the comma as the value separator, so that MemberOne,MemberThree will
I want use html5's new tag to play a wav file (currently only supported
Currently using Oracle/BEA/Plumtree ALUI Portal 6.1, and am trying to display a client's IP

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.