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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:55:02+00:00 2026-06-16T04:55:02+00:00

I have the following code: ArrayList<String> words; words = new ArrayList<String>(); words.add(is); words.add(us); ListIterator<String>

  • 0

I have the following code:

    ArrayList<String> words;
    words = new ArrayList<String>();
    words.add("is");
    words.add("us");
    ListIterator<String> it;
    it = words.listIterator();
    it.add("##");
    System.out.println(words);
    it.next();
    it.next();
    it.previous();
    it.set("##");
    System.out.println(words);

I would expect that the output would be ## us ##, but when I run the program it returns ## is ##. I expect that this has to do with the ListIterator adding an item to the ArrayList instead of the ArrayList adding an item to its self.

Why does the program behave in this way?

  • 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-16T04:55:03+00:00Added an answer on June 16, 2026 at 4:55 am

    Your question isn’t clear, but I suspect the main point you may be missing is that ListIterator.add inserts at the current location:

    The element is inserted immediately before the next element that would be returned by next(), if any, and after the next element that would be returned by previous(), if any.

    And also:

    The new element is inserted before the implicit cursor: a subsequent call to next would be unaffected

    So after your initial call to it.add("##") the list contains "##", "is" "us". You’re then moving next twice – the first moves the cursor to just after "is" (which is returned). The second moves the cursor to just after "us" (which is returned). Then the call to previous() returns "us" again, and finally a call to set() replaces "us" with "##":

    Replaces the last element returned by next() or previous() with the specified element (optional operation).

    It all looks like it’s obeying the documentation perfectly. Unfortunately it’s not clear which of these steps is confusing you, as you’ve conflated so many in one question.

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

Sidebar

Related Questions

I have the following code: LinkedHashMap<String,ArrayList<String>> h; Set set = h.entrySet(); Iterator i =
I have the following code: List<int> intList = new ArrayList<int>(); for (int index =
I have the following code which works well: Dim dept As New ArrayList Dim
I have the following code: public class TestSynch extends Activity { public static ArrayList<HashMap<String,String>>
I can't figure this out. I have the following code: import java.util.ArrayList; import java.util.List;
I have following Code package cyclist.project; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException;
please have a look at the following code import java.util.ArrayList; import java.util.List; public class
Please have a look at the following code package Euler; import java.util.ArrayList; import java.util.List;
I have following code for updating user's column public void UpdateLastModifiedDate(string username) { using
I have following code. ASPX Page <a href=AnyASPXPageOfWebsite.aspx onclick=javascript:CallJQuery(); > Set Price </a> JS

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.