Q:

(Sort ArrayList) Write the following method that sorts an ArrayList: public static <E extends Comparable<E>> void sort(ArrayList<E> list)

0

(Sort ArrayList) Write the following method that sorts an ArrayList:

public static < E  extends comparable<E>>

         void sort(ArrayList<E> list)

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

/*********************************************************************************
* (Sort ArrayList) Write the following method that sorts an ArrayList:           *
*                                                                                *
* public static <E extends Comparable<E>>                                        *
* 	void sort(ArrayList<E> list)                                                  *
*********************************************************************************/
import java.util.ArrayList;

public class Exercise_19_09 {
	/** Sorts an ArrayList of comparable objects */
	public static <E extends Comparable<E>>
			void sort(ArrayList<E> list) {
		E currentMin;
		int currentMinIndex;

		for (int i = 0; i < list.size() - 1; i++) {
			// Find the minimum in the ArrayList
			currentMin = list.get(i);
			currentMinIndex = i;

			for (int j = i + 1; j < list.size(); j++) {
				if (currentMin.compareTo(list.get(j)) > 0) {
					currentMin = list.get(j);
					currentMinIndex = j;
				}
			}

			// Swap list.get(i) wtih list.get(currentMinIndex) if necessary;
			if (currentMinIndex != i) {
				list.set(currentMinIndex, list.get(i));
				list.set(i, currentMin);
			}
		}
	}
}

Test.java

import java.util.*;

public class Test {
	public static void main(String[] args) {
	 	// Create a  list of Integers
	 	Integer[] intArray = {new Integer(2), new Integer(4),
	 	new Integer(3)};
	 	ArrayList<Integer> intList = 
	 		new ArrayList<>(Arrays.asList(intArray));
	 	
	 	// Create a list of Doubles
	 	Double[] doubleArray = {new Double(3.4), new Double(1.3),
	 	new Double(-22.1)};
	 	ArrayList<Double> doubleList = 
	 		new ArrayList<>(Arrays.asList(doubleArray));
	 		
	 	// Create a list of Characters
	 	Character[] charArray = {new Character('a'),
	 	new Character('J'), new Character('r')};
	 	ArrayList<Character> charList = 
	 		new ArrayList<>(Arrays.asList(charArray));
	 	
	 	// Create a list of Strings
	 	String[] stringArray = {"Tom", "Susan", "Kim"};
	 	ArrayList<String> stringList = 
	 		new ArrayList<>(Arrays.asList(stringArray));
	 	
	 	// Sort the lists
	 	Exercise_19_09.sort(intList);
	 	Exercise_19_09.sort(doubleList);
	 	Exercise_19_09.sort(charList);
	 	Exercise_19_09.sort(stringList);
	 
	 	// Display the sorted lists
	 	System.out.println("Sorted Integer objects: " + intList);
	 	System.out.println("Sorted Double objects: " + doubleList);
	 	System.out.println("Sorted Character objects: " + charList);
	 	System.out.println("Sorted String objects: " + stringList);
	}
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now