ArrayList调用remove方法需要注意的地方
ArrayList中有remove 方法和 removeAll方法,
ArrayList中不仅继承了接口Collection中的remove方法,而且还扩展了remove方法。
Collection中声明的接口为 public boolean remove(Object o)
public boolean removeAll(Collection<?> c)
ArrayList中含有的方法为public E remove(int index)
实际编程中可能会通过一个Collection来调用remove接口,这种情况下不会报错,但是程序什么也不做。
import java.util.ArrayList;
import java.util.Collection;
import java.util.List; public class ArrayList01 { public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> allList = null;
Collection<String> allCollection = null; allList = new ArrayList<String>();
allCollection = new ArrayList<String>(); allList.add("hello");
allList.add(0, "world");
System.out.println(allList);
allCollection.add("Yes");
allCollection.add("Good");
allList.addAll(allCollection);
allList.addAll(0, allCollection); System.out.println(allList); allList.remove(allCollection);
//allList.removeAll(allCollection);
System.out.println(allList);
} }
实际编程可以插入任意对象,但是如果想要通过remove(object o)来删除某个对象,这个对象必须是系统自定义的对象,如果不是的话,需要在类中覆写Object类的equals()及hashCode()方法。
HashSet 特点:
里面元素不能重复,而且采用散列的存储方式,没有顺序。
TreeSet 特点:
可以对输入的数据进行有序的排列。
虽然如此,但是对于自定义的类,如果没有制定好排序规则,会出现异常,因此必须实现comparable接口才能正常使用TreeSet。实现comparable接口还需注意,如果对于类中某个属性没有进行比较的指定,则也会认为是同一个对象,比如Man{name = tom, score = 20}; Man { name = Jim,score = 20};如果只是指定score的比较,而没有指定名字的比较,则会认为两个Man对象是同一个对象。
而对于HashSet,如果想要除去其中所有的重复元素,必须覆写Object类中的equals()方法和hashCode()方法,后者是表示一个哈希编码,可以理解为一个对象的编码。
import java.util.HashSet;
import java.util.Set; class Per{
private String name;
private int age;
public Per(String name, int age) {
// TODO Auto-generated constructor stub
this.name = name;
this.age = age;
} public boolean equals(Object obj){
if (this == obj) { //地址相等时是同一个对象
return true;
}
if (!(obj instanceof Per)) { //传进来的不是本类的对象 不是同一个对象
return false;
} Per p = (Per)obj; //进行向下转型
if (this.name.equals(p.name)&&this.age==p.age) { //属性依次比较,如果相等,是同一个对象,不等不是同一个对象
return true;
}else {
return false;
}
} public int hashCode(){
return this.name.hashCode()*this.age; //覆写hashCode 方法,指定编码公式
} public String toString(){
return "姓名:"+this.name+"; 年龄: "+this.age;
}
} public class SetDemo02 { public static void main(String[] args) {
// TODO Auto-generated method stub
Set<Per> allSet = new HashSet<Per>();
allSet.add(new Per("Tom", 30));
allSet.add(new Per("Jim", 31));
allSet.add(new Per("Tony", 32));
allSet.add(new Per("Tony", 32));
allSet.add(new Per("Tony", 32));
allSet.add(new Per("Jack", 29));
allSet.add(new Per("Mark", 33)); System.out.println(allSet); } }
ArrayList调用remove方法需要注意的地方的更多相关文章
- ArrayList调用remove(int index)抛出UnsupportedOperationException问题分析以及解决记录
使用Arrays转数组成为List后,不能调用add(...)和remove(...)方法,此时如果调用就会抛出UnsupportedOperationException异常 原因 其实Arrays. ...
- List 调用 remove 方法时抛出 java.lang.UnsupportedOperationException 异常原因
原因 使用 Arrays.asList(arr) 转换的 List 并不能进行 add 和 remove 操作. Arrays.asList(arr) 返回的类型是 Aarrays$Arr ...
- Java基础(37)ArrayList的remove方法
1.问题描述 给定两个字符串 s 和 t,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. 输入: s = "abcd& ...
- java ArrayList的remove()方法的参数为int和Integer的问题
ArrayList的父类List中,有2个remove重载方法: remove(int index) remove(Object o) 假如参数输入为数字类型,到底是删除值等于该数字的对象还是删除索引 ...
- Java list.remove( )方法需要注意的地方
List<Integer> integerList = new ArrayList<>(); 当我们要移除某个Item的时候 remove(int position):移除某个 ...
- List的remove方法里的坑
今天遇到一件怪事,用一个ArrayList添加了一个对象,再调用ArrayList的remove方法删除该对象,当然这时对象是数据库里查出来的,但内容绝对是一样,却发现remove失败了.演示一下,这 ...
- java 数据类型:集合接口Collection之List~ArrayList:remove移除;replaceAll改变原有值;sort排序;迭代器listIterator();
什么是List集合: 特点: 元素有序可重复的集合. 集合中每个元素都有其对应的顺序索引. List集合默认按元素的添加顺序设置元素的索引,索引从0开始. List接口的常用方法: List可以使 ...
- EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解
前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...
- List<T>的IndexOf方法和Remove方法
Microsoft地址 List<T>的IndexOf()方法 如果T是值类型的,就按照比较值的方法从列表的第一个元素开始逐个匹配,如果T是引用类型,就比较引用是否相同 举例如下: cla ...
随机推荐
- Sql server之路 (一)基础学习
查询 1.Select * from表名 2.Select 字段1,字段2,from表名 3.Select 字段1,字段2,...from表名 where 字段1 in('内容') 插入 1.inse ...
- mysql事务处理的意义
MySQL的事务支持不是绑定在MySQL服务器本身,而是与存储引擎相关 1.MyISAM:不支持事务,用于只读程序提高性能 .InnoDB:支持ACID事务.行级锁.并发 .Berkeley DB:支 ...
- Azure Blob Storage从入门到精通
今天推荐的是一个系列文章,让读者阅读完成后可以对Azure Blob Storage的开发有一个全面的了解,可谓是从入门到精通. Azure在最初的版本里面就提供了非结构化数据的存储服务,也即Blob ...
- HDU 4374 One hundred layer DP的单调队列优化
One hundred layer Problem Description Now there is a game called the new man down 100th floor. The ...
- 终于解决SQL Server 2008 64位系统无法导入Access/Excel的问题 2012/08/01
最近更换了新服务器,操作系统Windows Server 2008 X64,数据库SQL Server 2008 X64,Office 2007(好像只有32位),在存储过程执行OpenDatasou ...
- LIB 配置文件读写器
由于读写配置文件的次数比较频繁,而且拥有众多的类库,到最后,直接被各种各样的类库烦死. 顺手封一个简单,实用的.主要功能: 读写AppSetting 读取连接字符串 读取自定义配置节 using Sy ...
- 获取当前的时间,转化为char[]格式unix时间戳
/* 在这个程序当中实现获取当前的unix时间戳 转化为char[] */ #include<stdio.h> #include<stdlib.h> #include<t ...
- 20130617 hbase regionserver 老挂掉
hbase regionserver 老挂掉: 添加如下: <property><name>hbase.regionserver.restart.on.zk.expire< ...
- c++ insert iterators 插入型迭代器
insert iterators 插入型迭代器 (1)front inserters 前向插入迭代器 只适用于提供有push_front()成员函数的容器,在标准程序库中这样的容器是deque和lis ...
- linux下C语言获取微秒级时间
使用C语言在linux环境下获得微秒级时间 1.数据结构 int gettimeofday(struct timeval*tv, struct timezone *tz); 其参数tv是保存获取时间结 ...