Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Keeping a reference to a list

Posted on 2007-11-29
3
222 Views
Last Modified: 2010-03-30
Hi,

I have some class that loads an array list. I want to let another class instance reference it, but don't want to make a whole copy of it. Something like:

class Loader {
    public void Load()
    {
         m_List.add(new something());
    }
    ArrayList<something> m_List;
}

class Other {

     public void SetListReference(ArrayList<something> theList)
     {
           // just want to keep a reference to theList here.
          m_ListRef = theList;
     }
     ArrayList<something>& m_ListRef;
}


Something like that,

Thanks
0
Comment
Question by:DJ_AM_Juicebox
  • 2
3 Comments
 
LVL 17

Accepted Solution

by:
contactkarthi earned 500 total points
ID: 20374426
try something like this
class Loader {
    public void Load()
    {
         m_List.add(new something());
    }
    ArrayList<something> m_List;
    
    public ArrayList<something> getListReference()
    {
		return m_List;
	}
    
}
 
class Other {
 
     public void SetListReference(ArrayList<something> theList)
     {
           // just want to keep a reference to theList here.
          m_ListRef = theList;
     }
     ArrayList<something>& m_ListRef;
     
     public static void main(String args[])
     {
		 Other oth = new Other();
		 
		 Loader loader = new loader();
		 loader.Load();
		 
		 oth.SetListReference(loader.getListReference());
	}
}

Open in new window

0
 
LVL 1

Expert Comment

by:dankuck
ID: 20374497
In Java, all object variables are references.

The code you have supplied would work if you remove the C++ ampersand:

ArrayList<something>& m_ListRef;

becomes

ArrayList<something> m_ListRef;

The code in SetListReference will not copy the ArrayList elements into a new ArrayList, it will just put the original ArrayList reference into the new variable m_ListRef.  Then any changes you make to the ArrayList within class Other will be reflected when class Loader uses the ArrayList.
0
 
LVL 1

Expert Comment

by:dankuck
ID: 20383922
You'll need to remove the & on contactkarthi's line 22.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
custom annotations 9 42
Should localization be done inside spring controller 5 32
windows explorer path to command prompt 5 54
collection output issue 9 36
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question