• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to access an object in an ArrayList of another class?  RSS feed

 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to access an ArrayList of another class?
I have three classes
Class1 <--Main Class to read out the objects and fields from my ArrayList.
Class2 <-- The arrayList that stores a few testObjests with fields.
TestObject <-- The test Object with getter and setters

===Class1===

===Class2===

===TestObject===
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Alex Lucard,

Welcome to JavaRanch, nice to see you here.

Also, please keep in mind, that you need to UseCodeTags (<- link) when you post your code.
I have added them for you this time, see how better it looks now?
 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Hi Alex Lucard,

Welcome to JavaRanch, nice to see you here.

Also, please keep in mind, that you need to UseCodeTags (<- link) when you post your code.
I have added them for you this time, see how better it looks now?


Thank you that does look better.
 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did make a change to the Class1


now my output is
run:
5
TestObject@659e0bfd
TestObject@2a139a55
TestObject@15db9742
TestObject@6d06d69c
.TestObject@7852e922
BUILD SUCCESSFUL (total time: 0 seconds)
but still can not get it to print the two fields "firstName and lastName"
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alex Lucard wrote:How to access an ArrayList of another class?
Please clarify this, what is your intention to do?

Nevertheless, if I understand correct, the answer would be - in the same way as you accessing "firstName" and "lastName" of your "TestObject" class (through getters).
But before that, you need to declare it as a member variable. Maybe remind yourself how it is done here (<- link).
 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What I'm trying to do is
1) Build an object called TestObject with two fields fistName and lastName.

2) Make 5 copies of the TestObject both fields populate firstName, lastName.

3) In a new class access the Objects in the ArrayList and print them out.

In short I'm looking to make an arrayList with N number of the same object in it and have that ArrayList be accessed from a separate class.
I looked at the Declaring Member Variables but still do not know what I need to do.
 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed the Class1 to this



Setup a new class called Class3

 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now when i run it with the new class3 and the change to the Class1 I get the output of
5
TestObject@659e0bfd
TestObject@2a139a55
TestObject@15db9742
TestObject@6d06d69c
TestObject@7852e922
But I still can not access and print the fields in each object.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop changing everything and wait for us to answer. The output you have shown us may mean nothing to you, but we have seen it before. Try this:-
System.out.println(new Object());
… and see what you get. It looks the same, doesn't it? If you look in the Object class for the toString method, it tells you what that means. If you don't override the toString method, that is waht you get.
Solution: write a toString method in the test object class.

And welcome to the Ranch again
 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this working now.
If anyone would like to try this all i had to do was change the Class3

Her is my new and updated Class3.

 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use raw ArrayList.
Change your ArrayList declaration to ArrayList<TestObject>.
And then you won't need to cast myAl1.
https://docs.oracle.com/javase/tutorial/java/generics/why.html
 
Alex Lucard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Paweł Baczyński for your help. I did make the change and it does work.

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why do you have such a meaningless class names?. In two months time you'll look at them and won't be able to remember what these are. Even if it is for test purpose choose meaningful names for classes, methods, variables - it requires some practice also. The class, which contains method "main" suppose to be named in the way, that you could identify it straight away, that it is the one you need to execute.

Look at this 6 years old post (<- link). Quite old, but still can find some good tips in there. As well as link to Java coding conventions page. That document is even older (20.04.1999), but still standards are followed in nowadays.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!