This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
I've been trying to figure out how to link two different arrays. One is a string array, listing 5 items for purchase. Another is a double array, listing the prices for the items. I'm supposed to use the getSelectedIndex method of a comboBox that lists the items as the index numbers of the arrays. I'm just having a hard time understanding the concept. Any explanation would be greatly appreciated.
It's a recommendation for the assignment, so I want to go by that. Apparently it may make it easier to append a text area. The main thing to me is getting the price array to line up with the item array. This is one of the last things I have to do for my beginner class.
Ricky Rogers wrote:Well, I thought I was starting to understand. I'm trying to find the method of indexing them together.
The method is that every time you refer to index X in one array, you also refer to index X in the other array.
I'm going to reiterate what was already said though, in stronger terms: That's the wrong way to go about it. A better way would be to define a class that has a field for name and a field for price, or whatever the values are that you need to capture. Then you have a single array of those objects. If at some point it's necessary or significantly more convenient to deal with two parallel arrays, then you provide methods to transform your single array to and from the parallel arrays as needed, but the "master copy", so to speak, is the single array of objects that properly model your domain concepts.
Ricky Rogers wrote:It's a recommendation for the assignment, . . .
You should query that. It is far better practice to create a Foo class which encapsulates your String and your double. Then you can use a Foo. Then you can give it a better name than Foo. That is more‑or‑less exactly what Jeff V said earlier.