您的位置:首页 > 编程语言 > Java开发

Java基础学习笔记之七(2)--List&Set

2013-01-26 10:43 501 查看

***List 接口

An ordered collection (also known as a sequence).

Unlike sets, lists typically allow duplicate elements.

List接口是Collection的子接口,实现List接口的容器类中的元素

是有顺序的,而且是可以重复的。

List容器中的元素都对应一个整数型的序号,记载其在容器中的位置,

可以根据序号取容器中的元素。

ArrayList--底层是数组实现的List

LinkedList--底层是链表实现的List

常用方法:

1.Object get(int index)

Returns the element at the specified position in this list.

返回此列表中指定位置上的元素

2.Object set(int index,Object element)

Replaces the element at the specified position in this list

with the specified element

用指定的元素替代此列表中指定位置上的元素。

3.void add(E e)

Appends the specified element to the end of this list

将指定的元素追加到此列表的尾部。

4.void add(int index,E element)

Inserts the specified element at the specified position

in this list

将指定的元素插入此列表中的指定位置

5.Object remove(int index);

Removes the element at the specified position in this list.

移除此列表中指定位置上的元素。

6.int indexOf(Object o)

Returns the index of the first occurrence of the specified element

in this list

搜索指定元素在列表第一次出现的位置,如果没有返回-1

7.int lastIndexOf(Object o)

Returns the index of the last occurrence of the specified element

in this list.

返回指定的对象在列表中最后一次出现的位置索引。

***Set 接口

A collection that contains no duplicate elements.

一个不包含重复元素的 collection。更正式地说,set 不包含

满足 e1.equals(e2) 的元素对 e1 和 e2,并且最多包含一个 null 元素。

主要实现类HashSet,可以用来过滤数据。Set方法参考API

***对比

选择哪种数据结构--读的效率和改的效率

Array--读快,改慢(程序读的多时选择)

Linked--读慢,改快(程序改的多时选择)

Hash--两者之间

参考文章:

1.ArrayList 源码分析 /article/7665020.html

2.Java数组与内存控制 /article/8006001.html

3.数组在内存中的存放形式 http://www.linuxidc.com/Linux/2012-10/72887.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐