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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:16:46+00:00 2026-06-17T12:16:46+00:00

Is it possible to add a string to beginning of String array without iterating

  • 0

Is it possible to add a string to beginning of String array without iterating the entire array.

  • 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-17T12:16:47+00:00Added an answer on June 17, 2026 at 12:16 pm

    The only way to do this is to maintain a ring buffer. i.e. you have a counter which remembers where the start is and you move that instead of moving all the entries in the array. This only works because you re-define what the “start” means.

    See the source for ArrayDeque which has three fields

       86       /**
       87        * The array in which the elements of the deque are stored.
       88        * The capacity of the deque is the length of this array, which is
       89        * always a power of two. The array is never allowed to become
       90        * full, except transiently within an addX method where it is
       91        * resized (see doubleCapacity) immediately upon becoming full,
       92        * thus avoiding head and tail wrapping around to equal each
       93        * other.  We also guarantee that all array cells not holding
       94        * deque elements are always null.
       95        */
       96       private transient E[] elements;
       97   
       98       /**
       99        * The index of the element at the head of the deque (which is the
      100        * element that would be removed by remove() or pop()); or an
      101        * arbitrary number equal to tail if the deque is empty.
      102        */
      103       private transient int head;
      104   
      105       /**
      106        * The index at which the next element would be added to the tail
      107        * of the deque (via addLast(E), add(E), or push(E)).
      108        */
      109       private transient int tail;
    

    So adding to the start works like this

      224       public void addFirst(E e) {
      225           if (e == null)
      226               throw new NullPointerException();
      227           elements[head = (head - 1) & (elements.length - 1)] = e;
      228           if (head == tail)
      229               doubleCapacity();
      230       }
    
    
      312       /**
      313        * @throws NoSuchElementException {@inheritDoc}
      314        */
      315       public E getFirst() {
      316           E x = elements[head];
      317           if (x == null)
      318               throw new NoSuchElementException();
      319           return x;
      320       }
    

    Note: it moves the head rather than shifting all the elements down the array.

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

Sidebar

Related Questions

Would it be possible to add a new operator to the String class that
Is it possible to add an Argument to an python argparse.ArgumentParser without it showing
I am trying to determine if it is possible to add a concatenated string
Possible Duplicate: Append an int to a std::string I want to add string and
Is it possible in MySQL to insert a string three characters from the beginning
Is it possible to add a documentation string to a namedtuple in an easy
Is it possible to add a domain string to validation messages in CakePHP models?
Is it possible to add a string with spaces or special characters to an
Is it possible to add empty query string parameters with ASP.NET MVC? I need
With Spring it's possible to add BeanPostProcessor implementations to the context in order to

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.