Java examples for Collection Framework:Array Algorithm
Sums the squares of all components; also called the energy of the array.
//package com.java2s; public class Main { public static void main(String[] argv) throws Exception { double[] data = new double[] { 34.45, 35.45, 36.67, 37.78, 37.0000, 37.1234, 67.2344, 68.34534, 69.87700 }; System.out.println(sumSquares(data)); }/*www .ja v a2 s.c o m*/ /** * Sums the squares of all components; * also called the energy of the array. */ public static double sumSquares(double[] data) { double ans = 0.0; for (int k = 0; k < data.length; k++) { ans += data[k] * data[k]; } return (ans); } /** * Sums the squares of all components; * also called the energy of the array. */ public static double sumSquares(double[][] data) { double ans = 0.0; for (int k = 0; k < data.length; k++) { for (int l = 0; l < data[k].length; l++) { ans += data[k][l] * data[k][l]; } } return (ans); } /** * Sums the squares of all components; * also called the energy of the array. */ public static int sumSquares(int[] data) { int ans = 0; for (int k = 0; k < data.length; k++) { ans += data[k] * data[k]; } return (ans); } /** * Sums the squares of all components; * also called the energy of the array. */ public static int sumSquares(int[][] data) { int ans = 0; for (int k = 0; k < data.length; k++) { for (int l = 0; l < data[k].length; l++) { ans += data[k][l] * data[k][l]; } } return (ans); } }