JAVA语言程序设计集合中元素的修改。
@SuppressWarnings("unchecked")publicclassTest{publicstaticvoidmain(String[]args){try{...
@SuppressWarnings("unchecked")
public class Test{
public static void main(String[] args){
try{Student s1=new Student("张三","2018120018","创智181","男",20,"19981218","中国","创新创业学院");
Student s2=new Student("李四","2018120011","创智181","男",19,"19990810","中国","创新创业学院");
Student s3=new Student("王五","2018120068","创智181","男",18,"20001113","中国","计算机学院");
Student s4=new Student("赵六","2018120078","创智181","男",16,"20010506","中国","计算机学院");
System.out.println(s1.toString());
System.out.println(s2.toString());
System.out.println(s3.toString());
System.out.println(s4.toString());
ArrayList arrayList1=new ArrayList();
arrayList1.add(s1);
arrayList1.add(s2);
arrayList1.add(s3);
arrayList1.add(s4);
Iterator iterator1=arrayList1.iterator();
while(iterator1.hasNext())
System.out.println(iterator1.next());
s1.setsage(15);
s1.setsdept("商学院");
System.out.println(s1.toString());
arrayList1.add(s1);
while(iterator1.hasNext())
System.out.println(iterator1.next());
}
}
} 展开
public class Test{
public static void main(String[] args){
try{Student s1=new Student("张三","2018120018","创智181","男",20,"19981218","中国","创新创业学院");
Student s2=new Student("李四","2018120011","创智181","男",19,"19990810","中国","创新创业学院");
Student s3=new Student("王五","2018120068","创智181","男",18,"20001113","中国","计算机学院");
Student s4=new Student("赵六","2018120078","创智181","男",16,"20010506","中国","计算机学院");
System.out.println(s1.toString());
System.out.println(s2.toString());
System.out.println(s3.toString());
System.out.println(s4.toString());
ArrayList arrayList1=new ArrayList();
arrayList1.add(s1);
arrayList1.add(s2);
arrayList1.add(s3);
arrayList1.add(s4);
Iterator iterator1=arrayList1.iterator();
while(iterator1.hasNext())
System.out.println(iterator1.next());
s1.setsage(15);
s1.setsdept("商学院");
System.out.println(s1.toString());
arrayList1.add(s1);
while(iterator1.hasNext())
System.out.println(iterator1.next());
}
}
} 展开
3个回答
展开全部
ArrayList里放的是引用,修改完不用再add一次。
import java.util.ArrayList;
public class Test {
public static void main(final String[] args) {
final Student s1 = new Student("张三", "2018120018", "创智181", "男", 20, "19981218", "中国", "创新创业学院");
final Student s2 = new Student("李四", "2018120011", "创智181", "男", 19, "19990810", "中国", "创新创业学院");
final Student s3 = new Student("王五", "2018120068", "创智181", "男", 18, "20001113", "中国", "计算机学院");
final Student s4 = new Student("赵六", "2018120078", "创智181", "男", 16, "20010506", "中国", "计算机学院");
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
System.out.println(s4);
System.out.println("-----------");
final ArrayList<Student> arrayList1 = new ArrayList<>();
arrayList1.add(s1);
arrayList1.add(s2);
arrayList1.add(s3);
arrayList1.add(s4);
for (final Student s : arrayList1)
System.out.println(s);
System.out.println("------------");
s1.setAge(15);
s1.setDept("商学院");
System.out.println(s1);
System.out.println("------------");
for (final Student s : arrayList1)
System.out.println(s);
}
}
你没贴Student类,凑合写了一点,仅作演示:
public class Student {
private int age;
private String dept;
private final String name;
private final String birthday;
public Student(final String name, final String birthday, final String string3, final String string4, final int age,
final String string5, final String string6, final String dept) {
this.name = name;
this.birthday = birthday;
this.age = age;
this.dept = dept;
}
public void setAge(final int i) {
this.age = i;
}
public void setDept(final String dept) {
this.dept = dept;
}
@Override
public String toString() {
return String.format("[%s %s %d %s]", name, birthday, age, dept);
}
}
运行结果:
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询