aspose file tools*
The moose likes Beginning Java and the fly likes ArrayList objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList objects" Watch "ArrayList objects" New topic
Author

ArrayList objects

Christopher Laurenzano
Ranch Hand

Joined: Jul 25, 2009
Posts: 85
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)?

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12





This is another example of populating an ArrayList:



Does that clear things up?


When you do things right, people won't be sure you've done anything at all.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
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

Joined: Jul 25, 2009
Posts: 85
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
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

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

Joined: Jul 25, 2009
Posts: 85
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
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

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

Joined: Jul 25, 2009
Posts: 85
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
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

sometimes "this" works and I dunno really why....

See below:
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
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

Joined: Jul 25, 2009
Posts: 85
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
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

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

Joined: Jul 25, 2009
Posts: 85
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
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

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

Joined: Jul 25, 2009
Posts: 85
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
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

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
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
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
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1726
    
  12

Thanks!
 
wood burning stoves
 
subject: ArrayList objects