我在Core Java I书中阅读了下面的代码片段.
将数组列表分配为 新的ArrayList <'Employee>(100)//容量为100
与新员工[100] //大小为100时分配新数组不同
数组列表的容量与数组的大小之间存在重要区别.如果您分配一个包含100个条目的数组,则该阵列有100个插槽,可供使用.容量为100个元素的数组列表有可能容纳100个元素(实际上,超过100个,以额外的重新分配为代价); 但是在开始时,即使在初始构造之后,数组列表也根本不包含任何元素.
当我看到源代码数组列表时,构造函数创建一个给定容量的Object数组,该数组已准备好容纳给定容量的元素(下面是代码片段).
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
this.elementData = new Object[initialCapacity];
}
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚作者在上面的文字中提到的实际差异.
我thread.isInterrupted对下面程序中的行为感到有点困惑.
public class ThreadPractice {
public static void main(String args[]) throws InterruptedException {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("Starting thread..." + Thread.currentThread().getName());
Thread.sleep(10000);
System.out.println("Waking up");
}catch(InterruptedException e){
System.out.println("Thread is interrupted!!!");
Thread.currentThread().interrupt();
}
}
});
t.start();
Thread.sleep(2000);
t.interrupt();
//System.out.println(!t.isInterrupted());
while(!t.isInterrupted()){
System.out.println("Current thread not interrupted!!!");
}
}
}
Run Code Online (Sandbox Code Playgroud)
当执行上述程序时,它打印,
Starting thread...Thread-0
Thread is interrupted!!!
Run Code Online (Sandbox Code Playgroud)
但是当我取消注释System.out语句以打印中断状态时,它会遇到无限循环打印"当前线程未被中断"
我无法弄清楚System.out语句的确切区别.
在IE9中,对象属性的数字键被排序,并且与IE8相比,IE9中的迭代顺序不同,IE8在插入时保留了顺序.
var obj = {
"5": "John",
"1": "Kumar",
"3": "Rajesh",
"2": "Yogesh"
}
for(var key in obj) alert(key)
Run Code Online (Sandbox Code Playgroud)
结果
在IE9中// 1,2,3,4
// 5,1,3,2在IE8,IE7中
无论如何我可以通过IE9禁用此自动排序.如果没有,则可以以某种方式使浏览器理解密钥应该被标识为字符串而不是数字(但不附加任何空格,_或任何其他特殊字符)
请建议!!
以下是我遇到此问题的示例代码段.
function Person(id, name) {
this.id = id;
this.name = name;
}
var persons = new Object();
var p1 = Person("5","John")
persons[5]=p1
var p2 = Person("1","Kumar")
persons[1]=p2
var p3 = Person("3","Rajesh")
persons[3]=p3
var p4 = Person("4","Yogesh")
persons[4]=p4
for(var id in personId){
var p = persons[id];
var option = new Option(p.name, p.id);
select.options[select.options.length] = option; …Run Code Online (Sandbox Code Playgroud)