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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T14:45:48+00:00 2026-05-29T14:45:48+00:00

I am totally confused with ArrayList behavior. Wrote really long post, then realized no

  • 0

I am totally confused with ArrayList behavior. Wrote really long post, then realized no one is going to analyse huge code, so just core of the problem. Numbers are for convenience, but in my app these 0 and 24 are dynamic values.

ArrayList<VoipBlock> sortedBlocks = new ArrayList<VoipBlock>();

VoipBlock vb3 =new VoipBlock();

vb3=sortedBlocks.get(0);

vb3.setPacketNumber(24); 

Essentially my final aim is to: modify and add back to arrayList as new value. However when I do that the guy at position 0 in ArrayList -> unsortedBlocks.get(0); replicates all the changes done to vb3 which of course is not what I want. I want vb3 acquire same values as VoipBlock inside of ArrayList, but I want it to be detached.

This is yet another case of passing by reference. I hate technical explanations – Java passes everything by value, BUT in some cases it passes references by values – this is same as saying not-oily oil. Please help.

It reminds me my start of learning JavaScript – I hated the language – until I watched proper materials at lynda.com – JavaScript Good Practices? – Diagrams killed me. It is the lazy description that turns us-youth away from brilliant technology, not the technology itself.

Please don’t let it bother my stress and don’t be in any way offended by me, it is just general complaining, maybe someone will look at it and make life better 🙂

Thanks for Your time,
Desperately awaiting for help 🙂

  • 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-29T14:45:50+00:00Added an answer on May 29, 2026 at 2:45 pm

    To achieve your objective you can use clone method. you have to override this method in VoipBlock class

    Lets say VoipBlock is as follows

    public class VoipBlock {
    
         private int packetNumber;
         private String type;
    
         public int getPacketNumber() {
          return packetNumber;
         }
    
         public String getType() {
          return type;
         }
    
         public void setPacketNumber(int value) {
           packetNumber = value;
         } 
    
         public void setType(String value) {
           type = value
         }
    
         public VoipBlock clone() {
            VoipBlock clone = VoipBlock();
            clone.setType(this.getType());
            clone.setPacketNumber(this.getPacketNumber());
            return clone;
         }
    
    }
    

    So, using the same code you can do like as follows

    ArrayList<VoipBlock> sortedBlocks = new ArrayList<VoipBlock>();
    
    VoipBlock vb3 =new VoipBlock();
    
    sortedBlocks.add(vb3);
    
    vb3=sortedBlocks.get(0).clone();
    
    vb3.setPacketNumber(24);
    

    Note that upon calling clone method in above code segment, vb3 get assigned with a new VoipBlock instance. And already inserted VoipBlock to the array remains unchanged.

    if you are looking to have kind of sample instances of VoipBlock instances which you later wanted to use in creating similar instances like them. check on immutability/mutability aspect of the code. check “Effective Java” by Joshua Blouch

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

Sidebar

Related Questions

Totally confused by this one... We have a WAMPServer installation set up, running a
I'm totally confused between these 4. What is the difference between ElapsedMilliseconds (long), ElapsedTicks
I'm totally confused which one to use and when, first thing I do when
Okay I'm totally confused on this one. I have a script that receives a
gcc 4.4.2 c89 I re-engineering some code in c89. However, I am totally confused
Totally confused here. I have a PARENT UIViewController that needs to pass an NSMutableArray
I am totally confused between WCF and ASMX web services. I have used a
I'm totally confused on this matter, some say use tables and others say it's
I'm new in the Java world and I'm totally confused with the sheer number
I'm getting totally lost and confused on how to use the new strongly typed

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.