A general method for array sorting and searching through Java generics

Source: Internet
Author: User
Tags arrays comparable min

The following is a discussion of array sorting and search capabilities, while a common approach is to implement the comparable interface, a method that needs to be implemented through Java generics. Here's a step-by-step explanation of why, and how to implement the generic method of this class through the Java generics.

How do some common methods in Java classes, especially some static tool methods?

For example, array arrays sort, search, and so on?

1. Ordinal contraction of an array of integers

public static int seqSearch(int[] arr, int first, int last, int target) {
     for (int i = first; i < last; i++)
         if (arr[i] == target)
             return i;

     return -1;
}

1.1 The above method is abstracted, first let us think of IS, using Java object reference, to implement the common method

public static int seqSearch(Object[] arr, int first, int last, Object target) {
     for (int i = first; i < last; i++)
         if (arr[i].equals(target))
             return i;

     return -1;
}

2.1 This seems like the reference to object is very convenient, and the second sequential search can be used to float,double,string and so on. If we're going to study further, we'll have problems.

public static void selectionSort(int[] arr) {
     int n = arr.length, smallIndex = 0;
     for (int i = 0; i < n; i++) { // 遍历array数组
         smallIndex = i;
         for (int j = i + 1; j < n; j++)
             if (arr[smallIndex] > arr[j]) // 选择最小的索引j
                 smallIndex = j;
         // if (smallIndex != i) {
         exchange(arr, i, smallIndex);// 交换array[i]与 min(array[i+1,..,n])
         // }
     }
}

2.2 The code above is an order-sorted algorithm, and if we want to write a common method, we must force the object type to be a way to implement the comparable interface.

The JVM throws a warning when it handles type coercion: Uncheck Cast

@SuppressWarnings("unchecked")
public static void selectionSort(Object[] arr) {

     int n = arr.length, smallIndex = 0;
     for (int i = 0; i < n; i++) { // 遍历array数组
         smallIndex = i;
         for (int j = i + 1; j < n; j++)
             if (((Comparable<Object>)arr[smallIndex]).compareTo(((Comparable<Object>)arr[j])) > 0) // 选择最小的索引j
                 smallIndex = j;
         // if (smallIndex != i) {
         exchange(arr, i, smallIndex);// 交换array[i]与 min(array[i+1,..,n])
         // }
     }
}

This can be seen using an object reference to handle a common problem, and when using an argument without implementing the comparable interface, the compiler throws a castclassexception Run-time exception. Such a program is unsafe.

3.1 uses object to generalize an algorithm (such as sequential search). By using the object reference and target value of an array, as long as the data type implements the Equals method, the data class in the algorithm to compare the size must implement the comparable interface, and now we're going to use Java generics to solve this problem

public static <T extends Comparable<? super T>> void selectionSort(T[] arr){
     int n = arr.length;
     int smallIndex;
     for (int i = 0; i < n-1; i++) {
         smallIndex=i;
         for (int j = i+1; j < n; j++)
             if (arr[j].compareTo(arr[smallIndex])<0)
                 smallIndex=j;
         exchange(arr, smallIndex, i);
     }
}

In the arrays class, the static Method Selectionsort (), which handles the integer type. To implement this algorithm with a generic version, because two elements in the generic type array t[] are to be compared, the object type that passes the argument or its superclass must implement the comparable interface.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.