使构造函数抛出异常是一个好习惯吗?例如,我有一个类Person,我有age它的唯一属性.现在我提供课程为
class Person{
int age;
Person(int age) throws Exception{
if (age<0)
throw new Exception("invalid age");
this.age = age;
}
public void setAge(int age) throws Exception{
if (age<0)
throw new Exception("invalid age");
this.age = age;
}
}
Run Code Online (Sandbox Code Playgroud) 如何在n不使用递归的情况下遍历一棵树?
递归方式:
traverse(Node node)
{
if(node == null)
return;
for(Node child : node.getChilds()) {
traverse(child);
}
}
Run Code Online (Sandbox Code Playgroud) 我们可以在交换机内使用枚举吗?
public enum Color {
RED,BLUE,YELLOW
}
public class Use {
Color c = Color.BLUE;
public void test(){
switch(c){
case Color.BLUE:
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在这收到一些错误.
The enum constant Color.BLUE reference cannot be qualified in a case label Use.java line 7 Java Problem
Run Code Online (Sandbox Code Playgroud) 如何实现将float或int转换为字符串的算法?我发现了一个链接 http://geeksforgeeks.org/forum/topic/amazon-interview-question-for-software-engineerdeveloper-0-2-years-about-algorithms-13
但我无法理解那里给出的算法