Solved

Keeping a reference to a list

Posted on 2007-11-29
3
196 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now