import java.util.List;
import java.util.Vector;
import java.util.ArrayList;
import java.util.Collections;
public class GenericMethodsExercises{
Swap the elements at two indices
<p>Note: Does not check if the indices are valid</p>
@param array
@param index1
@param index2
public static void swap2Original( Integer[] array, Integer index1, Integer index2 ){
Integer temp = array[index1];
array[ index1 ] = array[ index2 ];
array[ index2 ] = temp;
}
Swap the elements at two indices
<p>Note: Does not check if the indices are valid</p>
@param array
@param index1
@param index2
public static <E> void swap2( E[] array, int index1, int index2 ){
E temp = array[index1];
array[ index1 ] = array[ index2 ];
array[ index2 ] = temp;
}
Display all of the numbers (whole or real) in a list on one line
@param lst
public <T extends Number> void printList(List<T> lst) {
System.out.println( "printList( + "+lst+" )");
for( Number n : lst )
System.out.print(n + " ");
System.out.println();
}
Finds the minimum value in a list between two indices
<p>Note: Does not check if the indices are valid</p>
@param lst
@param startIndex
@param endIndex
@return
public static <T extends Comparable<T>> T minT(List<T> lst, int startIndex, int endIndex){
T min = lst.get( startIndex );
for(int i = startIndex; i <= endIndex; ++i){
if( min.compareTo( lst.get(i) ) > 0 ){
min = lst.get(i);
}
}
return min;
}
public static void main( String[] args ){
Vector< Character > vowels = new Vector< Character >();
vowels.add( 'a' );
vowels.add( 'e' );
vowels.add( 'i' );
vowels.add( 'o' );
vowels.add( 'u' );
vowels.add( 'y' );
for( Character ch : vowels ){
System.out.println( ch );
}
Integer[] nums = {1, 2, 3};
Character[] hello = {'h', 'e', 'l', 'l', 'o'};
for( int i : nums ){
System.out.print( i );
}
System.out.println("\n");
swap2( nums, 1, 2 );
for( int i : nums ){
System.out.print( i );
}
System.out.println("\n");
for( Character c : hello ){
System.out.print( c );
}
System.out.println("\n");
swap2( hello, 4, 3 );
for( Character c : hello ){
System.out.print( c );
}
System.out.println("\n");
System.out.println("minT( vowels, 1, 5)" );
System.out.println( minT( vowels, 1, 5) );
ArrayList< Integer > primesList = new ArrayList< Integer >();
primesList.add( 2 );
primesList.add( 3 );
primesList.add( 5 );
primesList.add( 7 );
primesList.add( 11 );
primesList.add( 13 );
primesList.add( 17 );
Collections.shuffle(primesList);
System.out.println("minT( " + primesList + ", 0, primesList.size() - 1)" );
System.out.println( minT( primesList, 0, primesList.size() - 1) );
}
}