Java中迭代器怎么使用?
部分代码:classStudent{Stringname;intage;Stringsex;publicStudent(String_name,int_age,Strin...
部分代码:
class Student{
String name;
int age;
String sex;
public Student(String _name,int _age,String _sex){
this.name = _name;
this.age = _age;
this.sex = _sex;
}
public String toString(){
return this.name + "," + this.age + "," + this.sex;
}
}
public class IteratorTest {
public static void main(String[] args) {
Student stu1 = new Student("孙彦波",22,"男");
Student stu2 = new Student("伍明智",23,"男");
Student stu3 = new Student("姚晓燕",20,"女");
ArrayList list = new ArrayList();
list.add(stu1);
list.add(stu2);
list.add(stu3);
Iterator iterator = list.iterator();
while(iterator.hasNext())
System.out.println(iterator.next());
}
}
问题:上面的Name,age和sex是一起输出的,有没有办法分开输出,比如“姓名:**,年龄:**,性别:**”,怎么实现? 展开
class Student{
String name;
int age;
String sex;
public Student(String _name,int _age,String _sex){
this.name = _name;
this.age = _age;
this.sex = _sex;
}
public String toString(){
return this.name + "," + this.age + "," + this.sex;
}
}
public class IteratorTest {
public static void main(String[] args) {
Student stu1 = new Student("孙彦波",22,"男");
Student stu2 = new Student("伍明智",23,"男");
Student stu3 = new Student("姚晓燕",20,"女");
ArrayList list = new ArrayList();
list.add(stu1);
list.add(stu2);
list.add(stu3);
Iterator iterator = list.iterator();
while(iterator.hasNext())
System.out.println(iterator.next());
}
}
问题:上面的Name,age和sex是一起输出的,有没有办法分开输出,比如“姓名:**,年龄:**,性别:**”,怎么实现? 展开
3个回答
展开全部
修改Student的toString()方法啊。
public String toString(){
return “姓名:”+this.name + ", 年龄:" + this.age + ", 性别:" + this.sex;
}
就行了。如果想分别获取每一个属性,就添加几个方法
class Student{
String name;
int age;
String sex;
public Student(String _name,int _age,String _sex){
this.name = _name;
this.age = _age;
this.sex = _sex;
}
public String toString(){
return name + "," + age + "," + sex;
}
public String getName(){
return name;
}
public String getAge(){
return age;
}
public String getSex(){
return sex;
}
}
实例化对象后,对象的getName()取姓名 getAge()取年龄 getSex()去性别
public String toString(){
return “姓名:”+this.name + ", 年龄:" + this.age + ", 性别:" + this.sex;
}
就行了。如果想分别获取每一个属性,就添加几个方法
class Student{
String name;
int age;
String sex;
public Student(String _name,int _age,String _sex){
this.name = _name;
this.age = _age;
this.sex = _sex;
}
public String toString(){
return name + "," + age + "," + sex;
}
public String getName(){
return name;
}
public String getAge(){
return age;
}
public String getSex(){
return sex;
}
}
实例化对象后,对象的getName()取姓名 getAge()取年龄 getSex()去性别
追问
谢谢了,有点像JavaBean哦
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
修改 toString()
public String toString(){
return "姓名"+this.name + ", 年龄" + this.age + ", 性别" + this.sex;
}
public String toString(){
return "姓名"+this.name + ", 年龄" + this.age + ", 性别" + this.sex;
}
更多追问追答
追问
只能这样吗?有没有办法分别获取Name,age和Sex?
追答
修改这个
while(iterator.hasNext())
System.out.println(iterator.next());
为:
while(iterator.hasNext()){
Student s = (Student)iterator.next() ;
print("姓名: "+s.name);
print(", 年龄: "+s.age) ;
println(", 性别: "+s.sex);
}
外,迭代器的话 用 for in语句更方便
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
、、、
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询