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 7636915

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:48:22+00:00 2026-05-31T07:48:22+00:00

I just want to merge two ArrayLists and have the contents in one ArrayList.

  • 0

I just want to merge two ArrayLists and have the contents in one ArrayList. Both lists contain the object which is an instance of the same class.
The object reference themselves are different though. However I am getting this unexpected size for the combined arraylist. I use JAVA 1.4

ArrayList a1 = new ArrayList();
ArrayList b1 = new ArrayList();


ClassA cls1A = new ClassA();

ClassA cls1B = new ClassA();


a1.add(cls1A);

b1.add(cls1B);


a1.size()  = 100;

b1.size() = 50;


//merge the two arraylist contents into one

//1st method and its result

 a1.addAll(b1);


 //Expected Result
 a1.size = 150  

 //but 

 //Obtained result
 a1.size = 6789  


//2nd method and its result

 Collections.copy(a1, b1)

 //Expected result
  a1.size() = 150

 //but

 //Obtained result
 a1.size = 6789  

How can I have an ArrayList which displays the combined size??

  • 0 0 Answers
  • 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-31T07:48:23+00:00Added an answer on May 31, 2026 at 7:48 am

    I came up with the following solution. First get the size of both the arraylists, then increase the capacity(by using ensureCapacity method of ArrayList class) of the arraylist to which the two needs to be merged. Then add the objects of the 2nd arraylist from the last index of the first arraylist.

         a1.add(cls1A);
         b1.add(cls1B);
            int p = a1.size();
    
            int w = b1.size();
    
            int j = (p + w);
    
            a1.ensureCapacity(j);   
    
    
    
            for(int r = 0; r <w; r++)
            {
    
            ClassA cls1B = new ClassA();
            Object obj  = b1.get(r);                
            cls1B = (ClassA)obj;
            a1.add(p, cls1B);
            p++;
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just want to ask how to merge two tables. Because I have a
I have two images that I want to merge into one. (Eg House.png on
Simple idea: I have two images that I want to merge, one is 500x500
I just want to clarify one thing. This is not a question on which
Let's say I have two PDF templates created with Adobe Acrobat, which are both
I have two ObservableCollection lists, that i want to unite. My naive approach was
I have a similar question to this one: Merge the contents of 2 cells
I want to merge two CSV files that I have read into python using
I have two separate meshes I want to merge with different materials using the
Possible Duplicate: What is the best method to merge two PHP objects? I have

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.