英文:
Exception in java, print a list
问题
我有一个关于异常的一般性问题。
我应该写什么代码才能打印出整个列表?
在输出末尾的时候,我的输出以 **"String is null"** 结束
<br>谢谢
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
Collections.addAll(list, "hi", null, "hello");
try {
print(list);
} catch (NullPointerException e) {
System.out.println(e.getMessage());
}
}
private static void print(List<String> list) {
for (String string : list) {
if (string == null) {
throw new NullPointerException("String is null");
}
System.out.println(string);
}
}
英文:
I have a general question about exception.
What should I write so that the whole list is printed?
At the time my output ends by "String is null"
<br>Thank you
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
Collections.addAll(list, "hi", null, "hello");
try {
print(list);
} catch (NullPointerException e) {
System.out.println(e.getMessage());
}
}
private static void print(List<String> list) {
for (String string : list) {
if (string == null) {
throw new NullPointerException("String is null");
}
System.out.println(string);
}
}
答案1
得分: 4
如果您想要打印整个列表,您应该使用System.out.println("String is null");
而不是throw new NullPointerException("String is null");
,因为异常会停止程序的运行,导致无法打印剩余的元素。
import java.util.*;
public class Program
{
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
Collections.addAll(list, "hi", null, "hello");
try {
print(list);
} catch (NullPointerException e) {
System.out.println(e.getMessage());
}
}
private static void print(List<String> list) {
for (String string : list) {
if (string == null) {
System.out.println("String is null");
} else {
System.out.println(string);
}
}
}
}
英文:
If you want to print the whole list, you should use System.out.println("String is null");
instead of throw new NullPointerException("String is null");
because the exception stops the program and it can't print the rest of the elements.
import java.util.*;
public class Program
{
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
Collections.addAll(list, "hi", null, "hello");
try {
print(list);
} catch (NullPointerException e) {
System.out.println(e.getMessage());
}
}
private static void print(List<String> list) {
for (String string : list) {
if (string == null) {
System.out.println("String is null");
} else {
System.out.println(string);
}
}
}
}
答案2
得分: 2
以下是翻译好的部分:
你可以通过检查可为空的变量来控制流程,而不是抛出空指针异常...
import java.util.*;
public class Program {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
Collections.addAll(list, "hi", null, "hello");
print(list);
}
private static void print(List<String> list) {
if(list == null ){
return;
}
for (String string : list) {
if (string == null) {
System.out.println("String is null");
} else {
System.out.println(string);
}
}
}
}
英文:
You can control your flow by checking nullable variables instead of throwing nullpointer exception...
import java.util.*;
public class Program {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
Collections.addAll(list, "hi", null, "hello");
print(list);
}
private static void print(List<String> list) {
if(list == null ){
return;
}
for (String string : list) {
if (string == null) {
System.out.println("String is null");
} else {
System.out.println(string);
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论