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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T03:15:30+00:00 2026-06-02T03:15:30+00:00

I have class SpeexRunner as follows, The constructor of takes two arguments a boolean

  • 0

I have class SpeexRunner as follows, The constructor of takes two arguments a boolean variable and a LinkedList<short[]>. As follows :-

public class SpeexRunner implements Runnable {
    public boolean stopThread;
    LinkedList<short[]> dataList;

    public SpeexRunner(boolean val_stopThread, LinkedList<short[]> dataRef){
        this.stopThread = val_stopThread;
        dataList = dataRef;
    }
    @Override
    public void run() {
     //add objects in dataList;
     // change / remove dataList Objects
    }
}

My Question is:- If I change the dataList inside the run(), will the changes be reflected to the original list which is declared somewhere else ?

  • 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-02T03:15:31+00:00Added an answer on June 2, 2026 at 3:15 am

    If I change the dataList inside the run(), will the changes be reflected to the original list which is declared somewhere else ?

    Yes. Your constructor receives a reference to the list, not a copy of it. If you want to copy it, you’ll have to use the LinkedList copy constructor. Then you’ll have your own copy of the list. But note that the entries on the two lists are still shared, because the entries are arrays (short[]), and arrays are stored by reference.

    This is perhaps best demonstrated by example:

    import java.util.*;
    
    public class ListExample {
        public static final void main(String[] args) {
            List<short[]> list;
    
            // Direct use (no copies)
            list = new LinkedList<short[]>();
            list.add(new short[] { 0, 0, 0 });
            System.out.println("list.size() before direct use: " + list.size());
            System.out.println("list.get(0)[0] before direct use: " + list.get(0)[0]);
            new DirectUser(list).doSomething();
            System.out.println("list.size() after direct use: " + list.size());
            System.out.println("list.get(0)[0] after direct use: " + list.get(0)[0]);
            // Output, note how both the list and its contents have been changed:
            // list.size() before direct use: 1
            // list.get(0)[0] before direct use: 0
            // list.size() after direct use: 2
            // list.get(0)[0] after direct use: 1
    
            // Copying the list, but note that the entries are shared by both lists:
            list = new LinkedList<short[]>();
            list.add(new short[] { 0, 0, 0 });
            System.out.println("list.size() before copy-list use: " + list.size());
            System.out.println("list.get(0)[0] before copy-list use: " + list.get(0)[0]);
            new CopyListUser(list).doSomething();
            System.out.println("list.size() after copy-list use: " + list.size());
            System.out.println("list.get(0)[0] after copy-list use: " + list.get(0)[0]);
            // Output, note how our list didn't change (it doesn't have a new entry), but
            // the entry at index 0 *was* changed:
            // list.size() before copy-list use: 1
            // list.get(0)[0] before copy-list use: 0
            // list.size() after copy-list use: 1
            // list.get(0)[0] after copy-list use: 1
    
            // "Deep" copying, both the list and its entries:
            list = new LinkedList<short[]>();
            list.add(new short[] { 0, 0, 0 });
            System.out.println("list.size() before deep-copy use: " + list.size());
            System.out.println("list.get(0)[0] before deep-copy use: " + list.get(0)[0]);
            new DeepCopyUser(list).doSomething();
            System.out.println("list.size() after deep-copy use: " + list.size());
            System.out.println("list.get(0)[0] after deep-copy use: " + list.get(0)[0]);
            // Output, note that neither the list nor its entries was affected by the call:
            // list.size() before deep-copy use: 1
            // list.get(0)[0] before deep-copy use: 0
            // list.size() after deep-copy use: 1
            // list.get(0)[0] after deep-copy use: 0
    
    
            System.exit(0);
        }
    
        static class DirectUser {
            List<short[]> items;
    
            DirectUser(List<short[]> items) {
                // DirectUser doesn't copy the list
                this.items = items;
            }
    
            void doSomething() {
                this.items.get(0)[0] = 1;
                this.items.add(new short[] { 2, 2, 2 });
            }
        }
    
        static class CopyListUser {
            List<short[]> items;
    
            CopyListUser(List<short[]> items) {
                // CopyListUser copies the list, but both lists still share items
                this.items = new LinkedList<short[]>(items);
            }
    
            void doSomething() {
                this.items.get(0)[0] = 1;
                this.items.add(new short[] { 2, 2, 2 });
            }
        }
    
        static class DeepCopyUser {
            List<short[]> items;
    
            DeepCopyUser(List<short[]> items) {
                // DeepCopyUser copies the list AND each entry
                this.items = new LinkedList<short[]>();
                for (short[] entry : items) {
                    this.items.add(Arrays.copyOf(entry, entry.length));
                }
            }
    
            void doSomething() {
                this.items.get(0)[0] = 1;
                this.items.add(new short[] { 2, 2, 2 });
            }
        }
    }
    

    When DirectUser used the list, in our calling code we saw changes both to the list (in that it got longer) and to its contents (the first entry’s first slot changed from 0 to 1).

    When CopyListUser used it, it made a copy of the list, so we didn’t see any change to our list in our calling code (it didn’t get longer). But we did see the change to the first entry (because both lists shared the same array object) — the first slot changed from 0 to 1 again.

    When DeepCopyUser used it, it made a copy of the list and a copy of each entry, so things were completely and totally disconnected. No changes to the list or to its items were seen by our calling code.

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

Sidebar

Related Questions

I have class which implements Countable, ArrayAccess, Iterator and Serializable. I have a public
I have class class DateOptTimeType implements org.hibernate.usertype.UserType that works with two columns @org.hibernate.annotations.Type(type =
I have class A: public B { ...} vector<A*> v; I want to do
I have class Fred { public: void inspect() const {}; void modify(){}; }; int
I have class that represents users. Users are divided into two groups with different
I have class like public class ProgressBars { public ProgressBars() { } private Int32
I have: class A { public: B toCPD() const; And: template<typename T> class Ev
I have class User that contains protected constructor and class Account that has access
I have class ObjectController { public: ... template<template<class> class Action, class T> Action<T> *
I have class Person: public class Person : INotifyPropertyChanged { private String name =

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.