Java对象的引用

情形:实际中将数据存储在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>


可以看到这里那个被删除的对象还是存在的,因为其被stu所引用了。删除只是删除ArrayList中的引用。

实际对象并没有被删除。

如图所示:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值