// diplay all the values in an array in order public static void displayForward( int[] a ) { for ( int n : a ) out.println(n); }
// diplay all the values in an array in *reverse* order public static void displayBackward( int[] a ) { for ( int i=a.length-1 ; i>=0; i-- ) out.println( a[i] ); }
// find the smallest value in an array (return smallest) public static int findSmallest( int[] arr ) { int min = Integer.MAX_VALUE; for ( int v : arr ) if ( v < min ) min = v; return min; }
// find the largest value in an array (return largest) public static int findLargest( int[] a ) { int max = Integer.MIN_VALUE; for ( int q : a ) if ( q > max ) max = q; return max; }
// get the sum of all the values in an array (return sum) public static int getSum( int[] values ) // we're up all night to... { int totes = 0; for ( int n : values ) totes += n; return totes; }
// display all values greater than some threshold (filter) in an array public static void displaySome( int[] thingys, int limit ) { for ( int t : thingys ) if ( t > limit ) out.println(t); }
// find the location (index) for a value in an array (return slot) public static int locate( int[] arr, int toFind ) { for ( int i=0 ; i<arr.length; i++ ) if ( arr[i] == toFind ) return i; return -1; }
// determine if a value is present in an array (return true/false) public static boolean contains( int[] arr, int toFind ) { for ( int k : arr ) if ( k == toFind ) return true; return false; }
// diplay all the values in an ArrayList in order public static void displayForward( ArrayList<String> names ) { for ( String s : names ) out.println(s); }
// diplay all the values in an ArrayList in *reverse* order public static void displayBackward( ArrayList<String> winners ) { for ( int i=winners.size()-1 ; i>=0; i-- ) out.println( winners.get(i) ); }
// find the smallest value in an ArrayList (return smallest) public static String findSmallest( List<String> list ) { String min = ""; for ( String v : list ) if ( v.compareTo(min) < 0 ) min = v; return min; }
// get the sum of all the values in an ArrayList (return sum) public static int getTotal( ArrayList<Double> sales ) { int sum = 0; for ( double s : sales ) sum += s; return sum; }
// display all values greater than some threshold (filter) in an ArrayList public static void displaySome( ArrayList<Integer> stuff, int limit ) { for ( int t : stuff ) if ( t > limit ) out.println(t); }
// find the location (index) for a value in an ArrayList (return slot) public static int locate( ArrayList<String> nicks, String wanted ) { for ( int i=0 ; i<nicks.size(); i++ ) if ( nick.get(i).equals(wanted) ) return i; return -1; }
// determine if a value is present in an ArrayList (return true/false) public static boolean contains( ArrayList<Integer> arr, int toFind ) { for ( int k : arr ) if ( k == toFind ) return true; return false; }
©2013–2015 Graham Mitchell
This assignment is licensed under a
Creative Commons Attribution-NonCommercial-ShareAlike 3.0 United States License.