char大小为16位,短大小为16位.如果我们有一个接受short参数的构造函数,如果我们将char传递给它,为什么即使char和short的大小相同,它也不会接受?
import java.util.*;
public class Coffee {
Coffee(short s2) {
System.out.println("short accepted");
}
public static void main(String[] args) {
Coffee c=new Coffee('c');
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我们不能传递char给接受的构造函数short?
我有使用 PriorityQueue 的程序。poll() 没有给出队列中的所有值。
class Coffee {
public static void main(String[] args) {
PriorityQueue<Double> pq = new PriorityQueue<Double>();
Random rand = new Random();
for (int i = 0; i < 10; i++) {
pq.offer(rand.nextDouble());
}
System.out.println(pq);
System.out.print("size value " + pq.size());
for (int i = 0; i < pq.size(); i++) {
System.out.println(pq.poll());
}
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
[0.005756373546009885, 0.057563473207216886, 0.3415582636412481, 0.2026760924302
6186, 0.10792479235868724, 0.768845643547834, 0.5107848139799113, 0.758559713387
8311, 0.6437353209123445, 0.5156937257761389]
size value 10
0.005756373546009885
0.057563473207216886
0.10792479235868724
0.20267609243026186
0.3415582636412481
Run Code Online (Sandbox Code Playgroud)
大小为 10,那么为什么我无法使用 poll() 获取所有 …
我正在阅读“ Java思维”中的泛型章节。该程序在下面。
public class GenericWriting {
static <T> void writeExact(List<T> list, T item) {
list.add(item);
}
static List<Apple> apples = new ArrayList<Apple>();
static List<Fruit> fruit = new ArrayList<Fruit>();
static void f1() {
writeExact(apples, new Apple());
// writeExact(fruit, new Apple()); // Error:------------------line 1
// Incompatible types: found Fruit, required Apple
}
static <T> void writeWithWildcard(List<? super T> list, T item) {
list.add(item);
}
static void f2() {
writeWithWildcard(apples, new Apple());
writeWithWildcard(fruit, new Apple());
}
public static void main(String[] args) { f1(); …Run Code Online (Sandbox Code Playgroud)