Explaining a little snippet

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bnashenas1984
    Contributor
    • Sep 2007
    • 257

    Explaining a little snippet

    Hi everyone
    I don't have any knowledge in JAVA.
    Could anyone please explain what this JAVA class does?

    What does "Double.POSITIV E_INFINITY" do?
    Code:
    public class Fraga7{
    	public static double min (double[] tal){
    		double min = Double.POSITIVE_INFINITY;
    		for (int i = 0; i < tal.length; i++){
    			if (tal[i] < min) min = tal[i];
    		}
    		return min;
    	}
    }
    Thanks
  • Anas Mosaad
    New Member
    • Jan 2013
    • 185

    #2
    The line in question (Line 3) assigns an initial min value positive infinity. It seemed weird to me but I like it.

    Usually, I would set the first item of the list to min and start the loop with 1. However, I have to check if the list is empty and in that case I should return infinity. This snippet does all of this. It returns Infinity if the list is empty (i.e.
    Code:
    min(new double[]{})
    ). Really a very good one.

    Comment

    • bnashenas1984
      Contributor
      • Sep 2007
      • 257

      #3
      Thanks Anas Mosaad
      I appreciate your help

      Comment

      • Anas Mosaad
        New Member
        • Jan 2013
        • 185

        #4
        You are always welcome :)

        Comment

        Working...