4 Replies Latest reply on May 25, 2009 9:28 PM by 807588

# getDouble from array index

I'm creating a program for users to purchase CD's. The CD's information are sorry in 3 arrays:

- title
- artist
- price

The price is stored as a double. Now I'd like to be able to access this double to work out the total for the users CDs and it looks as if getDouble is what I need but I can't find any example to help me.

The user would enter the index of the CD they'd like to purchase e.g. 5

Then the program would get the double from the array index 5

then I could use it in the purchase method to calulate the total etc...

any help on this would be great, many thanks!
• ###### 1. Re: getDouble from array index
So you have an array of... what?
• ###### 2. Re: getDouble from array index
If price is an array of double, you don't need any special method to obtain the value at a particular index.

[http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html]
• ###### 3. Re: getDouble from array index
Is this all in one class? It sounds as if you already know how to get the price for the 5th cd, simply use price[4] (since arrays start at 0 not 1).

A warning though: You're using what's called the "parallel array anti-pattern" where all your pertinent information is stored in several arrays that are called parallel since the 5th CD's title is at title[4], it's price is at price[4], and the artist is stored at location artist[4]. The problem with this pattern is it is easy for one of the arrays to go out of synch messing up all the data. Better is to create a CD class that holds the title, artist and price information. Then you could have a single array of CD objects with all the pertinent information tied together as it should be. THEN you'd need and use a getPrice() method.
• ###### 4. Re: getDouble from array index
``double d = price[2];``