import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
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 <T> void swap2( T[] array, int index1, int index2 ){
T 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
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 findMin(List<T> lst, int startIndex, int endIndex){
T temp = lst.get(startIndex);
for( int i = startIndex + 1; i <= endIndex; ++i){
if(lst.get(i).compareTo( temp ) < 0){
temp = lst.get(i);
}
}
return temp;
}
public static void main( String[] args ){
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");
ArrayList<Integer> primes = new ArrayList<Integer>();
primes.add(2);
primes.add(3);
primes.add(5);
primes.add(7);
primes.add(11);
primes.add(13);
primes.add(17);
primes.add(19);
primes.add(23);
Collections.shuffle( primes );
Integer min = findMin( primes, 4, primes.size() - 1);
System.out.println("Min value in " + primes + " between indices " + 4 + " and " + (primes.size() - 1) + " is " + min);
}
}