Skip to main content

Reading Arraylist of objects defined as private in another class

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
2 replies [Last post]
Frantech
Offline
Joined: 2012-10-08

Hi,

I'm a bit of a newbie to Java and I'm stuck on a problem. Could someone help me please ?
I've posted just the relevant sections of some code (which is quite long).

The problem is:

How to read objects that are defined in an arraylist that is defined as private in another class. The other class has a public get method which returns an arraylist. I've used that, but I don't know how to access the objects inside the returned arraylist.

Please see the attachment at "HERE".

It's close to midnight here so I'll check for responses in 7 hours time...

Thanks in advance...

AttachmentSize
java.net_post.txt1.35 KB

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Frantech
Offline
Joined: 2012-10-08

Amazing what nights sleep will do...

The answer is:

1) use the other classes get method to return the private arraylist defined in the other class.
2) use the arraylist.get() to return the object in the array
3) cast the object as an object type needed
4) assign that object type to an instance variable of that type in the class you are in
5) you now have access to the classes attributes

code like:
ArrayList a1 = vc.getCandidates();
theCandidate = (Candidate)a1.get(0);
System.out.println(theCandidate.name);

Thanks to those who took a look at this. It really had me stumped.

Frantech
Offline
Joined: 2012-10-08

Need to set this as answered... can anyone tell me how to do that please ?