Home > Java | Source Code > Java: Array Methods

Java: Array Methods

Posted on September 18, 2011 | No Comments

A few useful array methods in Java:
ArrayMethods.java

package arraymethods;

public class ArrayMethods {

	/*
	 * Returns the number of negative values in an array
	 *
	 * 	@Require: ArrayToCheck != Null && ArrayToCheck.length >=0
	 *
	 * 	@Ensure: will return the number of negatives number in ArraytoCheck
	 * 			 count >= 0
	 */

	public static int numberOfNegatives (int [] ArrayToCheck) {
		int count = 0;
		int i = 0;
		while ( i < ArrayToCheck.length){
			if (ArrayToCheck[i]<0)	count=count+1;
			i=i+1;
		}
		return count;

	}

	/*
	 * Returns an array reversed
	 *
	 * 	@Require: Arraytoreverse != Null && Arraytoreverse.length >=0
	 *
	 * 	@Ensure: will return the array reversed
	 * 			ArraytoReverse[last] = ArraytoReverse[0]
	 * 			ArraytoReverse[last-1] = ArraytoReverse[1]
	 * 			......
	 */

	public static int[] reverseArray (int [] ArraytoReverse) {
		int right = ArraytoReverse.length-1;
		int left = 0;
		int temp;
		while (left=0
	 * 	@ensure
	 * 		ArraytoShift[0] = ArraytoShift[ArraytoShift.length-1]
	 * 		for all i>0
	 * 			ArraytoShift[i+1] = ArraytoShift[i]
	 */

	public static int[] ShiftArray (int [] ArraytoShift) {
		int last = ArraytoShift.length-1;
		int temp = ArraytoShift[last];
		while (last>0) {
			ArraytoShift[last]=ArraytoShift[last-1];
			last--;
		}
		ArraytoShift[0] = temp;
		return ArraytoShift;
	}

	/*
	 * Computes the inner product from two arrays given.
	 * 	@Require Array1.length == Array2.length >=0
	 * 	@ensure
	 * 		For all i: result = result + (Array1[i]*Array2[i]);
	 *
	 */

	public static int ArrayProduct (int [] Array1, int [] Array2) {
		assert Array1.length==Array2.length;
		int result = 0;
		for (int i = 0; i < Array1.length; i++) {
			result = result + (Array1[i]*Array2[i]);
		}

		return result;
	}

	/*
	 * Computes the inner sum from two arrays given.
	 * 	@Require Array1.length > 0 && Array2.length > 0
	 * 	@ensure
	 * 		For all i: ArrayFinal[i]=Array1[i]+Array2[i];
	 *
	 */

	public static int[] addArrays (int [] Array1, int [] Array2) {
		int largest = 0;

		//Takes the largest length sent to create the ArrayFinal
		if (compare(Array2.length,Array2.length)>0) largest = Array2.length;
		else largest = Array2.length;

		//final array using the largest length
		int ArrayFinal[] = new int[largest];

		int carry = 0;
		int digit = 0;
		for (int i = 0; i < ArrayFinal.length; i++) {
			if (Array1[i]+Array2[i]+carry>=10) {
				digit = (Array1[i]+Array2[i])-10;
				ArrayFinal[i]=digit + carry;
				carry = 1;
			}
			else {
				digit = (Array1[i]+Array2[i]);
				ArrayFinal[i]=digit + carry;
				carry = 0;
			}

		}

		return ArrayFinal;
	}

	/*
	 * Computes the inner product from two arrays given.
	 * 	@Require Array1.length > 0 && Array2.length > 0
	 * 	@ensure
	 * 		For all i: ArrayFinal[i]=Array1[i]+Array2[i];
	 *
	 */

	public static int[] MultArrays (int [] Array1, int [] Array2) {

		int ArrayFinal[] = new int[Array1.length+Array2.length];

		for (int i = 0; i < ArrayFinal.length; i++) {
			ArrayFinal[i]=Array1[i]*Array2[i];

		}

		return ArrayFinal;
	}

	/**
	 *  compare integers
	 * returns 0 if the both  are equal
	 * returns <0 if length precedes length2
	 * returns >0 if length2 precedes length1
	 */
	public static int compare(int length, int length2) {
		return length - length2;
	}

}

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>