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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:33:27+00:00 2026-06-11T11:33:27+00:00

I wrote a function to process some ArrayList, and want to pass out the

  • 0

I wrote a function to process some ArrayList, and want to pass out the lists to outer caller. But I found the list was not changed after calling the function.

Here are some test code (of coz, it’s a demo but illustrates my problem):

@Test
public void testList() {
    List<Integer> l1 = new ArrayList<Integer>(5);
    List<Integer> l2 = new ArrayList<Integer>(5);
    l1.add(11); l1.add(12); l1.add(13); l1.add(14); l1.add(15);
    l2.add(21); l2.add(22); l2.add(23); l2.add(24); l2.add(25);
    System.out.println("outer l1: " + l1);
    System.out.println("outer l2: " + l2);

    subList(l1, l2);  //call the function

    System.out.println("after l1: " + l1);
    System.out.println("after l2: " + l2);
}

protected static void subList(List<Integer> l1, List<Integer> l2) {
    l1.remove(0);  l1.remove(0);
    l2 = l2.subList(0, 4);
    System.out.println("inner l1: " + l1);
    System.out.println("inner l2: " + l2);
}

And the result is:

outer l1: [11, 12, 13, 14, 15]
outer l2: [21, 22, 23, 24, 25]
inner l1: [13, 14, 15]             (in function, changed)
inner l2: [21, 22, 23, 24]         (in function, changed too)
after l1: [13, 14, 15]             (changed)
after l2: [21, 22, 23, 24, 25]     (unchanged?!)

It seems that List2 has not changed by function subList(List l1, List l2), while List1 changed.

Is the List parameter passed by CopyValue?? So, in the function, when l2’s reference changed, JVM copy a new list, the original outer list is maintain old value?

How to write a “side-effect” procedure to modify ArrayList ?

  • 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-11T11:33:29+00:00Added an answer on June 11, 2026 at 11:33 am

    References are always passed by value in Java. So, assigning a new value to l2 in the called method won’t change the reference in the calling method.

    You need the following code for it to work:

    List<Integer> subList = new ArrayList<Integer>(l2.subList(0, 4));
    l2.clear();
    l2.addAll(subList);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to pass some data around threads but want to refrain from using
I wrote this function to make columns sortable. I want to rearrange divs based
I wrote a function to match fingerprint templates using VC++.NET. Now I want to
I need to write a function to retrieve and process some data. This data
I wrote a little powershell function that executes Get-EventLog against remote servers. On some
I want to write a function for process sequence, like this: def wrap[A >:
I wrote this function to inject DLL into running process: DLL_Results CDLL_Loader::InjectDll() { DWORD
(VB.NET, .NET 3.5) I wrote the following function to read some text from txt
I wrote function onclick of a element <div class=left_collapsed>Որոնում</div> <div class=container style=display: none;> <ul>
I wrote following function void validateUser(void) { string uName; string uPassword; char c; map

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.