错误 java.lang.ClassCastException:java.lang.Integer 无法转换为 java.lang.Long
Error java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
我收到给定代码的以下错误。
我无法纠正错误是什么?
行中:"Long pw1 = pq1.remove();"
int n = in.ni();
long a[] = in.gla(n);
PriorityQueue<Long> pq = new PriorityQueue<>();
Map<Long, PriorityQueue> map = new HashMap<>();
Set<Long> s = new HashSet<>();
for (int i = 0; i < n; i++) {
if (s.contains(a[i])) {
map.get(a[i]).add(i);
} else {
pq.add(a[i]);
s.add(a[i]);
map.put(a[i], new PriorityQueue<>());
map.get(a[i]).add(i);
}
}
while(!pq.isEmpty()){
System.out.println(pq.remove());
}
while(!pq.isEmpty()){
long p = pq.remove();
System.out.println(p);
PriorityQueue<Long> pq1 = map.get(p);
while(pq1.size()>1){
Long pw1 = pq1.remove();
Long pw = pq1.remove();
System.out.println(pw);
if(s.contains(2*p)){
map.get(2*p).add(1);
}
else{
s.add(2*p);
pq.add(2*p);
map.put(p*2, new PriorityQueue<Long>());
map.get(2*p).add(1);
}
}
}
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
map
值的声明是 raw type PriorityQueue
(而不是 参数化类型 PriorityQueue<Long>
) .通过改变
给它一个类型
Map<Long, PriorityQueue> map = new HashMap<>();
至:
Map<Long, PriorityQueue<Long>> map = new HashMap<>();
我收到给定代码的以下错误。
我无法纠正错误是什么?
行中:"Long pw1 = pq1.remove();"
int n = in.ni();
long a[] = in.gla(n);
PriorityQueue<Long> pq = new PriorityQueue<>();
Map<Long, PriorityQueue> map = new HashMap<>();
Set<Long> s = new HashSet<>();
for (int i = 0; i < n; i++) {
if (s.contains(a[i])) {
map.get(a[i]).add(i);
} else {
pq.add(a[i]);
s.add(a[i]);
map.put(a[i], new PriorityQueue<>());
map.get(a[i]).add(i);
}
}
while(!pq.isEmpty()){
System.out.println(pq.remove());
}
while(!pq.isEmpty()){
long p = pq.remove();
System.out.println(p);
PriorityQueue<Long> pq1 = map.get(p);
while(pq1.size()>1){
Long pw1 = pq1.remove();
Long pw = pq1.remove();
System.out.println(pw);
if(s.contains(2*p)){
map.get(2*p).add(1);
}
else{
s.add(2*p);
pq.add(2*p);
map.put(p*2, new PriorityQueue<Long>());
map.get(2*p).add(1);
}
}
}
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
map
值的声明是 raw type PriorityQueue
(而不是 参数化类型 PriorityQueue<Long>
) .通过改变
Map<Long, PriorityQueue> map = new HashMap<>();
至:
Map<Long, PriorityQueue<Long>> map = new HashMap<>();