how to create, initialize, and process arrays

Java Programming Language / Array in java

1292

Program:

public class ArrayExample {

    public static void main(String[] args) {
         double[] myList = {3.9, 5.9, 22.4, 31.5};

         // Print all the array elements
         for (int i = 0; i < myList.length; i++) {
            System.out.println(myList[i] + " ");
         }

         // Summing all elements
         double total = 0;
         for (int i = 0; i < myList.length; i++) {
            total += myList[i];
         }
         System.out.println("Total is " + total);

         // Finding the largest element
         double max = myList[0];
         for (int i = 1; i < myList.length; i++) {
            if (myList[i] > max) max = myList[i];
         }
         System.out.println("Max is " + max);
   }
}

Output:

3.9
5.9
22.4
31.5
Total is 63.7
Max is 31.5
Press any key to continue . . .

Explanation:

None

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.