GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Linking arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Linking arrays" Watch "Linking arrays" New topic
Author

Linking arrays

Ricky Rogers
Greenhorn

Joined: Oct 04, 2012
Posts: 4
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

That's a really odd way to model that data. Is it a requirement for an assignment that you use unrelated arrays? That's not even remotely how I'd approach the problem in the real world.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ricky Rogers
Greenhorn

Joined: Oct 04, 2012
Posts: 4
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.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61315
    
  66

There isn't any way to "link" arrays. You'd just place the corresponding items in the same index.

Just be aware that this is in no way reflects how such data would actually be modeled outside of the classroom.
Ricky Rogers
Greenhorn

Joined: Oct 04, 2012
Posts: 4
I think I'm slowly starting to understand. I'm just finding arrays hard right now because I was just exposed to it. Thanks for your assistance thus far.
Ricky Rogers
Greenhorn

Joined: Oct 04, 2012
Posts: 4
Well, I thought I was starting to understand. I'm trying to find the method of indexing them together.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39090
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Linking arrays