linsir6 / androidnote Goto Github PK
View Code? Open in Web Editor NEWAndroid基础知识、Android进阶知识、Android自定义View相关、面试相关的知识,欢迎fork,star~
License: Apache License 2.0
Android基础知识、Android进阶知识、Android自定义View相关、面试相关的知识,欢迎fork,star~
License: Apache License 2.0
https://github.com/linsir6/AndroidNote/blob/master/AndroidNote/Android%E8%BF%9B%E9%98%B6/Android%E5%86%85%E5%AD%98%E6%B3%84%E6%BC%8F%E6%80%BB%E7%BB%93.md
2、当集合里面的对象属性被修改后,再调用remove()方法时不起作用。
例如:
public static void main(String[] args)
{
Set<Person> set = new HashSet<Person>();
Person p1 = new Person("唐僧","pwd1",25);
Person p2 = new Person("孙悟空","pwd2",26);
Person p3 = new Person("猪八戒","pwd3",27);
set.add(p1);
set.add(p2);
set.add(p3);
System.out.println("总共有:"+set.size()+" 个元素!"); //结果:总共有:3 个元素!
p3.setAge(2); //修改p3的年龄,此时p3元素对应的hashcode值发生改变
set.remove(p3); //此时remove不掉,造成内存泄漏
set.add(p3); //重新添加,居然添加成功
System.out.println("总共有:"+set.size()+" 个元素!"); //结果:总共有:4 个元素!
for (Person person : set)
{
System.out.println(person);
}
}
实际测试可以 remove
函数dui
是个构造函数,当通过new
来调用时,dui
内部的this
指向的是当前新建出来的对象,所以this.list
指向的是新建对象自己的list。
原本程序的运行结果之所以会让人觉得“_dui,和_dui2是会想互影响的”,原因在于函数_insert
和函数print
内部的list
指向的其实是外部全局的list
。
同理,修改后的程序运行结果其实也是不正确的。函数dui
内部的list
在去掉this
前缀之后也是指向外部全局环境的list
的,只是它在每次new
操作的时候都被指向了传参进来的新数组。
如果在修改后的程序后面再执行一次_dui.print()
,你会发现_dui
的list
是跟_dui2
的list
一样的。
请问大神你有写博客吗?给个地址吧,已关注,希望大神持续更新
git 分支怎么合并到主分支呢?有冲突怎么解决呢?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.