wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Array or Array List or something else for entering a theoretically limitless amount of objects/info Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array or Array List or something else for entering a theoretically limitless amount of objects/info" Watch "Array or Array List or something else for entering a theoretically limitless amount of objects/info" New topic
Author

Array or Array List or something else for entering a theoretically limitless amount of objects/info

Christopher Laurenzano
Ranch Hand

Joined: Jul 25, 2009
Posts: 85
This has to do with the CD program I'm trying to write. I've only gotten up to array lists as a means of collecting information (I'm reading HFJ) and I was wondering -- is it possible to use either of these two to enter a theoretically limitless amount of CD's?

I want to give the CD objects the name CDRecord1(CDRecord1.Artist, CDRecord1.albumTitle, CDRecord2.name, CDRecord2.albumTitle, etc) but I'm not sure how or if I can use that naming method with an Array or an Array List in some kind of a loop to advance to the next record. Can anyone tell me if this is possible or do I have to wait until I read more?
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
This has to do with the CD program I'm trying to write. I've only gotten up to array lists as a means of collecting information (I'm reading HFJ) and I was wondering -- is it possible to use either of these two to enter a theoretically limitless amount of CD's?

ArrayLists can hold a lot of information. I'm not sure of their size limits or even if they have size limits, as I've never run into them, but having said this, if you are looking to create a functional program (not just a school assignment) then my guess is that if you want to store and manipulate a huge amount of data, then your best bet is to use a real database. That's what they are built for and optimized for. Java can interface quite nicely with many database engines and you may wish to explore this some more.

I want to give the CD objects the name CDRecord1(CDRecord1.Artist, CDRecord1.albumTitle, CDRecord2.name, CDRecord2.albumTitle, etc) but I'm not sure how or if I can use that naming method with an Array or an Array List in some kind of a loop to advance to the next record. Can anyone tell me if this is possible or do I have to wait until I read more?

I'm not sure that I understand exactly what you are trying to do here. Could you clarify this part of your question a bit more?
Christopher Laurenzano
Ranch Hand

Joined: Jul 25, 2009
Posts: 85
Sorry --

I'm learning Java and and trying to put together a program that will take all of my cd's and organize them into a list (I could do it Excel, but that would defeat the purpose). I first want a user to enter the information for the CD's four fields -- Artist, album, label and year of release, edit and save the data, etc, and eventually organize the list according to the user's criteria -- alphabetically by artist or album, label, whatever.


I'm just starting out and don't expect to have this completed any time soon, but what I was asking is how would I go about building a method(s) to take in the data. A regular array needs to have its size defined, which won't work, because theoretically there are a limitless number of CD's to catalog. So I was thinking that an array list might work.

From what I understand about array lists so far is that you can add new objects to the list -- Right now I'm thinking that each new object is a CDRecord (from my CD class), and each records has the four fields associated with it, which are the instance variables for the class. The thing is, currently as I understand an array list, I know I can add a new object, like, for example, CDRecord1 (which would be the first CD entered) and each record would then have its name, album, etc associated with it (using the dot operator). The next record would be named CDRecord2 with all the necessary fields associated with it for that particular CD, and so on.

My confusion/obstacle how I would instantiate succeeding CDRecord objects -- somehow finding a way to get the program to create the new object and add it to the list, given the necessity and endless amount of records that could be theoretically added. I could also just make one object and keep adding elements to it, but I don't think that would make sense. I'd rather create new objects for each CD and add it to the array list. But I'm not sure if an Array List is the best way for it, or if there's something else I can do, that maybe I haven't come across yet in HFJ (Chap 6 is where I am right now, which talks about array lists -- if you're familiar with the book, take a look at it.) I'm not familiar with database programs and/or how to interface with them. I want to write the program for the intellectual satisfaction of it. I know it can be done, I'm just not sure how at the moment.

Hope I made it clearer, if not my apologies in advance.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18545
    
  40


To be blunt, I am not sure if I understand what you are trying to ask either... but I can try to guess.

Christopher Laurenzano wrote:A regular array needs to have its size defined, which won't work, because theoretically there are a limitless number of CD's to catalog. So I was thinking that an array list might work.


Yes, in theory, an arraylist can grow to lots and lots elements. Nothing is really limitless, as you will probably run out of memory.

Christopher Laurenzano wrote:The thing is, currently as I understand an array list, I know I can add a new object, like, for example, CDRecord1 (which would be the first CD entered) and each record would then have its name, album, etc associated with it (using the dot operator). The next record would be named CDRecord2 with all the necessary fields associated with it for that particular CD, and so on.


Well the record won't be named cdrecord1 and cdrecord2, but yea, you can access element one and element two, which may be cdrecord objects.

Christopher Laurenzano wrote:My confusion/obstacle how I would instantiate succeeding CDRecord objects -- somehow finding a way to get the program to create the new object and add it to the list, given the necessity and endless amount of records that could be theoretically added.


Can you elaborate this? What's wrong with just adding the objects with a loop?

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Christopher Laurenzano
Ranch Hand

Joined: Jul 25, 2009
Posts: 85
I wasn't clear enough -- I know that I could use a loop, but not exactly how, do you have any suggestions? According to HFJ, I can add objects to the array list (if the example on page 133 is correct). I'm just not sure how to work it out.
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2155
    
    7
A String is an Object. So write a simple program that adds Strings to an ArrayList. Once you've mastered that you can then create your custom CDInfo Object and add it to an ArrayList.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

Altho an ArrayList will work, what you really want is a database, and access it from your java code. MySql and others are free, cross platform, and are handy to know for future job/resume work.

most of the Java Collections framework have no hard limits, but you quickly run into memory constraints. My personal CD collection has 856 albums with 10444 songs by 530 artists. Depending on how much information you keep on each song, that could be a fairly large amount of data quickly.
Embla Tingeling
Ranch Hand

Joined: Oct 22, 2009
Posts: 237
Christopher Laurenzano wrote:I want to give the CD objects the name CDRecord1(CDRecord1.Artist, CDRecord1.albumTitle, CDRecord2.name, CDRecord2.albumTitle, etc) but I'm not sure how or if I can use that naming method with an Array or an Array List in some kind of a loop to advance to the next record. Can anyone tell me if this is possible or do I have to wait until I read more?


You can use an ArrayList. It will grow as you add CD objects.

Note that in Java objects are always represented by references (pointers to objects). This means that for each CD object you store, only a reference (4 bytes) will be entered into the ArrayList. This also means the CD objects will hold the bulk of the information, not the ArrayList itself. The ArrayList will consume far less memory than the sum total of CD objects.

The overhead for an object is 16 bytes. One String object will consume 16 bytes of overhead plus 2 bytes for each character plus an integer of 4 bytes holding the String length. Say the artist name is 10 bytes on average and the album title too. This gives 16 + 2*10 + 4 = 40 bytes for the artist and 40 bytes for the title String object. Then you have the CD object itself. It will hold two String object references of 4 bytes. This gives 16 + 2*4 = 24 bytes.

So all in all one CD object will occupy 24 + 40 + 40 = 104 bytes. This should be compared with the 4 byte reference which is stored in the ArrayList for one CD object. This means only about 4% of the total memory requirements falls on the actual ArrayList and 96% will be on the CD objects themselves.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array or Array List or something else for entering a theoretically limitless amount of objects/info
 
Similar Threads
explanation of getter/setter methods -- what's the difference exactly?
Performance of Java Object Referece
cannot find symbol error -- can't see what I'm missing
ArrayList objects
Can't figure out the logic in this method