androidmathmath-functions

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


Solution

  • 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