由於 d3js 使用到大量的 array, 需要用到 array 內容的轉換,所以來學習如何遍歷 array.
解法:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
const array1 = ['a', 'b', 'c'];
array1.forEach((element) => console.log(element));
// Expected output: "a"
// Expected output: "b"
// Expected output: "c"
Java 世界遍歷 list 解法:
List<String> list = new ArrayList<>();
list.add("java");
list.add("C");
list.add("C++");
list.add("C#");
list.add("PHP");
/*
* 一般for迴圈
*/
for (int i = 0; i < list.size(); i++) {
System.out.println("一般for迴圈:" + list.get(i));
}
/*
* 增強for迴圈
*/
for (String str : list) {
System.out.println("增強for迴圈:" + str);
}
/*
* 迭代器1遍歷
*/
for (Iterator<String> it = list.iterator(); it.hasNext();) {
System.out.println("迭代器1遍歷:" + it.next());
}
/*
* 迭代器2遍歷
*/
Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println("迭代器2遍歷:" + it.next());
}
/*
* java8 Lambda表示式
*/
list.forEach(s -> {
System.out.println("java8 Lambda表示式:" + s);
});
/*
* java8 stream 有序流
*/
list.stream().forEach(s -> {
System.out.println("java8 stream 有序流:" + s);
});
/*
* java8 stream 並行流
*/
list.parallelStream().forEach(s -> {
System.out.println("java8 stream 並行流:" + s);
});