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

  • Home
  • SEARCH
  • 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 8693003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:26:36+00:00 2026-06-13T00:26:36+00:00

I have n objects each of them with an identifying number. I get them

  • 0

I have n objects each of them with an identifying number. I get them unsorted but the range of indexes (0, n-1) is used to identify them. I want to access them as fastest as possible. I suppose that an ArrayList would be the best option, I’d add the object with identifier n at the position of the ArrayList with index n by:

list.add(identifier, object);

The problem is that when I am adding the objects I get an IndexOutOfBounds Exception because I’m adding them unsorted and the size() is smaller although I know that previous positions will also be filled.

Another option is to use a HashMap but I suppose that this will decrease performance.

Do you know a collection that has the behavior described above?

  • 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-13T00:26:37+00:00Added an answer on June 13, 2026 at 12:26 am

    Do you know a collection that has the behavior described above?

    It sounds like you need a plain old Java array. And if you need it as a collection, then use “Arrays.asList(…)” to create a List wrapper for it.

    Now this won’t work if you needed to add or remove elements from the array / collection, but it sounds like you don’t need to from your problem description.

    If you do need to add / remove elements (as distinct from using set to update the element at a given position, then Peter Lawrey’s approach is best.


    By contrast, a HashMap<Integer, Object> would be an expensive alternative. At a rough estimate, I’d say it that “indexing” operations would be at least 10 times slower, and the data structure would take 10 times the space compared to an equivalent array or ArrayList type. A hash table based solution is only really a viable alternative (from a performance perspective) if the array is large and sparse.

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

Sidebar

Related Questions

I have a list of objects and for each of them I want to
You have list of objects and each of them has an id property. Here's
I have a collection of objects which have several properties in each of them.
Let suppose i have 5 objects and each object contains double values. I want
Suppose you have 2 UISwitch objects placed onto your storyboard. Each of them can
[I have 2 objects, both of them subclass of CCSprite. Each one of them
I have two executables that access a DLL containing Data Access Layer objects Each
I have many Topic objects and each Topic hasMany posts:Post How can I order
I have a list of core data objects each has a longitude and latitude
In javascript if I need objects each to have an individual value of an

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.