This week's book giveaway is in the Agile/Processes forum.
We're giving away four copies of Building Green Software: A Sustainable Approach to Software Development and Operations and have Anne Currie, Sarah Hsu , Sara Bergman on-line!
See this thread for details.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

ArrayList objects

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have some code examples from HFJ Chap 6:





My question is: in the first example, what you're saying is "make a List of Egg objects," and you're adding two separate egg object, object a and object (or references to object b)

In the second example, is 'a' one object that contains other objects (zero, one, two three), or are there four different 'a' objects (which doesn't make sense to me)?

 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




This is another example of populating an ArrayList:



Does that clear things up?
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Christopher:

An ArrayList is a concrete implementation of the List interface, backed up internally with an array. So, you can think of it as being similar to an array, but better. As Janeice said, the ArrayList will contain zero or more object references, depending on how many references you add or remove.

John.
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, for clarification: I've asked this on another post, but it might be more relevant here, given your last example.

I don't remember if I've already told you about the program I'm writing, but what I want to do is have a user input for four fields of information for my CD collection -- Artist, Album, Label and Year of Release. I'm going to use a loop for each new CD object, which I've called CDRecord. What I want to do is create the first Record (which I guess I'll call CDRecord1) and assign the fields' variables to it -- so it woule be CDRecord1.artist, CDRecord1.albumTitle, etc. When that's finished I want to add it to the array list, which I think I know how to do.

My question is how do I create CDRecord2? Unless, if I'm reading the last example of the array list, I can simply add the new CDRecord. However, it seems to me that if I use the same name for the second object, it will simply point to the first one and just change the values to the most recently entered ones.

But, I want later to print out all the Record entries, and be able to make any edits to specific records from the list before saving it for use at a later date (which I don't know how to do yet, because this project is long term, and a way of applying what I'm learning from HFJ). So, what, if anything, can be done to have the program created the next sequential record, assign its field values to it, and add it to the array list?

Could I assign a counter to the loop, just with a variable like num, and create CDRecord.num and then assign the field values, getting CDRecord1.num.artist, CDRecord.num.albumTitle, etc?
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think this is what you wanna do:



Then, you have all your CDRecord objects in that ArrayList.... ready to be outputted any way you could imagine
You don't need to name them all because they all have a cozy spot in that ArrayList.... you can access them from there (i.e. by using the API).

Remember, if you're finding yourself wanting to name things like Dog1, Dog2, Dog3, and so on.... it might be best to create an array, a list, or a map. Let the JVM worry about keeping track of your objects and you can save your worry for something else.
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's what I want to do.

But...just to make sure I understand:

each time I go through the loop (say five times) and say 'new CDRecord' (which is an object of the CD class in my program), there will be five separate cd objects, each referenced with a variable called "CDRecord"? If so, then how do I distinguish one record from the other - say, number the records so a user can choose which ones he wants to edit or delete, or whatever -- unless I can use one of the methods from the array list class to do that (maybe index?) or am I getting ahead of myself?

I would like to thank you for you help (past, present and future . You express yourself clearly and can understand what I'm trying to say, even if I don't think I express myself clearly sometimes. Muchos Gracias!
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You could theoretically loop through a million times and have a million objects. You reference them through the ArrayList. I find it helpful to have one I call, say, "w" so I can use it as a holder when I need to work with one. I'm sure that's not best practices, but I too am learning.

There's lots of ways to do things to objects that live in collections.

Probably easiest (not necessarily best) is to iterate through them and look for the one you want... or you can iterate through them and display them with their index number and have the user choose that way.

And no problem with the help.... I don't have time to come around here every day, but I try to read things frequently and reply when I can. It's hard being a starving college student

 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, it's been a bit -- hope ya'll had a nice Thanksgiving!

I've been working a bit on my CD listing program, and put together a little test regarding my understanding of Array Lists. Not sure how much I'm getting, but the little bit I have works for now, with one exception:

Here are the bits of code I'm working with:
This is the 'ready bake code' I use to input information:





Here's the code for the CD information:



And here's the test code:



When I compile the CDInputTestDrive code, I get a 'void' type not allowed here. I'm guessing I need a string return type, since I want to print strings. But if I change 'void' to 'string' I'm not sure how to code the return types I tried



But that didn't work. at one point I got 'unreachable statement."

-- I want it to obviously print the artistName and albumTitle. If I rewrite the code and use two separate print statements that just reference the getter methods separately:


the code runs fine. But how do I use the printRecord() method? There's got to be a way.

Also, I don't know how well this code is written, so feel free to offer any helpful tips (still trying to figure out when to use getter and setter methods, but what I have here is what I've seen in HFJ and I'm trying it out to see if I can get a hold of the concept)




 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sometimes "this" works and I dunno really why....

See below:
 
Marshal
Posts: 80479
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Janeice DelVecchio wrote:sometimes "this" works . . .

You should see no difference between getName() and this.getName() as long as the first getName() is intended to use the current object.
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tried 'this' and it didn't work, so I'm still clueless as to why I can't use the regular printRecord()method.

I'm still thinking it has to be 'string' rather than 'void', but can anyone tell me how to code the return types for the method?

 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see it.

46 - 49 in your test drive





A method like "print record" should do exactly that, print the record.

If you want to turn the record into a meaningful String, you should override the toString method.


You could take it one step farther....
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried your first suggestion (changed 46-49)

It worked?

Thanks. Could you explain the logic in your solution regarding the 'toString' method? I'm unclear as to what it does. (I haven't read about "this" yet and don't want to put it into my code; It might confuse me
I was wondering if you could also explain a little further why I couldn't use my original way of coding the method -- I'm unclear maybe as to what 'void' acutally mean/does.


Thanks again.
 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christopher Laurenzano wrote:I tried your first suggestion (changed 46-49)

It worked?

Thanks. Could you explain the logic in your solution regarding the 'toString' method? I'm unclear as to what it does. (I haven't read about "this" yet and don't want to put it into my code; It might confuse me
I was wondering if you could also explain a little further why I couldn't use my original way of coding the method -- I'm unclear maybe as to what 'void' acutally mean/does.


Thanks again.



I need a snack.... this is gonna be a long story....
....
... ok

1. toString Overriding - All objects in Java (even the ones you create) are subclasses of Object. This allows us to utilize some of the most basic and useful methods for every object we create, including equals(), getClass(), and toString() (there are others, check the API). toString() is what you want if you want a String representation of the Object.... go look at what it does in the API. Open another tab. I'll wait.......
....ok. So to make this information useful in your application you can (and typically should) override it. This eliminates the need for typing a billion println statements when you want to output, say, the objects name, album title, year it was made, et cetera. It makes sure that every time the object is printed to the screen it's the same and eliminates excess typing. To override it, all you need to do is create a method in your class with the same return type (String), name (toString) and parameters (none).

2. "this" as Campbell said, is used to identify that you are using "this" instance of the object. I don't really know why it made my application work correctly (or at this point whther it did or not). I use it to reiterate in my coding (to myself, my professor, my classmates, and others) that I am referring to THIS instance of the object and not some other instance.

3. The reasoning for the mishap in your program.... so campbell said that the "this" syntax wasn't going to work. There was nothing wrong with the method, logically or synctactically, so I looked back at where you called it. It was inside the System.out.println() method, as a parameter. That method takes a String as a parameter, not a void (we'll get to that), so it wasn't going to work. So, the call to the method had to be changed (with a toString() override and use of a new method, or the use of the printCD() method.... or whatever its name was). The easiest way was to just use the method you had already created, so we took it out of the println() and it worked.

4. void - we say it all the time as newbies. Do we know what it means? Not until someone tells us. It means the method should not return a value. That's it. That method signiture is very important, it has the access modifier, the return type, the name, and the parameters expected. That is, if you have a return type and don't return anything, the compiler is going to get mad. You need to return something with any return type other than void. If you don't think the method should return anything, it's a void. Otherwise, you figure out what it should return (like the getters all have a return type that matches what they are created to "get"). You do not have to do anything with the return value if you create one. It just matters that it's there.

I suppose the return type also means that the method can be used in the place wherever something of that type is expected:


Alright....
.... did we cover everything?
 
Christopher Laurenzano
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Janeice.

I hope I didn't ask too much -- I realize you're very busy, and I do appreciate your taking time to help out.

 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No big deal... I'm procrastinating a bit today, and it helps me to focus on things I know rather than to stress out about unknowns.

I'm glad I could help.
 
Campbell Ritchie
Marshal
Posts: 80479
455
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done, Janeice, only one tiny nit-pick. I can't remember what you call the method heading officially (try the JLS)

public final String getName(int number)

but officially the signature is the method name and types of parameters, as underlined, as you see here in the JLS, <T> might be included in the signature.

public final String getName(int number)

getName(int).

Not just me being anally-retentive, but what the compiler uses to check whether two methods are overloadings of each other. And remember the compiler often considers [] and ... as part of the type to mean the same thing.

 
Janeice DelVecchio
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic