Single array for two functions

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 1041 Views - Last Post: 23 October 2018 - 10:15 AM Rate Topic: -----

#1 jgssjs12   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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   User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 ndc85430   User is offline

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

Reputation: 895
  • View blog
  • 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?
Was This Post Helpful? 2
  • +
  • -

#4 g00se   User is offline

  • D.I.C Lover
  • member icon

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

Re: Single array for two functions

Posted 22 October 2018 - 09:26 AM

https://www.dreaminc...arallel-arrays/
Was This Post Helpful? 1
  • +
  • -

#5 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2682
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#6 Martyr2   User is online

  • Programming Theoretician
  • member icon

Reputation: 5309
  • View blog
  • 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. :)
Was This Post Helpful? 1
  • +
  • -

#7 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3599
  • View blog
  • 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
Was This Post Helpful? 1
  • +
  • -

#8 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 120
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#9 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3599
  • View blog
  • 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?
Was This Post Helpful? 1
  • +
  • -

#10 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 120
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12390
  • View blog
  • 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.
Was This Post Helpful? 1
  • +
  • -

#12 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 120
  • View blog
  • 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. :D 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 :yawn: nn
Was This Post Helpful? 0
  • +
  • -

#13 Piet Souris   User is offline

  • D.I.C Head

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

Re: Single array for two functions

Posted 22 October 2018 - 11:50 PM

View Postg00se, 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?
Was This Post Helpful? 0
  • +
  • -

#14 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3599
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#15 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 120
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2