arr[0]
double first(double [] arr) {return arr[0];}
arr[arr.length-1]
double last(double [] arr) {return arr[arr.length-1];}
arr[(arr.length-1)/2]
double mid(double [] arr) {return arr[(arr.length-1)/2];}
arr[arr.length-1-i]
double mirror(double [] arr, int i) {return arr[arr.length-1-i];}
for (int i = 0; i < arr.length; i++) { // use arr[i] … }
for (double e : arr) // use e … }
for (int i = arr.length-1; i >= 0; i--) { // use arr[i] … }
boolean done = false; for (int i = 0; i < arr.length && !done; i++) { // use arr[i] … if (some condition) done = true; … }
break
statement
for (int i = 0; i < arr.length; i++) { // use arr[i] … if (some condition) break; … }
for (double e : arr) // use arr[i] … if (some condition) break; … }
boolean done = false; for (int i = arr.length-1; i >= 0 && !done; i--) { // use arr[i] … if (some condition) done = true; … }
break
for (int i = arr.length-1; i >= 0; i--) { // use arr[i] … if (some condition) break; … }
while
can be used
int i = 0; while (i < arr.length) { // use arr[i] … i++; }
void print(double [] arr) { System.out.print("{"); for (int i = 0; i < arr.length; i++) System.out.print(arr[i]+ " "); System.out.print("}"); }
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("}"); }
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("}"); }
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; }
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; }
double sum(double [] arr) { double result = 0; for (double e: arr) result += e; return result; }
double average(double [] arr) {return sum(arr) / arr.length;}
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 }
return -1
is after the loop; you can't state the value is not there until you've traversed the entire array.
find
for our definition of a contains
method
boolean contains(double [] arr, double val) {return find(arr, val) != -1;}
double [] copy(double [] arr) { double [] newArr = new double[arr.length]; for (int i = 0; i < arr.length; i++) newArr[i] = arr[i]; return newArr; }
void swap(double [] arr, int i, int j) { double t = arr[i]; arr[i] = arr[j]; arr[j] = t; }
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;
}
void reverse(double [] arr) { for (int i = 0; i < mid(arr); i++) swap(arr, i; mirror(i)); }
double [] reverse(double [] arr) { double [] newArr = new double[arr.length]; for (int i = 0; i < arr.length; i++) newArr[i] = mirror(arr, i)); }
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; }
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; }
contains
method is somewhat 'customized' to our situation, it searches a subsequence of the array (a subarray)
contains
a because it is introduced to 'help' the calling hasDups
method
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.