diff --git "a/data/1.Java\345\210\235\351\230\266/10.\347\261\273\345\222\214\346\216\245\345\217\243/4.\345\214\277\345\220\215\347\261\273/anonymous.md" "b/data/1.Java\345\210\235\351\230\266/10.\347\261\273\345\222\214\346\216\245\345\217\243/4.\345\214\277\345\220\215\347\261\273/anonymous.md" index b172157164c85ff4614fc9250fdb3835ce098340..3e2f9f6fd5b4193b04b0fa8e8ed957c057320dd8 100644 --- "a/data/1.Java\345\210\235\351\230\266/10.\347\261\273\345\222\214\346\216\245\345\217\243/4.\345\214\277\345\220\215\347\261\273/anonymous.md" +++ "b/data/1.Java\345\210\235\351\230\266/10.\347\261\273\345\222\214\346\216\245\345\217\243/4.\345\214\277\345\220\215\347\261\273/anonymous.md" @@ -6,7 +6,7 @@ public class App { public Parsec text(String txt){ return state -> { - if(String.equals(state.read(txt.legth), txt)){ + if(state.read(txt.legth).equals(txt)){ return new Success(txt); } else { return new NotMatch(state); diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/2.if-else/config.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/1.if-else/config.json" similarity index 100% rename from "data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/2.if-else/config.json" rename to "data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/1.if-else/config.json" diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/1.if-else/ifelse.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/1.if-else/ifelse.json" new file mode 100644 index 0000000000000000000000000000000000000000..389d0f05255f82d7040e2b0d0af8256c619cb363 --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/1.if-else/ifelse.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "ifelse.md", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/1.if-else/ifelse.md" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/1.if-else/ifelse.md" new file mode 100644 index 0000000000000000000000000000000000000000..cff9443026cfa1076947564f5f963d6cacf705e2 --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/1.if-else/ifelse.md" @@ -0,0 +1,71 @@ +# 逻辑判断 + +在 `Login` 类型中,我们需要提供一个 `Optional login(String key)` 函数,在函数中,调用 +`boolean check(String key)` 函数,如果 key 通过了校验,调用 `String gen(String key)` +生成一个字符串 token ,否则返回 null 。那么下列实现中有错的是? + +## 答案 + +```java +public class Login { + public Optional login(String key){ + Optional token; + if(!check(key)) + token = Optional.empty(); + return token; + else + token = Optional.of(gen(token)); + return token; + } + //... +} +``` + +## 选项 + +### A + +```java +public class Login { + public Optional login(String key){ + Optional token; + if(check(key)){ + token = Optional.of(gen(token)); + } else { + token = Optional.empty(); + } + return token; + } + //... +} +``` + +### B + +```java +public class Login { + public Optional login(String key){ + Optional token = Optinal.empty(); + if(check(key)){ + token = Optional.of(gen(token)); + } + return token; + } + //... +} +``` + +### C + +```java +public class Login { + public Optional login(String key){ + if(check(key)){ + return Optional.of(gen(token)); + } else { + return Optional.empty(); + } + } + //... +} +``` diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/1.true\345\222\214false/config.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/1.true\345\222\214false/config.json" deleted file mode 100644 index c235512018890153d1589c163ef95da7f87e410d..0000000000000000000000000000000000000000 --- "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/1.true\345\222\214false/config.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "node_id": "java-09da728d4cac4f51bcb205b7170dd83b", - "keywords": [], - "children": [], - "export": [], - "title": "true和false" -} \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.Foreach\350\257\255\346\263\225/config.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/2.for/config.json" similarity index 68% rename from "data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.Foreach\350\257\255\346\263\225/config.json" rename to "data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/2.for/config.json" index e25430a713c51d6879de3a3eb42c665b88cd0992..228c1b98811abde231b9e048a25b93dcdff42501 100644 --- "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.Foreach\350\257\255\346\263\225/config.json" +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/2.for/config.json" @@ -2,6 +2,9 @@ "node_id": "java-722dc1cccbc74369b9bff5ac580bc649", "keywords": [], "children": [], - "export": [], + "export": [ + "for.json", + "foreach.json" + ], "title": "Foreach语法" } \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/2.for/for.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/2.for/for.json" new file mode 100644 index 0000000000000000000000000000000000000000..81ceaa0f443ec4113929d86fa99ab2c4a040a725 --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/2.for/for.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "for.md", + "exercise_id": "97fca3618efa4ff28b0f67688874401e", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/2.for/for.md" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/2.for/for.md" new file mode 100644 index 0000000000000000000000000000000000000000..917454833ac9096ad92b9760b3d47032c4cdc85a --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/2.for/for.md" @@ -0,0 +1,68 @@ +# 循环 + +我们希望写一个简单的查找程序,在一个单词列表 `List` 中查找匹配的单词,找到的话 +返回其索引,否则返回 -1。正确的选项是 + +## 答案 + +```java +public class App() { + public int find(List words, String word) { + for(int i=0, i < words.size(); i++){ + if(word.equals(words.get(i))){ + return i; + } + } + return -1; + } +} +``` + +## 选项 + +### A + +```java +public class App() { + public int find(List words, String word) { + for(int i=1, i <= words.size(); i++){ + if(word.equals(words.get(i))){ + return i; + } + } + return -1; + } +} +``` + +### B + +```java +public class App() { + public int find(List words, String word) { + int point = -1; + for(point=0, point <= words.size(); point++){ + if(!word.equals(words.get(point))){ + point = -1; + } + } + return point; + } +} +``` + +### C + +```java +public class App() { + public int find(List words, String word) { + int i = -1; + for(var w: words){ + if(!word.equals(words.get(w))){ + i++; + } + } + return point; + } +} +``` \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/2.for/foreach.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/2.for/foreach.json" new file mode 100644 index 0000000000000000000000000000000000000000..1195e2e3901fd28c874d50fcca7ccdc6f11a9bea --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/2.for/foreach.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "foreach.md", + "exercise_id": "b63ff9cb13f9499889a5618913bcb14a", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/2.for/foreach.md" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/2.for/foreach.md" new file mode 100644 index 0000000000000000000000000000000000000000..84f7151055d7cafc611657f9635e71812d57d0cd --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/2.for/foreach.md" @@ -0,0 +1,66 @@ +# Foreach + +现在有一个 `List` 列表,我们希望遍历它,得到其中有多少个单词,列表中的 +每个元素可能包含一个或多个单词,单词间用空格分割,那么下面哪个实现是对的? + +## 答案 + +```java +public class App { + public int words(List content){ + int count = 0; + for(var str: content){ + var tokens = str.split(" "); + count += tokens.length; + } + return count; + } +} +``` + +## 选项 + + +### A + +```java +public class App { + public int words(List content){ + int count = 0; + for(int i=0; i< content.size(); i++){ + count += i; + } + return count; + } +} +``` + +### B + +public class App { + public int words(List content){ + int count = 0; + for(Character w: content){ + if(w == ' '){ + count += 1; + } + } + return count; + } +} + +### C + +public class App { + public int words(List content){ + int count = 0; + for(String w: content){ + for(Character c: w){ + if(w == ' '){ + count += 1; + } + } + } + return count; + } +} \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/7.switch/config.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/3.switch/config.json" similarity index 78% rename from "data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/7.switch/config.json" rename to "data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/3.switch/config.json" index 900bfeb8be7d5174ad811dd00dc425a7dee7f0ca..d9773b0dd18b05bb545913c72bd901b548851ffe 100644 --- "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/7.switch/config.json" +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/3.switch/config.json" @@ -2,6 +2,6 @@ "node_id": "java-05144fa9daf94a64ad0667eab1d6cc9a", "keywords": [], "children": [], - "export": [], + "export": ["switch.json"], "title": "switch" } \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/3.switch/switch.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/3.switch/switch.json" new file mode 100644 index 0000000000000000000000000000000000000000..d4f966542bfeb09a8a5d5e72a555eab48a25dc1b --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/3.switch/switch.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "switch.md", + "exercise_id": "7dd4bea864164e3b8ff5a5644c18a9cc", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/3.switch/switch.md" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/3.switch/switch.md" new file mode 100644 index 0000000000000000000000000000000000000000..58962b6f8ec5e32f0fcfe91ae316e43b273bd276 --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/3.switch/switch.md" @@ -0,0 +1,157 @@ +# switch + +我们希望编写一个函数,统计一个 `List` 列表中的数值分布,其中 1 +是 small,2 是 middle, 3 是 big,3以上都是 huge 。列表中的数值全部都在 +1 到 5之间,下列哪个程序会在对象的 small, middle, big, huge 四个字段 +保存正确的统计结果? + +## 答案 + +```java +public class Counter{ + int small = 0; + int middle = 0; + int bug = 0; + int huge = 0; + //... getter methods + + public void read(List numbers){ + for(var value: numbers){ + switch(value){ + case 1: + small += 1; + break; + case 2: + middle += 1; + break; + case 3: + big += 1; + break; + default: + huge +=1; + } + } + } +} +``` + +## 答案 + +### A + +```java +public class Counter{ + int small = 0; + int middle = 0; + int bug = 0; + int huge = 0; + //... getter methods + + public void read(List numbers){ + for(var value: numbers){ + switch(value){ + case 1: + small += 1; + case 2: + middle += 1; + case 3: + big += 1; + default: + huge +=1; + } + } + } +} +``` + +### B + +```java +public class Counter{ + int small = 0; + int middle = 0; + int bug = 0; + int huge = 0; + //... getter methods + + public void read(List numbers){ + for(var value: numbers){ + switch(value){ + case 1: + small += 1; + return small; + case 2: + middle += 1; + return middle; + case 3: + big += 1; + return big; + default: + huge +=1; + return huge; + } + } + } +} +``` + +### C + +```java +public class Counter{ + int small = 0; + int middle = 0; + int bug = 0; + int huge = 0; + //... getter methods + + public void read(List numbers){ + for(var value: numbers){ + switch(value){ + case 1: + small += 1; + return value; + case 2: + middle += 1; + return value; + case 3: + big += 1; + return value; + default: + huge +=1; + return value; + } + } + } +} +``` + +### D + +```java +public class Counter{ + int small = 0; + int middle = 0; + int bug = 0; + int huge = 0; + //... getter methods + + public void read(List numbers){ + for(var value: numbers){ + switch(value){ + case 1: + small += 1; + break; + case 2: + middle += 1; + break; + case 3: + big += 1; + break; + default: + huge +=1; + } + } + } +} +``` \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/3.\350\277\255\344\273\243/config.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/3.\350\277\255\344\273\243/config.json" deleted file mode 100644 index aa74fdc2a83f2c7005f12a85a789ed9c57b75e06..0000000000000000000000000000000000000000 --- "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/3.\350\277\255\344\273\243/config.json" +++ /dev/null @@ -1,24 +0,0 @@ -{ - "node_id": "java-73fd080a97c34ae2a89bd60c9510c409", - "keywords": [], - "children": [ - { - "do-while": { - "keywords": [], - "children": [], - "node_id": "java-0238e72ddb834cde8c0059fb0a213d57", - "title": "do-while" - } - }, - { - "逗号操作符": { - "keywords": [], - "children": [], - "node_id": "569d5e11c4fc5de7844053d9a733c5e8", - "title": "逗号操作符" - } - } - ], - "export": [], - "title": "迭代" -} \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/8.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/config.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/config.json" similarity index 99% rename from "data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/8.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/config.json" rename to "data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/config.json" index d0df1f7462e50aa17f688b7f98d54d34e35c6ddb..2378f02087fb6625f257bb79649a544f3d6c2856 100644 --- "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/8.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/config.json" +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/config.json" @@ -215,6 +215,6 @@ } } ], - "export": [], + "export": ["exception.json", "using.json"], "title": "通过异常处理错误" } \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/exception.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/exception.json" new file mode 100644 index 0000000000000000000000000000000000000000..59d6da0a8e2c90252f49fe445d85d9b2bd7775b2 --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/exception.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "exception.md", + "exercise_id": "6f70218542f84189b8ea67018eb1e3fd", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/exception.md" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/exception.md" new file mode 100644 index 0000000000000000000000000000000000000000..daf1263f290d6a26fb37110a5015dbee2593fabd --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/exception.md" @@ -0,0 +1,141 @@ +# 异常 + +已知我们有一个特殊的文件类型 PopFile,这个类型的对象方法 read() 可以返回字符串 +数据,当读取到末尾的时候,会抛出 Eof 异常,我们希望先通过 PopFile.open(path) +新建一个对象,并从这样一个 PopFile 对象中读取最多一百个文本数据,然后调用 close +方法关闭它。返回字符串列表`List`。但是这个对象中的数据可能不足一百个。 +此时我们需要在末尾添加一项 `[stop]`。 + +下列选项正确的是: + +## 答案 + +```java +public class App{ + public List Top100(String path){ + PopFile popFile = PopFile.open(path); + List result = new ArrayList(); + try{ + for(int i=0; i<100; i++){ + result.add(popFile.read()); + } + } catch(EofException eof){ + result.add("[stop]"); + } finally { + popFile.close(); + } + return reseult; + } +} +``` + +## 选项 + +### A + +```java +public class App{ + public List Top100(String path){ + PopFile popFile = PopFile.open(path); + List result = new ArrayList(); + try{ + for(int i=0; i<100; i++){ + result.add(popFile.read()); + } + return result; + } catch(EofException eof){ + result.add("[stop]"); + return result; + } + popFile.close(); + } +} +``` + +### B + +```java +public class App{ + public List Top100(String path){ + PopFile popFile = PopFile.open(path); + List result = new ArrayList(); + try{ + while(true){ + result.add(popFile.read()); + } + } catch(EofException eof){ + if(result.size() < 100){ + result.add("[stop]"); + } + return result; + } finally { + popFile.close(); + } + } +} +``` + +### C + +```java +public class App{ + public List Top100(String path){ + PopFile popFile = PopFile.open(path); + List result = new ArrayList(); + try{ + for(int i=0; i<100; i++){ + result.add(popFile.read()); + } + return result; + } catch(EofException eof){ + result.add("[stop]"); + return result; + } finally { + popFile.close(); + } + } +} +``` + +### D + +```java +public class App{ + public List Top100(String path){ + PopFile popFile = PopFile.open(path); + List result = new ArrayList(); + for(int i=0; i<100; i++){ + result.add(popFile.read()); + } + if(result.size() < 100){ + result.add("[stop]"); + } + popFile.close(); + return result; + } +} +``` + +### E + +```java +public class App{ + public List Top100(String path){ + PopFile popFile = PopFile.open(path); + List result = new ArrayList(); + try{ + for(int i=0; i<100; i++){ + result.add(popFile.read()); + } + if(result.size() < 100){ + result.add("[stop]"); + } + return result; + } catch(Excption error){ + } finally { + popFile.close(); + } + return result; + } +} +``` \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/using.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/using.json" new file mode 100644 index 0000000000000000000000000000000000000000..6052a222c6edf25c8a43a1c31052ab6f87d0e7ed --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/using.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "using.md", + "exercise_id": "2e7b0bb8e4fd4581aeba27815060cd73", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/using.md" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/using.md" new file mode 100644 index 0000000000000000000000000000000000000000..70f808918a7919b5a7d117dced341c897a532d3d --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/4.\351\200\232\350\277\207\345\274\202\345\270\270\345\244\204\347\220\206\351\224\231\350\257\257/using.md" @@ -0,0 +1,51 @@ +# using 模式 + +已知我们有一个特殊的文件类型 PopFile,这个类型的对象方法 read() 可以返回字符串 +数据,当读取到末尾的时候,会抛出 Eof 异常,我们希望先通过 PopFile.open(path) +新建一个对象,并从这样一个 PopFile 对象中读取最多一百个文本数据,然后调用 close +方法关闭它。返回字符串列表`List`。但是这个对象中的数据可能不足一百个。 +此时我们需要在末尾添加一项 `[stop]`。 + +如果我们想要用一下 `try using` 形式可靠的关闭 PopFile,需要满足什么条件? + +```java +public class App{ + public List Top100(String path){ + List result = new ArrayList(); + try(PopFile popFile = PopFile.open(path)){ + for(int i=0; i<100; i++){ + result.add(popFile.read()); + } + } catch(EofException eof){ + result.add("[stop]"); + } + return reseult; + } +} +``` + +## 答案 + +实现 `java.io.Closeable` 接口。 + +## 选项 + +### A + +实现 `void close()` 方法。 + +### B + +实现 `boolean close()` 方法. + +### C + +实现 `java.io.AutoClosable` 接口。 + +### D + +实现 `finally()` 方法。 + +### E + +实现 `exit()` 方法。 \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.return/config.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.return/config.json" deleted file mode 100644 index 5c34825aadd4297d2583b8139fe8919c1384c0db..0000000000000000000000000000000000000000 --- "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.return/config.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "node_id": "java-66019537247040fcbb6d74a29f47819f", - "keywords": [], - "children": [], - "export": [], - "title": "return" -} \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/9.\345\207\275\346\225\260/config.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/config.json" similarity index 77% rename from "data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/9.\345\207\275\346\225\260/config.json" rename to "data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/config.json" index 4ed906a06c027ff365510710376ca01eafc0f1d1..f1fab0774bf1f3af2b73bceb44d6af55a98390d8 100644 --- "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/9.\345\207\275\346\225\260/config.json" +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/config.json" @@ -2,5 +2,5 @@ "node_id": "java-f2482f12e0634885bdf6701a5a802c77", "keywords": [], "children": [], - "export": [] + "export": ["try.json"] } \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/try.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/try.json" new file mode 100644 index 0000000000000000000000000000000000000000..3c456044173e67eff4466fe340bd1b8d56d38ef5 --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/try.json" @@ -0,0 +1,7 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "try.md", + "exercise_id": "6d8116aa4c254e808bc1c2aa33afdd4f", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/try.md" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/try.md" new file mode 100644 index 0000000000000000000000000000000000000000..f81dbda5a39534a8c8528ce319a1a724d295d4ba --- /dev/null +++ "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/5.\345\207\275\346\225\260/try.md" @@ -0,0 +1,113 @@ +# Try 封装 + +我们的项目中有一个预备好的 `Try` 类型,它可以携带正常对象或异常,只需要调用对应的静态方法 +`Try.success(T result)` 或者 `Try.failure(Throwable err)` 即可。 + +现在我们希望为类型 +```java +public abstract class Parser { + public abstract T parse(E element); +} +``` +类编写一个静态的 ` Parser> pack(Parser E elemenmt)` 方法,它将传 +入的 parser 封装,返回一个新的,不会抛出异常的,而是返回 Try 类型的 `Parser>` 对象,下面 +正确的是 + +## 答案 + +```java +public abstract class Parser { + public abstract T parse(E element); + public static Parser> pack(Parser parser) { + return element -> { + try { + return Try.success(parser.parse(element)); + } catch(Throwable error){ + return Try.failure(error); + } + }; + } +} +``` + +## 选项 + +### A + +```java +public abstract class Parser { + public abstract T parse(E element); + public static Try pack(Parser parser) { + return element -> { + Try result; + try { + result = Try.success(parser.parse(element)); + } catch(Throwable error){ + result = Try.failure(error); + } finally { + return result; + } + }; + } +} +``` + +### B + +```java +public abstract class Parser { + public T parse(E element); + public Try pack(Parser parser) { + return element -> { + Try result; + try { + result = Try.success(parser.parse(element)); + } catch(Throwable error){ + result = Try.failure(error); + } finally { + return result; + } + }; + } +} +``` + +### C + +```java +public abstract class Parser { + public abstract T parse(E element); + public static Parser> pack(Parser parser) { + return element -> { + var result = parser.parse(element); + if(result instanceof Throwable){ + return Try.failure(result); + } else { + return Try.success(result); + } + }; + } +} +``` + +### D + +```java +public abstract class Parser { + public abstract T parse(E element); + public static Parser> pack(Parser parser) { + return element -> { + try { + var result = parser.parse(element); + if(result instanceof Throwable){ + return Try.failure(result); + } else { + return Try.success(result); + } + } catch(Throwable error){ + return Try.failure(error); + } + }; + } +} +``` \ No newline at end of file diff --git "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/6.break\345\222\214continue/config.json" "b/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/6.break\345\222\214continue/config.json" deleted file mode 100644 index a94569168f050856ee6f7a6504bb434535454d2a..0000000000000000000000000000000000000000 --- "a/data/1.Java\345\210\235\351\230\266/9.\346\216\247\345\210\266\346\211\247\350\241\214\346\265\201\347\250\213/6.break\345\222\214continue/config.json" +++ /dev/null @@ -1,7 +0,0 @@ -{ - "node_id": "java-1b0927bc3a3342869b4fc981abf17ff6", - "keywords": [], - "children": [], - "export": [], - "title": "break和continue" -} \ No newline at end of file diff --git a/data/tree.json b/data/tree.json index 107fc7169b984ae4fdcb8c9bf7f5fe3ba78af4a8..d5ed651e6a9b23d286f496bcf953576ac2ea00cd 100644 --- a/data/tree.json +++ b/data/tree.json @@ -672,13 +672,6 @@ "node_id": "java-05024fbfd53a4d88978d6c78d7ff06bf", "keywords": [], "children": [ - { - "true和false": { - "node_id": "java-09da728d4cac4f51bcb205b7170dd83b", - "keywords": [], - "children": [] - } - }, { "if-else": { "node_id": "java-85a126a6ba39488aaabd2d4015353175", @@ -687,50 +680,12 @@ } }, { - "迭代": { - "node_id": "java-73fd080a97c34ae2a89bd60c9510c409", - "keywords": [], - "children": [ - { - "do-while": { - "keywords": [], - "children": [], - "node_id": "java-0238e72ddb834cde8c0059fb0a213d57", - "title": "do-while" - } - }, - { - "逗号操作符": { - "keywords": [], - "children": [], - "node_id": "569d5e11c4fc5de7844053d9a733c5e8", - "title": "逗号操作符" - } - } - ] - } - }, - { - "Foreach语法": { + "for": { "node_id": "java-722dc1cccbc74369b9bff5ac580bc649", "keywords": [], "children": [] } }, - { - "return": { - "node_id": "java-66019537247040fcbb6d74a29f47819f", - "keywords": [], - "children": [] - } - }, - { - "break和continue": { - "node_id": "java-1b0927bc3a3342869b4fc981abf17ff6", - "keywords": [], - "children": [] - } - }, { "switch": { "node_id": "java-05144fa9daf94a64ad0667eab1d6cc9a",