在以下代码示例中,将键设置为null和 System.gc()调用时,将WeakHashMap丢失所有映射并清空.
class WeakHashMapExample {
public static void main(String[] args) {
Key k1 = new Key("Hello");
Key k2 = new Key("World");
Key k3 = new Key("Java");
Key k4 = new Key("Programming");
Map<Key, String> wm = new WeakHashMap<Key, String>();
wm.put(k1, "Hello");
wm.put(k2, "World");
wm.put(k3, "Java");
wm.put(k4, "Programming");
k1=null;
k2=null;
k3=null;
k4=null;
System.gc();
System.out.println("Weak Hash Map :"+wm.toString());
}
}
class Key{
private String key;
public Key(String key) {
this.key=key;
}
@Override
public boolean equals(Object obj) {
return this.key.equals((String)obj);
} …Run Code Online (Sandbox Code Playgroud) 问题 - 当基于applet的应用程序在浏览器中加载时,会出现Java安全信息弹出窗口.
当我选中"始终信任来自发布者的内容"并单击"运行"时,应用程序将运行,并且证书将添加到Java用户可信证书列表中.
下次运行应用程序时,不会显示"安全信息"弹出窗口.
我的问题是 -
如何避免其他用户登录同一台计算机的安全信息弹出窗口?
它是多个用户使用的共享桌面,每次新用户登录并使用基于applet的应用程序时,都会显示安全信息弹出窗口.
如何将证书添加到集中位置(可能是系统信任?)而不是用户配置文件,以便每当新用户登录到计算机并使用该应用程序时,安全警告都不会出现?
我到现在为止尝试过的事情:
我正在使用AlertModule从ng2-bootstrap. 在该imports部分中,如果我只是使用,则会AlertModule出现错误Value: Error: No provider for AlertConfig!。如果我使用AlertModule.forRoot()该应用程序工作正常。为什么?
我的 app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import {AlertModule} from 'ng2-bootstrap/ng2-bootstrap';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
// AlertModule, /*doesn't work*/
AlertModule.forRoot() /*it works!*/
],
providers: [],
bootstrap: [AppComponent]
})
export class …Run Code Online (Sandbox Code Playgroud) byte b=5;
Integer i=(int)b;//b cast to int and int wrapped into Integer
Integer k=(byte)b;//compilation error, cannot convert from byte to Integer
Integer z=(byte)5;//compiles
Run Code Online (Sandbox Code Playgroud)
我的问题是为什么Integer z=(byte)5编译而Integer k=(byte)b不是?对于这个问题Integer z1 = (byte)5L,Integer z2 = (byte)5.3F也编译.是因为我试图转换编译时常量并且转换对它没有影响吗?
Object arr = new int[]{1,2,3};
arr = (int[])arr;
int someArr [] = (int[])arr;
for(int i:arr) // compilation error
System.out.println(i);
for(int i:someArr) //works fine
System.out.println(i);
Run Code Online (Sandbox Code Playgroud)
为什么第二行的转换不会将arr转换为int数组?我在第4行得到一个编译错误,上面写着"只能迭代一个数组或一个实例java.lang.Iterable".在调试时,在第2行,变量快照显示带有索引值的arr.
如果它发生的次数超过数组大小的一半,则此方法返回正整数数组中的数字,否则返回-1.我需要改进更大阵列的运行时间(10^5<size<10^8).有什么建议?
public static int findResult(int arr[],int len){
int val=0;
HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
for(int i=0;i<len;i++){
if(map.containsKey(arr[i])){
val = (Integer)map.get(arr[i]);
map.put(arr[i], val+1);
}else{
val=1;
map.put(arr[i], val);
}
}
Iterator<Integer> it=map.keySet().iterator();
while(it.hasNext()){
int next=it.next();
if((Integer)map.get(next)>(len/2)){
return next;
}
}
return -1;
}
Run Code Online (Sandbox Code Playgroud) public class DeadLock {
public static void main(String[] args) {
final A a = new A();
final B b = new B();
new Thread(new Runnable(){
@Override
public void run() {
a.aMethod(b);
}
},"Thread-2").start();
new Thread(new Runnable(){
@Override
public void run() {
b.bMethod(a);
}
},"Thread-2").start();
}
}
class A {
public void aMethod(B b) {
System.out.println("A method");
}
}
class B {
public void bMethod(A a) {
System.out.println("B method");
}
}
Run Code Online (Sandbox Code Playgroud)
我知道当两个或多个线程被阻塞等待彼此时会发生死锁.如何使用上面的代码实现相同的功能?同步A类和B类中的方法没有帮助.
java ×6
algorithm ×1
angular ×1
casting ×1
keytool ×1
security ×1
truststore ×1
typescript ×1
weakhashmap ×1