what I am trying to do, is get the minimun and maximun value from some float values I put manually. But I do not know how to do it. any helping coidng would be great. Thank you. Asume a list of floats p1, p2, p3, p4, p5, p6, p7, p8, p9
Asked
Active
Viewed 1,843 times
1
-
1pls show your code – Praveen Feb 13 '19 at 05:03
2 Answers
2
You can use a List Collection like ArrayList and use the default sorting method provided by the library - Collections.sort(list) and fetch the first and last element
List<Float> list = new ArrayList<Float>();
list.add(13.3);
list.add(134.3);
list.add(1.3);
Collections.sort(list);
float min = list.get(0); //1.3
float max = list.get(list.size-1); //134.3

Raghav Raman
- 134
- 2
- 9
-
-
1While this gives the correct answer, it is inefficient if the array gets longer. O(n log n) instead of O(n). – Henry Feb 13 '19 at 05:18
2
Add float values in ArrayList
val simpleArray = arrayOf(55.4, 20.0, 99.99, 1.99, 23.0, 34.2, 88.0, 72.1, 61.2, 43.9)
then use these two simple functions like below
val largestElement = simpleArray.max()
val smallestElement = simpleArray.min()

Akshay Katariya
- 1,464
- 9
- 20