以下是我在ruby中实现的shuffle算法:
def shuffle03!(arr)
len = arr.length
for i in 0..len-1
index1 = Random.rand(0..len-1)
index2 = Random.rand(0..len-1)
arr[index1], arr[index2] = arr[index2], arr[index1]
end
end
Run Code Online (Sandbox Code Playgroud)
我通过推算测试了这个算法:
class ShuffleTest
def initialize(seed)
len = seed.length
@count = {}
for i in 0..len-1
@count[seed[i]] = Array.new(len, 0)
end
end
def test(arr)
for i in 0...arr.length
@count[arr[i]][i] += 1
end
end
def show_count
return @count
end
end
def shuffle03!(arr)
len = arr.length
for i in 0..len-1
index1 = Random.rand(0..len-1)
index2 = Random.rand(0..len-1)
arr[index1], arr[index2] = arr[index2], …Run Code Online (Sandbox Code Playgroud) 在下面的代码nameRef.get()是null,之后name = null和System.gc().
import java.lang.ref.WeakReference;
public class Main {
public static void main(String[] args) {
String name = new String("ltt");
WeakReference<String> nameRef = new WeakReference<>(name);
System.out.println(nameRef.get()); // ltt
name = null;
System.gc();
System.out.println(nameRef.get()); // null
}
}
Run Code Online (Sandbox Code Playgroud)
WeakHashMap基于WeakReference.最后,我认为map.size()将是0.事实上,它是1.
import java.util.WeakHashMap;
public class Main2 {
public static void main(String[] args) {
String name = new String("ltt");
WeakHashMap<String, Integer> map = new WeakHashMap<>();
map.put(name, 18);
System.out.println(map.size()); // 1
name = null;
System.gc();
System.out.println(map.size()); // …Run Code Online (Sandbox Code Playgroud) 然后返回指定的Promise给出以下代码段:
function get(url) {
return new Promise(function(resolve, reject) {
var req = new XMLHttpRequest();
req.open('GET', url);
req.onload = function() {
if (req.status == 200) {
resolve(req.response);
}
else {
reject(Error(req.statusText));
}
};
req.onerror = function() {
reject(Error("Network Error"));
};
req.send();
});
}
Run Code Online (Sandbox Code Playgroud)
该功能可以如下使用:
get('story.json').then(function(response) {
console.log("Success!", response);
}, function(error) {
console.error("Failed!", error);
});
Run Code Online (Sandbox Code Playgroud)
我想使用Promise来加载多个网址,例如
get('a.json').get('b.json')
// or get('a.json').then().get('b.json')
Run Code Online (Sandbox Code Playgroud)
我已经以其他方式实现了它。但是据我了解,Promise无法完成这项工作。真的吗
实际上,我实现了一个类似的库,可帮助在浏览器中执行动态脚本:
Loader = (function() {
var load_cursor = 0;
var load_queue;
var loadFinished = function() {
load_cursor ++;
if …Run Code Online (Sandbox Code Playgroud) Java 将时间戳 -923130000 和 -923130001 解析为 LocalDateTime 失败。两个结果之间的秒数是3599,太奇怪了。
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
public class TestLocalDateTime {
public static void main(String[] args) {
System.out.println("-923130000: " + toLocalDateTime(-923130000L));
System.out.println("-923130001: " + toLocalDateTime(-923130001L));
System.out.println(ChronoUnit.SECONDS.between(toLocalDateTime(-923130000L), toLocalDateTime(-923130001L)));
}
private static LocalDateTime toLocalDateTime(Long timestamp) {
return LocalDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault());
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
-923130000: 1940-09-30T23:00
-923130001: 1940-09-30T23:59:59
3599
Run Code Online (Sandbox Code Playgroud)
爪哇版:
$ java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
Run Code Online (Sandbox Code Playgroud)
代码在 macOS 上执行。