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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:09:48+00:00 2026-06-08T02:09:48+00:00

Using this Java code: // create the items list List<Item> items = new ArrayList<Item>();

  • 0

Using this Java code:

    // create the items list
    List<Item> items = new ArrayList<Item>();
    // ... (add some elements into the list so that it is not empty)

    // iterating ArrayList<Item> from right to left we find the position
    // based on the `if` condition satisfied for an item property
    int pos = 0;
    for (int j = items.size() - 1; j >= 0; j--) {
        Item item = items.get(j);
        if (item.property <= 7) {
            pos = j + 1; break;
        }
    }

    // add new item on the found above position
    Item it = new Item();
    if (pos == items.size()) {
        items.add(it);
    } else {
        items.add(pos, it);
    }

I’m wodering if this statement Item item = items.get(j); will take some extra time for execution because of the ArrayList used. For example, imagine we need to add the new item to the end, then by calling get() on the items list will iterate it from the left only, which is redundant. I would expect to use Deque structure instead of ArrayList.

What could you recommend, maybe I’m wrong at all, because new elements can be added in the beginning as well, though the goal is to iterate from the right side to the left.

  • 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-08T02:09:51+00:00Added an answer on June 8, 2026 at 2:09 am

    For an ArrayList, the operations get(i), iteration and add/remove at the (right) end are cheap (basically O(1) or amortized O(1)). However, adding/removing elements at the other (left) end are very expensive and involve copying the whole backing array each time.

    So if you need to add and remove at both ends, be sure to use an ArrayDeque. This is basically as cheap as an ArrayList for all operations, but supports adding at both ends in a cheap way.

    Note that if you only want to add/remove at the beginning of the list, not at the end, you can still use an ArrayList and just it “backwards” (so when you want to add something in the beginning, you just add it at the end, and when you want to iterate right-to-left, you actually iterate left-to-right).

    Inserting elements with the add(Object, int) method is O(n) for both data structures (all elements to the right of the inserted element need to be moved in the array).

    An alternative could be to use LinkedList (which also implements Deque). You just need to be sure that you do not use get(int) or any other method where you pass an index as an int, because this is O(n). However, add/remove/insert operations are O(1) at all positions in the list (as long as you already have an iterator pointing to the position). So for the loop in your code, call .listIterator() on the list, iterate appropriately and use the ListIterator.add(Object) method to insert the element. This would be cheapest solution for your code overall.

    Edit

    I hadn’t noticed that ArrayDeque doesn’t offer the method to insert elements in the middle (although it could, just like ArrayList). (Thanks A.H.) So if you really need all these operations (insertion in the beginning, middle and end), use LinkedList and ListIterator.

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

Sidebar

Related Questions

The pasted below code i am using to create dropdown list and java script
I am using iTextSharp in VB.net. I have this piece of code from java
I'm running a java code using Apache Avro. Some code gets deprecated in the
This is my Java code line which I am using to pass the data
Basically, I need to add items to the arraylist. Am I correct in using
I'm using this java script library to show ratings http://www.fyneworks.com/jquery/star-rating/#tab-Overview . I'm using below
Using AmazonSimpleEmailServiceClient.sendEmail() ; as per java docs All service calls made using this client
What is the best practise for using the this keyword in Java? For example,
Below is my Oracle Procedure. When i call this procedure using java, it throws
I found this Is Sun Java really necessary for using Android SDK on Ubuntu

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.