类集框架(一)

1.单值保存的最大父接口:Collection

在Collection中定义了15个方法,在所以的方法中,只有两个方法最为常用:add(),iterator().从开发来讲,很少直接使用Collection。

2.允许重复的子接口:LIst

public interface List extends Collection
List接口继承了Collection接口,但是List接口对Collection接口进行了大量的扩充。

No方法名称描述
1public E get(int index)取得指定索引位置上的数据
2public E set(int index,E element)修改指定索引位置上的数据
3public ListIteratorlistIterator为ListIterator接口实例化

List接口有两个常用的子类ArrayList和Vector

2.1 ArrayList

public class ArrayList
extends AbstractList
implements List ,RandomAccess,Coloneable,Serializable
使用ArrayList的主要目的是为List接口实例化,所以操作方法都以List接口为主

例:使用ArrayList进行List接口的功能验证
import java.util.ArrayList;
import java.util.List;
public class test{
public static void main(String[] args) {
List<String>all=new ArrayList<String>();//实例化List接口
all.add("hello");//添加内容
all.add("hello");
all.add("World");
for(int x = 0 ;x<all.size();x++){
System.out.println(all.get(x));
}

程序运行结果:[hello, hello,World]

2.2 Vector

例:使用Vector进行List接口的功能验证
import java.util.Vector;
import java.util.List;
public class test{
public static void main(String[] args) {
List<String>all=new Vector<String>();//实例化List接口
all.add("hello");//添加内容
all.add("hello");
all.add("World");
for(int x = 0 ;x<all.size();x++){
System.out.println(all.get(x));
}

程序运行结果:[hello, hello,World]

ArrayList 和Vector 都是List接口的子类

ArrayList和Vector区别

No区别ArrayListVector
1推出时间JDK1.2JDK1.0
2性能采用异步处理方式,性能更高采用同步处理方式,性能相对较低
3安全性非线程安全线程安全
4输出Iterator、ListIterator、foreachIterator、ListIterator、foreach、Enumeration

3.不允许重复的子接口:Set

public interface Setextends Collection
Set子接口完整的继承了Collection接口,Set子接口常用的两个子类为HashSet和TreeSet

3.1散列存放的子类:HashSet

HashSet使用一种散列(无序)的方式保存集合数据

例:使用Set接口
import java.util.HashSet;
import java.util.Set;
public class test{
public static void main(String[] args) {
Set<String> all=new HashSet<String>();
all.add("hello");
all.add("hello");
all.add("world");
System.out.println(all);
}
}

程序运行结果:[world, hello]

使用Set保存数据的时,集合中重复数据没有保存,并且无序

3.2排序存放的子类:TreeSet

使用TreeSet
import java.util.Set;
import java.util.TreeSet;
public class test{
public static void main(String[] args) {
Set<String> all=new TreeSet<String>();
all.add("c");
all.add("b");
all.add("a");
System.out.println(all);
}
}

程序运行结果:[a, b, c]

TreeSet排序,自定义类排序,使用Comparable

Source>Generate hashSet

4.集合的输出操作

4种输出方式:Iterator,ListIterator,Enumeration,foreach

4.1迭代输出:Iterator

Iterator中常用方法

No方法名称描述
1public boolean hasNext()判断是否有下一个元素
2public E next()取出当前元素
3public void remove()移除当前元素

如何取得Iterator的实例化对象?Collection继承了一个Iterator接口,在Iterator接口中定义了一个方法“public Iteratoriterator()

例:使用Iterator输出集合数据
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class test{
public static void main(String[] args) {
List<String> all=new ArrayList<String>();
all.add("hello");
all.add("hello");
all.add("world");
Iterator<String>iter =all.iterator();
while(iter.hasNext()){
String str = iter.next();
System.out.print(str+" ");
}
}
}

程序运行结果:hello hello world

在项目开发中,只要遇到集合对象输出问题,一定要使用Iterator接口完成

4.2 双向迭代输出:ListIterator(了解)

Iterator可以完成由前向后的单向输出操作,如果希望完成由前向后和由后向前输出的话可以利用ListIterator接口完成

ListIterator接口的扩充方法

No方法名称描述
1public boolean hasPrevious()判断是否有前一个元素
2public E previous()取出前一个元素

如果要取得ListIterator接口的实例化对象,只能后依靠List接口,在List接口中存在方法,为ListIterator接口实例化:public ListIteratorlistIterator()

例:执行双向迭代
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
public class test{
public static void main(String[] args) {
List<String> all=new ArrayList<String>();
all.add("hello");
all.add("hello");
all.add("world");
ListIterator<String>iter =all.listIterator();
System.out.print("从前向后输出");
while(iter.hasNext()){
String str = iter.next();
System.out.print(str+" ");
}
System.out.print("\n"+"从后向前输出");
while(iter.hasPrevious()){
String str =iter.previous();
System.out.print(str+" ");
}
}
}

程序运行结果:
从前向后输出hello hello world
从后向前输出world hello hello

对于由后向前的输出操作,在进行前一定要首先发生由前向后的输出。由于此输出接口只有List可以使用,所以在开发中几乎不会出现

4.3 Enumeration

Enumeration是最早的输出接口,最早称为枚举,在JDK1.0时已经推出,在JDK1.5的时候进行了扩充,主要增加了泛型,在Enumeration接口里面只定义了两个方法

No方法名称描述
1public boolean hasMoreElements()判断是否有下一个值
2public E nextElement()取出当前元素

要取得Enumeration的实例化对象,不能依靠Collection接口,只能依靠Vector,在Vector中定义了方法public Enumreationelements()

例:使用Enumreation进行输出
import java.util.Enumeration;
import java.util.Vector;
public class test{
public static void main(String[] args) {
Vector<String> all=new Vector<String>();
all.add("hello");
all.add("hello");
all.add("world");
Enumeration<String>enu =all.elements();
System.out.print("从前向后输出");
while(enu.hasMoreElements()){
String str = enu.nextElement();
System.out.print(str+" ");
}
}
}

程序结果输出:从前向后输出hello hello world

4.4 foreach

例foreach
import java.util.ArrayList;
import java.util.List;
public class test{
public static void main(String[] args) {
List<String> all=new ArrayList<String>();
all.add("hello");
all.add("hello");
all.add("world");
for(String str:all){
System.out.println(str+" ");
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Maybe_ch

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值