Sum all elements in the array : Array « Collections Data Structure « Java






Sum all elements in the array

    
/*
 * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
 * 
 * Project: OpenSubsystems
 * 
 * $Id: ArrayUtils.java,v 1.9 2007/01/23 06:00:30 bastafidli Exp $
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */


/**
 * Collection of useful utilities to work with arrays. 
 * 
 * @version $Id: ArrayUtils.java,v 1.9 2007/01/23 06:00:30 bastafidli Exp $
 * @author Peter Satury
 * @code.reviewer Miro Halas
 * @code.reviewed 1.5 2005/07/29 07:36:24 bastafidli
 */
public final class ArrayUtils
{
   /** 
    * Private constructor since this class cannot be instantiated
    */
   private ArrayUtils(
   )
   {
      // Do nothing
   }
   
   // Public methods ///////////////////////////////////////////////////////////

   /**
    * Method to exclude 2 arrays of ints so that the result contains all elements
    * from the first array, which are not in the second array.
    *  
    * @param arrBase - base array to exclude from 
    * @param arrExclude - array to exclude from the first one
    * @return int[] - array which contains all elements from the first array 
    *                 which are not in the second array or null
    */
   public static int[] exclude(
      int[] arrBase, 
      int[] arrExclude
   )
   {
      int[] arrReturn = null;

      if ((arrBase != null) && (arrBase.length > 0) && (arrExclude != null)
         && (arrExclude.length > 0))
      {
         int[] arrHelp;
         int   iCount1;
         int   iHelp;
         int   iLength = 0;
         
         arrHelp = new int[arrBase.length];
         for (iCount1 = 0; iCount1 < arrBase.length; iCount1++)
         {
            iHelp = arrBase[iCount1];
            if (ArrayUtils.contains(arrExclude, iHelp) == -1)
            {
               // If the element is not part of the second array then it should
               // be included in the result
               arrHelp[iLength++] = iHelp;
            }
         }
         
         // Shrink the array
         // TODO: Performance: Replace this with System.arraycopy
         arrReturn = new int[iLength];
         for (int iCount = 0; iCount < iLength; iCount++)
         {
            arrReturn[iCount] = arrHelp[iCount];
         }
      }
      else
      {
         arrReturn = arrBase;
      }
      
      return arrReturn;
   }
   
   /**
    * Test if specified array contains given element and if it does, find 
    * its position.
    * 
    * @param source - array to search, can be null
    * @param iTarget - element to find
    * @return int - -1 if it doesn't exist there otherwise its position
    */
   public static int contains(
      int[] source,
      int   iTarget
   )
   {
      int iReturn = -1;
      
      if ((source != null) && (source.length > 0))
      {   
         int iIndex;
         
         for (iIndex = 0; iIndex < source.length; iIndex++)
         {
            if (source[iIndex] == iTarget)
            {
               iReturn = iIndex;
               break;
            }
         }
      }
      
      return iReturn;
   }

   /**
    * Sum all elements in the array.
    * 
    * @param source - array to sum elements of
    * @return long - sum of the elements in the array
    */
   public static long sum(
      int[] source
   )
   {
      int iReturn = 0;
      
      if ((source != null) && (source.length > 0))
      {   
         int iIndex;
         
         for (iIndex = 0; iIndex < source.length; iIndex++)
         {
            iReturn += source[iIndex];
         }
      }
      
      return iReturn;
   }
}

   
    
    
    
  








Related examples in the same category

1.Initialize a static array
2.Initialization and re-assignment of arraysInitialization and re-assignment of arrays
3.Doubling the size of an arrayDoubling the size of an array
4.Timing array loop performance
5.Array 2DArray 2D
6.Can you change the .length of an array
7.Show Two-Dimensional Array of Objects
8.ArrayListDemo done over using an ArrayList
9.Array Hunt game
10.Multi Dimension Array
11.Clone Array
12.Associates keys with valuesAssociates keys with values
13.Arrays of primitives
14.Creating arrays with new
15.Array initialization
16.Creating an array of nonprimitive objects
17.Create multidimension arraysCreate multidimension arrays
18.Initializing Array ValuesInitializing Array Values
19.Creating a Two-Dimensional Array
20.Initializing a Two Dimensional ArrayInitializing a Two Dimensional Array
21.Using the length VariableUsing the length Variable
22.Triangular array
23.Grow arrayGrow array
24.Define array for class Define array for class
25.String array and output to consoleString array and output to console
26.Multiply two matrices
27.Array Of Arrays Demo 2Array Of Arrays Demo 2
28.Array Copy DemoArray Copy Demo
29.Copying Elements from One Array to Another
30.Java program to demonstrate multidimensional arraysJava program to demonstrate multidimensional arrays
31.Extend the size of an array
32.Copy an array
33.Initialize multidimensional array
34.Get array upperbound
35.To get the number of dimensions
36.Resize an array, System.arraycopy()
37.Dump array content: Convert the array to a List and then convert to String
38.java.utils.Arrays provides ways to dump the content of an array.
39.Dump multi-dimensional arrays
40.Use the new shorthand notation to iterate through an array
41.Create a repeated sequence of character
42.Reverse array elements order
43.Convert array of primitives into array of objects
44.Array Initializers
45.Reinitializes a byte array
46.Reinitializes an int array
47.Sums an array of numbers log(x1)...log(xn)
48.A class to iterate over all permutations of an array.
49.Palidrome Array
50.Set of utilities used to manipulate arrays.
51.ArrayUtils provides static methods for manipulating arrays when using a tool such as java.util.ArrayList is inconvenient.
52.Array Util
53.clone two dimensional array