switch(number){
case 1 :
do{
Employee e = new Employee();
e.show();
e.display();
ArrayList<Employee> list = new ArrayList<Employee>();
list.add(e);
System.out.println("Data is Stored!");
System.out.println(list);
System.out.println("Add Another Data ? Press Yes=0 or No=1");
number = scan3.nextInt();
}while(number != 1);
break;
}
数据仅添加一次到列表中。我为连续流添加了while循环,但当前数据仅添加了nly。需要帮忙?员工是我拥有所有用户输入值的另一类。每次将数据添加到arraylist时,是否都会创建员工对象?
移动线
ArrayList<Employee> list = new ArrayList<Employee>();
在循环之前。
顺便说一句,将列表声明为ArrayList是不好的,因为您很可能不希望此依赖关系。典型的Java习惯用法是将变量声明为超类型,因此这里
List<Employee> list = new ArrayList<Employee>();
在Java 7中,其新功能“钻石算子” :)为
List<Employee> list = new ArrayList<>();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句