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

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);

Edited 7 Years Ago by Ezzaral: n/a

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.

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

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?

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.

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.

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.

This question has already been answered. Start a new discussion instead.