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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:22:41+00:00 2026-05-16T00:22:41+00:00

public updateList(lst) { lst += a } List lst = [] updateList(lst) println(lst) This

  • 0
public updateList(lst) {
  lst += "a"
}

List lst = []
updateList(lst)
println(lst)

This prints an empty list. However;

public updateList(lst) {
  lst.add("a")
}

List lst = []
updateList(lst)
println(lst)

, will print “a” as desired.

I always assumed += was the same as .add(), but obviously not. I assume += is creating a new List, whereas .add() only updated the existing List?

  • 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-05-16T00:22:42+00:00Added an answer on May 16, 2026 at 12:22 am

    The first method calls plus on the lst variable

    As we can see from the documentation this will:

    Create a collection as a union of a
    Collection and an Object.

    So a new collection will be returned, and the original lst (outside the scope of this method) will be unchanged. (Obviously, inside this method’s scope, lst will be a new list with one element)

    This can be seen by printing out the result of the updateList method:

    public updateList(lst) {
      lst += "a"  // calls plus, creates a new list, and returns this new list.
                  // lst (outside the context of this method) is unmodified
    }
    
    List lst = []
    println( updateList(lst) )
    

    If you call add, then you call the standard java add method.

    public updateList(lst) {
      lst.add "a"
    }
    

    So the original lst is modified

    An alternative to add would be to use the leftShift operator:

    public updateList(lst) {
      lst << "a"
    }
    

    Which calls add behind the scenes: (code from Groovy trunk source)

    public static <T> Collection<T> leftShift(Collection<T> self, T value) {
        self.add(value);
        return self;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

public static void main(String[] args) { List<? extends Object> mylist = new ArrayList<Object>(); mylist.add(Java);
public static void main(String[] args) { int i = 0; while(i==0){ System.out.println(Possibilities: R, T,
public void openOptions() { Intent intent=new Intent(this,Game.class); //intent.putExtra(razmer, level); startActivity(intent); // Intent intent=new Intent(this,Options.class);
public void valueChanged(TreeSelectionEvent event) { //Add images depending on selection. String selection = navigation.getLastSelectedPathComponent().toString();
public class IdAsync extends AsyncTask<String, Void, Void> { AlertDialog alertDialog = new AlertDialog.Builder(MainClass.this).create(); protected
I have a class as follows: Public Class Courses Public CoursesOfferedMAIN As New List(Of
public List findCatalog() { Query query = getEntityManager().createQuery(SELECT pc.productCatalog, p.name, p.product FROM ProductCatalog pc
public String getContact(String searchName) { SQLiteDatabase db = this.getReadableDatabase(); String[] args = { searchName
I have the following code class sample { public void update(List l) { l
I feel stupid asking this but I am. The line List<HasId> ids = list

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.