情形:实际中将数据存储在ArrayList里面了,然后需取得索引为index的那个数据,再删除ArrayList中那个index索引所对的数据。
其实是android中ListView中的数据源,删除某item,然后需要将这个item所对应的对象传到asynctask中,数据库对比,删除此对象。
问题:因为将ArrayList中将要删除的index对应的对象取出来了,然后再remove,ArrayList中的数据。那么刚刚取出来的数据是不是变为null了。
简略代码:
<span style="font-size:18px;">package com.robot.test;
public class Student{
public int age;
public String name;
Student(int age, String name) {
this.age = age;
this.name = name;
}
}</span>
<span style="font-size:18px;">package com.robot.test;
import java.util.ArrayList;
public class TestCode {
public static void main(String[] args) {
ArrayList<Student> stuList = new ArrayList<Student>();
Student s1 = new Student(12, "jack");
Student s2 = new Student(23, "Ann");
Student s3 = new Student(76, "Mike");
stuList.add(s1);
stuList.add(s2);
stuList.add(s3);
Student stu = stuList.get(0); // 获取ArrayList的第一个值
stuList.remove(0); // 删除ArrayList的第一个值
System.out.println(stu.age);
System.out.println(stu.name);
System.out.println(stuList.size());
}
}</span>
实际对象并没有被删除。
如图所示: