1,105,409 Community Members

adding objects into an array

Member Avatar
whiteyoh
Posting Pro in Training
486 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 0 [?]
 
1
 

Hi all, im very new to java

i would like to know how to add the following objects into an array

CD cd1 = new CD("Kaiser "," up the khazi ", 9.99);
		CD cd2 = new CD("Oasis "," morning glory ",3.99);
		CD cd3 = new CD("Bob Dylan "," Alreet Sunna ",6.99);

i know i need to declare a variable, but i dont know what kind as the object has String and double.

im trying to achieve the following:

declare and set max (using cd[0].getCost())
declare and set maxIndex
for each remaining CD
If CD cost is bigger than max
Set max to bigger value
Set maxIndex
output result

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
1
 

You declare it as an array of CD objects, like so

CD[] cdList = new CD[10];

and create them in the array like so

cdList[0]=new CD("Kaiser "," up the khazi ", 9.99);
Member Avatar
whiteyoh
Posting Pro in Training
486 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 0 [?]
 
0
 

Great, thanks very much!!!!

Question Answered as of 4 Years Ago by Ezzaral
Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,751 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

whiteyoh: Using an array is dumb. It's fixed size, and you have a variable number of CDs. You should use an ArrayList. I gave you this advice, and sample code yesterday. OK, you don't want my involvement, I'm outta here. Good luck anyway.

Member Avatar
pooja.ui7
Newbie Poster
5 posts since Sep 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

The use of ArrayList in place of Array, can simplify your problems many folds. You can dynamically add and remove elements from it without worrying about the size of it.

For Arrays you have to declare the size and you cannot overshoot it. In practice we usually use ArrayList for maximum flexibility.

You can check out http://javacodeonline.blogspot.com/2009/08/difference-arraylist-and-array-in-java.html for more explanation on why to use ArrayList in place of Arrays.

Member Avatar
whiteyoh
Posting Pro in Training
486 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 0 [?]
 
0
 

James, i was unaware that an array was a bad choice. In fact i thought an arraylist was an array?!

Fair enough on your frustration! guess patience can only last so long. Many Thanks for help

Member Avatar
whiteyoh
Posting Pro in Training
486 posts since Jun 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 0 [?]
 
0
 

so with an array, say you set it to 10, and you dont use all 10, you will get an error on compiling.

arrayList makes the array the right size based on what you putting into it?

Member Avatar
masijade
Industrious Poster
4,165 posts since Feb 2006
Reputation Points: 1,351 [?]
Q&As Helped to Solve: 510 [?]
Skill Endorsements: 22 [?]
Team Colleague
Featured
 
0
 

so with an array, say you set it to 10, and you dont use all 10, you will get an error on compiling.

No. But it will if you to try to add an eleventh.

arrayList makes the array the right size based on what you putting into it?

No. It creates a small array (internal to the class, I believe the default size is 10), then when you go to add an eleventh item, it creates another array of 10 (or possibly one of 20 copying the items from the first, but I'm not sure, I'd need to check the source) when you go to add the eleventh item, and another when you go to add the 21st, etc, etc, etc. But you, as the programmer, do not need to worry about that, as you see only a single index.

Member Avatar
JamesCherrill
... trying to help
10,383 posts since Apr 2008
Reputation Points: 2,081 [?]
Q&As Helped to Solve: 1,751 [?]
Skill Endorsements: 47 [?]
Moderator
Featured
 
0
 

so with an array, say you set it to 10, and you dont use all 10, you will get an error on compiling.

Not a compile error but a runtime error,which is worse. If you only use the first 3 elements, you get a null pointer exception when you try to use the fourth. So it's up to you to keep track of how many elements you ae actually using at any time.
ArrayList just handles all that stuff so you don't need to do it yourself.

Member Avatar
Ezzaral
Posting Sage
7,431 posts since May 2007
Reputation Points: 2,714 [?]
Q&As Helped to Solve: 953 [?]
Skill Endorsements: 31 [?]
Moderator
Featured
 
0
 

Please keep in mind that although ArrayList might be a better choice, students may be required to use arrays on certain assignments as part of their course progression.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article