• Post Reply Bookmark Topic Watch Topic
  • New Topic

Print all objects  RSS feed

 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a custom class like the below:



How do I write a method that prints the name of all students in the class?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you think you'd do it? Describe it in English, without worrying about anything Java-related. If you had a group of students, and you wanted to find out which of them were in a particular class, how would you do it? Be as precise as possible, and use small, simple steps.
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Jeff,

I don't want to find anything about which class the student is in. I just want a method that can print the list of names of all Student objects in that class.

Thanks,
Prasanna
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:Hello Jeff,

I don't want to find anything about which class the student is in.


Yes you do.

I just want a method that can print the list of names of all Student objects in that class.


And how do you think you'll do that without asking a student which class he's in?
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I don't know if my mind has completely blanked out now, or if I am just misstating. I don't seem to understand this.

What if the class has only one attribute name and that I want to print the names of all student objects?
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am just trying to add a method that prints

A, B as the output.



Please help me understand this.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:What if the class has only one attribute name and that I want to print the names of all student objects?


Okay, that's a little different from what I thought you meant. When you said you wanted to "print out all the students in the class," I assumed you meant all the students in a particular class. That is, if some students had class set to "math" and some to "history", you wanted to print out all the "math" students.

But now you're saying you want to print out all the students?

It's impossible to say precisely how to print them all out, since all you've shown us is a very tiny snippet of a very minimal student class. You haven't said anything about where "all" the students are. I assume they're in an array or collection. If that's the case, then you just need to iterate over the array or collection and print each student as you get to it--or use one of the methods in java.util that does that for you.

Really, though, you need to TellTheDetails(⇐click) about what you're actually trying to accomplish and what specific part of it is giving you trouble.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:I am just trying to add a method that prints

A, B as the output.



You can either override Person's toString() method, and then just print out p1 and p2, or you can print out p1.name and p2.name.

Again, though, it's not clear what you're actually having problems with. Have you done Hello World yet? Do you know how to print something out? Do you know how to access the members of an object (which you shouldn't generally do from outside that class, but you gotta start somewhere)?

 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Jeff! Yes, I tried out the iterator and was able to make it work. But, if you want to read all the objects' names before you store those objects in a collection, is there a way to do it?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:Thank you, Jeff! Yes, I tried out the iterator and was able to make it work.


You're welcome! I'm glad you got it working!

But, if you want to read all the objects' names before you store those objects in a collection, is there a way to do it?


Of course there is, although it's not clear exactly what you mean by that or exactly what trouble you're having doing it.

Do you know how to read one object's name? If so, then what problem are you having reading all of them?
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know how to read an object's fields using the dot operator. This is a dumb question, but please bear with me.

I know we can use iterator when the objects are stored on a collection. But where are they stored when we just create them and before we add them to any collection? Is there any way to print all the names before adding the objects to any collection?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:I know how to read an object's fields using the dot operator. This is a dumb question, but please bear with me.


Assming the field is accessible--for instance it's public, or you're in the same class where it was defined--you can do something like this:


Normally though, if we access another objects field directly at all (which in general we try to avoid in OO programming), it would be through getter and setter methods. You can google for more details and examples on those.

I know we can use iterator when the objects are stored on a collection. But where are they stored when we just create them and before we add them to any collection? Is there any way to print all the names before adding the objects to any collection?


They're store in whatever variable you put them in. You can print all those variables.



However, you can't do this:


And you can't do this:


 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They're store in whatever variable you put them in. You can print all those variables.




OK, I understand this. Now, what are we doing when we are storing these in a collection? Are we just storing all the foo variables in a collection because we can then use the iterator etc. which gives us a way to loop through all the objects easily and because there is no other way for us to do it other than using a collection? Or, for what other reasons do we store in collection?

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prasanna Raman wrote:Now, what are we doing when we are storing these in a collection? Are we just storing all the foo variables in a collection because we can then use the iterator etc. which gives us a way to loop through all the objects easily and because there is no other way for us to do it other than using a collection? Or, for what other reasons do we store in collection?


Yes, collections are useful for iterating. That is, for when we want to say: "For each one of these things here, do X."

We use collections when we want to store a large and/or unknown number of the same type of thing.

If there are just 1 or 2 or 3 of something, and we know it will always be that many, then we can easily use 1 or 2 or 3 variables, and just deal with them individually. Beyond that, it gets to be unwieldy.

When we want to say, "There will be some number of this thing, and it might be 0 or 1 or 2 or 100, and for each one of those things, I want to do something to it," that's when we use a collection.

For example, say you're writing an email app. The user provides the "To:" addresses. There might be 0 or 1 or 2 or 100 of them. Or more. You're not going to have:


Are you?

Or do you want to say, "For each email address in the 'To:' line, send a copy of this email to that address."
 
Prasanna Raman
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I am not! Thank you very much for persisting with me through some of my dumb questions, Jeff!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome!

They're not dumb questions. Programming requires learning not only terminology and language syntax, but new ways of thinking and looking at problems as well. It can take a while to get used to it.
 
Campbell Ritchie
Marshal
Posts: 56599
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know whether you have enough experience to know about Maps, but a Map<String, List<Student>> can be used to record the Student objects by class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!