• Post Reply Bookmark Topic Watch Topic
  • New Topic

print contents of arraylist in object  RSS feed

 
tim bean
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a class that creates an object made of 2 strings and an arraylist


this object then gets returned to an arraylist of these objects

my question is how can i then get the arraylist from this object to print its contents?
thanks for replys brain is twisted here i know iv done this before but cant seem to figure it out
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use variable names which mean something. And avoid underscores except in CONSTANT_NAMES.

You are getting all confused. You have a List as a field for each instance. And you are immediately overwriting that List with another List, we know not whence. If you consider the notion of Single Responsibility, that an object takes care of itself, does that include adding itself to a List? I would have thought not, but others might disagree. you should have some other object which is responsible for all those objects and adds them to the List. At least that is what I think.Note that by adding things to a List, you may be breaching encapsulation; if you add the Foo from mySomething and call methods on the elements of the List, you may change the state of mySomething too. The answer is in Effective Java™ by Joshua Bloch, and is discussed here.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me format and fix this up a little so my brain doesn't hurt:

I agree with Campbell about the names but I'm not sure you guys are on the same page about what you want to do. As I understood your question, you'd like to know how you can get the cons list, presumably a contact list of some sort, from a User object and print it out. Is that right?

There are a number of possible ways you can do that, the simplest being to provide an accessor or getter method for the cons field. This way is also the most likely to break encapsulation of the class. We can go into that and other possible approaches later if you're interested in knowing more.

Going back to the names thing: It looks like your intent is to have each instance of this class represent a single User, correct? If so, the class name really should be singular, not plural. The plural form just makes your code confusing to read. Which of the following is grammatically correct?

1. "This Users has an IP of whatever and a name of Mary."
2. "This User has an IP of whatever and a name of Mary."

The second one, right?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:If you consider the notion of Single Responsibility, that an object takes care of itself, does that include adding itself to a List? I would have thought not, but others might disagree. You should have some other object which is responsible for all those objects and adds them to the List. At least that is what I think.

Enter the devil's advocate ... (that first one's meant to be a demon, not a PO'd guy)

You have to be careful that this kind of thinking doesn't lead to an Anemic Domain Model. Right off the bat, I can think of an example where you might want an object to know how to manage itself and yet also have it make sense for it to help manage a list or two. Let's say we have these classes:

It's not inconceivable to have code like this:

With this code, you'd expect an Animal to keep track of which party they are currently attending, then call the appropriate Party's checkIn() and checkOut() methods when they go to crash() a different Party.
 
tim bean
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
k thanks for replies there all helpful however i i knew i was being particularly stupid today YOU USE GETTERS stupid stupid me thanks again for replys
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!