Solved

Keeping a reference to a list

Posted on 2007-11-29
3
233 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
[X]
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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

732 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