Comparator<? super T> c这个是什么意思
时间: 2023-06-08 10:03:31 浏览: 99
Comparator<? super T> c是Java中的泛型接口,它表示一个可以比较T类型的对象的比较器,其中?表示一个未知的类型,super T表示该未知类型是T类型的父类。这个接口用于在集合中对元素进行排序。
相关问题
public static <T> int binarySearch(T[] a, T key, Comparator<? super T> c)的用法
该方法是Java中的一个泛型方法,用于在指定的数组中查找指定的关键字。方法的参数包括:
- T[] a:表示需要查找的数组,该数组中元素的类型为T类型;
- T key:表示要查找的关键字,类型为T;
- Comparator<? super T> c:表示一个比较器对象,用于比较数组中的元素和关键字的大小关系。该比较器是一个泛型类型,用“?super T”表示它可以接受T类型及其父类的比较器对象。
方法的返回值是一个int类型,表示关键字在数组中的位置。如果找到了关键字,则返回其在数组中的索引;如果没有找到,则返回一个负数,表示要插入该关键字的位置。
该方法使用二分查找算法来查找关键字,比较器对象用于比较数组中的元素和关键字的大小关系。因为数组元素和关键字的类型可以不同,所以需要使用Comparator类型的比较器对象来进行比较。
1.Discuss how and why the SecurityManager class for JVM check methods can be defined differently based on context.. 2.The library class Collections contains a method sort with the following type signature. public static <T> void sort (T[] a, Comparator <? super T> c); The Comparator interface contains the following method which returns a negative integer, zero or a positive integer according to whether o1 is considered to be before, equal to or after o2 in a proposed ordering. int compare (T o1, T o2) ; What does the identifier T mean in the declaration of the method sort? Illustrate your answer with an example. 3.What does the notation <? super T> mean in the declaration of sort? Again, illustrate your answer with an example. 4.Explain the concept of an anonymous inner class in Java using a method call example. 5.Convert the method call example using an anonymous inner class to a lambda expression. Give the code required for this. 6.In what types of situations might you prefer to use an anonymous class instead of a lambda expression, and vice versa? 7.Explain (auto)boxing and (auto)unboxing in Java? 8.What does the annotation @Override mean in Java? What are the benefits of adding it? 9.What are wildcards in Java and how are they used in generic programming? 10.Can you explain the bounded wildcards, and provide an example
1. The SecurityManager class for JVM check methods can be defined differently based on context because different contexts may have different security requirements and restrictions. For example, a web application may need to restrict access to certain resources, while a desktop application may not have the same restrictions.
2. The identifier T in the declaration of the method sort represents a generic type parameter that can be replaced with any type when the method is called. For example, if we have an array of integers, we can call the sort method like this:
```
Integer[] arr = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3};
Comparator<Integer> c = Comparator.naturalOrder();
Collections.sort(arr, c);
```
3. The notation <? super T> in the declaration of sort means that the Comparator can accept any type that is a superclass of T. For example, if we have a class Animal and a subclass Dog, we can use a Comparator that accepts any superclass of Dog to sort an array of Dogs:
```
Dog[] dogs = ...;
Comparator<Animal> c = ...;
Collections.sort(dogs, c);
```
4. An anonymous inner class in Java is a class that is defined and instantiated at the same time, without a separate class definition. For example, we can define an anonymous inner class to implement an interface and pass it as an argument to a method:
```
Runnable r = new Runnable() {
public void run() {
System.out.println("Hello, world!");
}
};
r.run();
```
5. The same example using a lambda expression would look like this:
```
Runnable r = () -> System.out.println("Hello, world!");
r.run();
```
6. You might prefer to use an anonymous class instead of a lambda expression when you need to define a class that has additional behavior or state beyond the method being called. You might prefer to use a lambda expression when you only need to define a single method and want to keep the code more concise.
7. (Auto)boxing and (auto)unboxing in Java refer to the automatic conversion between primitive types and their corresponding object wrapper types. For example, an int can be automatically boxed to an Integer, and an Integer can be automatically unboxed to an int.
8. The annotation @Override in Java indicates that a method is intended to override a method in a superclass or interface. Adding this annotation can help catch errors at compile time and make the code more readable.
9. Wildcards in Java are used in generic programming to allow a generic type to accept any type that meets a certain criteria. For example, a List<? extends Number> can accept a List of any subclass of Number.
10. Bounded wildcards in Java restrict the types that can be used with a generic type parameter. For example, a List<? extends Number> can only accept types that are subclasses of Number, while a List<? super Integer> can accept types that are superclasses of Integer. An example of using a bounded wildcard would be:
```
public static double sum(List<? extends Number> list) {
double total = 0.0;
for (Number n : list) {
total += n.doubleValue();
}
return total;
}
```