Solved

Keeping a reference to a list

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping1 challenge 7 71
firstswap challenge 20 65
Why method in Java which is called from Runnable run() doesn't need to be 'static'? 1 22
servlet doXXX methods 3 31
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

947 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

22 Experts available now in Live!

Get 1:1 Help Now