• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Linking arrays

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ricky Rogers
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I thought I was starting to understand. I'm trying to find the method of indexing them together.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 76870
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic