简体   繁体   English

在Android Studio中获取最小值和最大值

[英]Get the min and max value in android studio

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 假设有一个浮点数列表p1,p2,p3,p4,p5,p6,p7,p8,p9

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 您可以使用ArrayList之类的List集合,并使用库提供的默认排序方法-Collections.sort(list)并获取第一个和最后一个元素

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

Add float values in ArrayList 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM