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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:22:36+00:00 2026-06-01T16:22:36+00:00

I have this code. But I don’t know how to explain the result: ArrayList<String>

  • 0

I have this code. But I don’t know how to explain the result:

ArrayList<String> first = new ArrayList<String>();
        first.add("1");
        first.add("2");
        first.add("3");

        ArrayList<String> second = new ArrayList<String>();
        second = first;
        System.out.println("before modified:"+second.size());
        second.clear();
        System.out.println("after modified:");
        System.out.println("   First:"+first.size());
        System.out.println("   Second:"+second.size());

The result will be: 3 / 0 /0

The problem I don’t know is: when you assign first = second; so, both first and second array will point to same object (1,2 and 3). after you clear all elements on second array, so all reference between second array and these objects will loose (no problem here).

The thing I don’t know is: but these objects (1,2 and 3) still hold reference to first array. Why first array’s size is 0.

Please explain for me.

Thanks 🙂

  • 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-01T16:22:38+00:00Added an answer on June 1, 2026 at 4:22 pm

    By assigning second = first, there is only one arraylist with two references. The references are the same. So, when call clear using one of the two references (first or second), clear will be performed on the referenced arraylist.

    This is something else than you first thought. It’s not so that assigning the second = first all the references of the strings you added to the first one, will be copied into a new ArrayList object, that would be magic (in Java).

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

Sidebar

Related Questions

I have this code, I know I'm missing something but don't know what. It
I have this code but is not working and I don't know why... function
I have this code, but I don't know how can I make it looks
I currently have this code but it leaks because I don't know how to
I have this code which works well but I don't know if it is
I have this code to draw an ellipse in the screen but I don't
I have this code example, but I don't understand why changing the values in
I have this code, but I don't see where I went wrong here. It
I have this example of code, but I don't understand why this code creates
I have this code and its only returning the first string [0] and errors

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.