CISC 1115
Introduction to Programming Using Java
Lecture #12
Techniques III — Arrays


Common Operations/Techniques on Arrays

First element

arr[0]

Last element

arr[arr.length-1]

Middle element

arr[(arr.length-1)/2]

'Mirror' element

arr[arr.length-1-i]

Traversing an array

Traversing an array backwards

for (int i = arr.length-1; i >= 0; i--) {
	// use arr[i]
	…
}

Traversing an array until some condition

Traversing an array backwards until some condition

Traversing using a while loop

Printing an array

void print(double [] arr) {
	System.out.print("{");
	for (int i = 0; i < arr.length; i++)
		System.out.print(arr[i]+ " ");
	System.out.print("}");
}

Printing an array (with comma logic)

void print(double [] arr) {
	System.out.print("{");
	for (int i = 0; i < arr.length; i++)
		System.out.print(arr[i]);
		if (i < arr.length-1) System.out.print(", ");
	}
	System.out.print("}");
}

Comma logic using the conditional operator

void print(double [] arr) {
	System.out.print("{");
	for (int i = 0; i < arr.length; i++)
		System.out.print(arr[i] + (i < arr.length-1 ? ", " : "");
	} 
	System.out.print("}");
}

Reading in an array using a header value

double [] read(Scanner scanner) {
	int numVals = scanner.nextInt();
	double [] arr = new double[numVals];
	for (int i = 0; i < numVals; i++)  
		arr[i] = scanner.nextDouble();
	return arr;
}

Finding the maximum/minimum of an array

double max(double [] arr) {
	double result = arr[0];
	for (int i = 1; i < arr.length; i++)
		if (arr[i] > result) result = arr[i];
	return result;
}

Finding the sum of an array

double sum(double [] arr) {
	double result = 0;
	for (double e: arr)
		result += e;
	return result;
}

Finding the average of an array

double average(double [] arr) {return sum(arr) / arr.length;}

(Linear) Searching an array: find and contains

int find(double [] arr, double val) {
	for (int i = 0; i < arr.length; i++)
		if (val == arr[i]) return i;
	return -1;		// -1 is never a valid index, so it's a good indicator that 'val' wasn't found
}
Notes
boolean contains(double [] arr, double val) {return find(arr, val) != -1;}

Copying (cloning) an array

double [] copy(double [] arr) {
	double [] newArr = new double[arr.length];
	for (int i = 0; i < arr.length; i++)
		newArr[i] = arr[i];
	return newArr;
}

Swapping two elements of an array

void swap(double [] arr, int i, int j) {
	double t = arr[i];
	arr[i] = arr[j];
	arr[j] = t;
}

A surprising result: the impossibility of direct swapping

void swap(int x, int y) {
	int t = x;
	x = y;
	y = t;
}
void swap(String s1, String s2) {
	String t = s1;
	s1 = s2;
	s2 = t;
}

Reversing an array

Determine if the are any duplicates in an array

The technique used for finding duplicates in a file is similar to that used for various array sorting techniques:
boolean hasDups(double [] arr) {
	for (int i = 0; i < arr.length; i++)
		for (int j = i+1; j < arr.length; j++)
			if (arr[i] == arr[j]) return true;
	return false;
}
The inner loop begins its processing at increasing indexs — the first time at index 1, the second at index 2, etc
Another Approach Using a 'Helper' Method
boolean hasDups(double [] arr) {
	for (int i = 0; i < arr.length; i++)
		if (contains(arr, i+1, arr.length-1, arr[i]) return true;
	return false;
}

boolean contains(double [] arr, int start, int end, double val) {
	for (int i = start; i <= end; i++)
		if (arr[i] == val) return true;
	return false;
}

Merging two sorted arrays to a third

Sorting an array

Sorting has its own lecture.

Determine if the are any duplicates in a sorted array

How is this different than the determining duplicates above? Is it harder? Easier?

Rotating an array

Rotating an array is similar in concept to rotating three elements but it's done in a loop from beginning to end or end to beginning depending on a left or right shift.

Shifting an array

Shifting an array is similar in concept to shifting three elements but it's done in a loop from beginning to end or end to beginning depending on a left or right shift.

Reading in an array (from a file) using a trailer value or eof by doing a pre-scan of the file

Scanner = new Scanner(new File(…));		

// Open the file, read the file completely and count the number of values

int count = 0;
while (scanner.hasNextInt()) {
	scanner.nextInt();
	count++;
}
scanner.close();

// We now know how many values are in the file; create the proper size array and read them in
double [] arr = new double[count];

// Open the file again, this time for reading

scanner = new Scanner(new File(…));		

for (int i = 0; i < count; i++)
	arr[i] = scanner.nextInt();
What we usually do instead — in such a situation — is cdeclare an array bigger then we could possibly need, and keep track of the number of elements we read into it.

Lab for this Lecture