ArrayList-VS-LinkedList
ArrayList 是List接口的实现类;底层的数据结构是数组,而LinkedList底层数据结构是双向循环链表。
所以在查询时ArrayList效率高,增删时LinkedList高。由于List中有索引,可以在指定位置插入。在iterator迭代器取元素时,在调用Arraylist中的iterator()返回的其实是Iterator接口实例, Iterator it = new Str();
而Str是ArrayList的一个内部私有类,实现了Iterator接口,所以有next(),hasNext(),..方法。而ListIterator 是Iterator的子接口。在remove()或contain()方法中调用的是ArrayList中的equals比较对象是否存在。可以通过重写看到。
public class ArrayListTest {
public static void main(String[] args) {
List list = new MyList();
list.add(new Person5("haha",34));
list.add(new Person5("haha",34));
list.add(new Person5("haha",34));
//System.out.println(list.remove(new Person5("haha",34)));
System.out.println(list.remove(new Person5("haha",34)));
ListIterator it =list.listIterator();
while(it.hasNext()){
Person5 p = (Person5)it.next();
System.out.println(p.getName()+"--------"+p.getAge());
}
}
}
class Person6 {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person6(String name, int age) {
super();
this.name = name;
this.age = age;
}
public int hashCode(){
System.out.println("----hashCode------");
return this.age;
}
public boolean equals(Object o){
Person5 p =(Person5)o;
System.out.println(this.name+"------equals---"+p.getName());
if(!(p instanceof Person5))
return false;
return this.name.equals(p.getName());
}
}
class MyList extends ArrayList{
@Override
public boolean remove(Object o) {
// TODO Auto-generated method stub
System.out.println("调用了remove方法----");
return super.remove(o);
}
}
ArrayList-VS-LinkedList的更多相关文章
- 深入理解java中的ArrayList和LinkedList
杂谈最基本数据结构--"线性表": 表结构是一种最基本的数据结构,最常见的实现是数组,几乎在每个程序每一种开发语言中都提供了数组这个顺序存储的线性表结构实现. 什么是线性表? 由0 ...
- ArrayList,Vector,LinkedList
在java.util包中定义的类集框架其核心的组成接口有如下:·Collection接口:负责保存单值的最大父接口 |-List子接口:允许保存重复元素,数据的保存顺序就是数据的增加顺序: |-Set ...
- Java数据结构之表的增删对比---ArrayList与LinkedList之一
一.Java_Collections表的实现 与c不同Java已经实现并封装了现成的表数据结构,顺序表以及链表. 1.ArrayList是基于数组的实现,因此具有的特点是:1.有索引值方便查找,对于g ...
- C++模拟实现JDK中的ArrayList和LinkedList
Java实现ArrayList和LinkedList的方式采用的是数组和链表.以下是用C++代码的模拟: 声明Collection接口: #ifndef COLLECTION_H_ #define C ...
- ArrayList与LinkedList用法与区别
1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构. 2.对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedLis ...
- ArrayList vs LinkedList vs Vector
List概览 List,正如它的名字,表明其是有顺序的.当讨论List的时候,最好拿它跟Set作比较,Set中的元素是无序且唯一:下面是一张类层次结构图,从这张图中,我们可以大致了解java集合类的整 ...
- ArrayList 和 LinkedList 的区别
1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构.2.对于随机访问get和set,ArrayList优于LinkedList,因为LinkedList要移动 ...
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析(转)
主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性能测试对比,根据ArrayList和LinkedList的源码实现分析性能结果,总结结论. 通过本文你可以 ...
- ArrayList和LinkedList的几种循环遍历方式及性能对比分析
最新最准确内容建议直接访问原文:ArrayList和LinkedList的几种循环遍历方式及性能对比分析 主要介绍ArrayList和LinkedList这两种list的五种循环遍历方式,各种方式的性 ...
- 集合中list、ArrayList、LinkedList、Vector的区别、Collection接口的共性方法以及数据结构的总结
List (链表|线性表) 特点: 接口,可存放重复元素,元素存取是有序的,允许在指定位置插入元素,并通过索引来访问元素 1.创建一个用指定可视行数初始化的新滚动列表.默认情况下,不允许进行多项选择. ...
随机推荐
- discuz函数quote
public static function quote($str, $noarray = false) { if (is_string($str)) return '\'' . addcslashe ...
- Xcode 3.2.5免证书开发调试
Xcode 3.2.5免证书开发调试 xcode3.2.5 应该没人用了.这里做个保存而已. Xcode编译遇到过 Code Sign error: a valid provisioning prof ...
- ubuntu 下编译内核
目的: 1. 练习.网上有很多类似的文章可供参考. 2. 为写qemu的watchdog驱动练手. 有朋友问make的 watchdog驱动 需要什么准备,所以写这个blog. 环境: ubuntu ...
- 「深入理解计算系统」从Hello World开始
从 hello world 开始 Table of Contents 1 程序源文件 2 程序源文件是什么 3 程序被编译 4 程序运行 4.1 读取命令 4.2 读取指令内容 4.3 执行过程 5 ...
- 利用UIScrollView和UIPageControl实现多页图片欢迎页面
在.h文件当中实现UIScrollViewDelegate协议,让控制器充当代理: #import <UIKit/UIKit.h> @interface RPRootViewControl ...
- Linux下文件及目录的一些操作(附递归遍历目录源码)
1.获取当前工作目录 #include <unistd.h> 1.char *getcwd(char *buf,size_t size); 2. 3.其中,buf为缓冲区地址,size为给 ...
- log4j 详解
转载自:http://www.blogjava.net/hwpok/archive/2008/08/23/223891.html >>>>1. 概述<<< ...
- 2014.9.25DOM元素操作
2.操作样式class a.className=”block” class样式,代码赋值的方式 (五)找相关元素 a.nextSibling 下一层,下一个同辈元素 a.previousSibling ...
- 思考----拒绝单纯copy
工作4个多月以来感触最深的是: 做事情的时候遇到不会的可以上网查或者问别人,但是获取到的知识不能只是单纯的copy过来使用达到要求就ok, 更重要的是事后等有空了一定要仔细研究学习,使知识网络完整,这 ...
- java 中有几种方法可以实现一个线程? 用什么关键字修 饰同步方法? stop()和 suspend()方法为何不推荐使用?
java5 以前, 有如下两种:第一种:new Thread(){}.start();这表示调用 Thread 子类对象的 run 方法, new Thread(){}表示一个Thread 的匿名子类 ...