# Single array for two functions

• (2 Pages)
• 1
• 2

## 19 Replies - 1041 Views - Last Post: 23 October 2018 - 10:15 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=413246&amp;s=a5a42d747d85070d135688dc0f0248c1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jgssjs12

Reputation: 0
• Posts: 11
• Joined: 19-October 18

# Single array for two functions

Posted 22 October 2018 - 07:45 AM

I have configured the following two arrays. I am attempting to create a singular method that finds the apples that are the least expensive and then returns a String with the apple name AND how much it is. I can only figure this out using TWO separate methods. How can this be combined into just one?

```String apples[]={"HoneyCrisp","DeliciousRed","Gala","McIntosh","GrannySmith"};
double prices[]={4.50, 3.10, 2.45, 1.50, 1.20};

```

Is This A Good Question/Topic? 0

## Replies To: Single array for two functions

### #2 Programmer2004

Reputation: 18
• Posts: 96
• Joined: 25-October 17

## Re: Single array for two functions

Posted 22 October 2018 - 07:58 AM

Create two variables, first one will store index of an apple in the array, and the second one will store the last found least price. Now, iterate through the price array. Everytime you find a less price than the current one, store it in the price variable, and also store its index in the index variable. Now, when the loop finishes, just print values from both arrays at index pointed by the index variable.

### #3 ndc85430

• I think you'll find it's "Dr"

Reputation: 895
• Posts: 3,602
• Joined: 13-June 14

## Re: Single array for two functions

Posted 22 October 2018 - 09:15 AM

In your application, an apple had a name (or type, I guess) and a price. Why, then, aren't you using a class to model that and then have a single array of instances of that class?

### #4 g00se

• D.I.C Lover

Reputation: 3599
• Posts: 16,456
• Joined: 20-September 08

## Re: Single array for two functions

Posted 22 October 2018 - 09:26 AM

### #5 astonecipher

• Senior Systems Engineer

Reputation: 2682
• Posts: 10,723
• Joined: 03-December 12

## Re: Single array for two functions

Posted 22 October 2018 - 12:04 PM

Being this is Java, is there a reason there isn't an Apple (or similar) class for this?

### #6 Martyr2

• Programming Theoretician

Reputation: 5309
• Posts: 14,172
• Joined: 18-April 07

## Re: Single array for two functions

Posted 22 October 2018 - 12:53 PM

I would agree with astonecipher. Another option would be to use something like a HashMap, HashTable, LinkedHashMap (java versions of a dictionary idea).... pretty much anything that is a map. There you can map the apples to their prices and pass that as a single parameter to a single function. The function would search the map for the cheapest, then return the apple name and its price as an object or any pair structure you find works best.

### #7 g00se

• D.I.C Lover

Reputation: 3599
• Posts: 16,456
• Joined: 20-September 08

## Re: Single array for two functions

Posted 22 October 2018 - 01:12 PM

A class with a custom Comparator for sorting would be what i'd use

### #8 Sheepings

• Senior Programmer

Reputation: 120
• Posts: 843
• Joined: 05-December 13

## Re: Single array for two functions

Posted 22 October 2018 - 03:20 PM

I would be incline to flow with Martyr2 on this and also opt for using Map<String, Integer> eDictionary = new HashMap<String, Integer>(); and use it like eDictionary.put("key", "value"); Or use whichever datatypes for HashMap<String, Integer>() you preference. Something to note, you can't use primitive types in a HashMap.

Correct me if wrong, but its not an ordered/sorted collection either, if I recall correctly.

### #9 g00se

• D.I.C Lover

Reputation: 3599
• Posts: 16,456
• Joined: 20-September 08

## Re: Single array for two functions

Posted 22 October 2018 - 04:17 PM

OK - for the proponents of this Map thing - let's take it further. Note that sorting is required:

Quote

I am attempting to create a singular method that finds the apples that are the least expensive

Can you now show how your Map fits in with that exactly?

### #10 Sheepings

• Senior Programmer

Reputation: 120
• Posts: 843
• Joined: 05-December 13

## Re: Single array for two functions

Posted 22 October 2018 - 04:39 PM

So because the OP mentions singular, we should not bother proposing other options unless they are a singular method? I think not, and doing so would defeat the purpose of the forum; as such to find different ways of achieving similar solutions.

### #11 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12390
• Posts: 45,519
• Joined: 27-December 08

## Re: Single array for two functions

Posted 22 October 2018 - 05:22 PM

Sheepings- Ease up there. You're getting defensive, for no good reason. g00se is correct; your proposed solution isn't the best. While it is viable, for the proposed problem, it is not very extensible. For example, what if the decision was made down the road to add additional attributes to the Apples? g00se also pointed out the issues with sorting.

Yes- a benefit of the forum is multiple perspectives. However, not all perspectives are equally useful, and constructive criticisms are both useful and welcome. I think g00se was both respectful and constructive in his feedback.

### #12 Sheepings

• Senior Programmer

Reputation: 120
• Posts: 843
• Joined: 05-December 13

## Re: Single array for two functions

Posted 22 October 2018 - 05:56 PM

Apologies, never meant to come across harsh. I type as I'd speak in RL. Obviously a HashMap would require a bit of dabbling, but its not impossible to do it with. You could use collections.min in conjunction with Integer min Collections.min(eDictionary.values()); System.out.println(min); to get the lowest price, and if he needs the key/value, he can iterate. And I did point out in my first post, that this probably lacks sorting.

However, not everyone is comfortable working with and utilizing classes, and so I went along with what Martyr2 suggested and tried keeping it simple. Yes, astonecipheris right, a class would be the preferred option here, as it would be easier to extend additional parameters for additional attributes should that be the case down the road.

It's early morning here, and I'm half asleep, so off to bed I go nn

### #13 Piet Souris

Reputation: 44
• Posts: 194
• Joined: 01-December 15

## Re: Single array for two functions

Posted 22 October 2018 - 11:50 PM

g00se, on 23 October 2018 - 01:17 AM, said:

OK - for the proponents of this Map thing - let's take it further. Note that sorting is required:

Quote

I am attempting to create a singular method that finds the apples that are the least expensive

Can you now show how your Map fits in with that exactly?

Where does it say that sorting is required?

Assuming that prices can vary on a daily basis, a (Sorted)Map is not a bad solution. But is OP aware of Maps, List<Apple> and Comparators?

### #14 g00se

• D.I.C Lover

Reputation: 3599
• Posts: 16,456
• Joined: 20-September 08

## Re: Single array for two functions

Posted 23 October 2018 - 01:39 AM

Quote

Where does it say that sorting is required?

Finding the least expensive is by definition sorting, even if sort() is not being called

Quote

I am attempting to create a singular method that finds the apples that are the least expensive

Quote

Assuming that prices can vary on a daily basis, a (Sorted)Map is not a bad solution.

Variation is a red herring, but yes, SortedMap<Double, String> could be used but it's not an easily extensible solution, and it ignores the fact that the data that belong together should be together, irrespective of the need for sorting. So:

Quote

However, not everyone is comfortable working with and utilizing classes

that comfort needs to be achieved as quickly as possible as that is essentially the whole point of Java - it's an OO language. If classes are to be avoided then simply use a different language - Java is the wrong tool for the job

### #15 Sheepings

• Senior Programmer

Reputation: 120
• Posts: 843
• Joined: 05-December 13

## Re: Single array for two functions

Posted 23 October 2018 - 04:11 AM

Piet Souris, on 23 October 2018 - 07:50 AM, said:

Where does it say that sorting is required?

It doesn't, but someone implied that it was.