JAVA类之集合 important

集合:相当于一个容器,只存储对象,长度可以改变;

数组:也相当一个容器,存储对象、基本数据类型,长度不可变;

首先先写一下集合的概念:容纳不同种类的数据建立在未知的基础之上,即java要用有限种类的集合类来容纳无限种类的数据对象;

其实集合类就相当于一个容器;用于存放对象的,而且长度可变;

java中的集合类分为三个类:集(Set)、列表(List)、映射(Map)

(Set):元素无序、不包含重复元素;

列表(List):有序、包含重复元素通过角标操作;

映射(Map):待续。。。。。;

忘了写了重要的一点;java.util.Collection接口是最基本的集合接口,是描述集合Set和列表List类型的跟接口;

Set-------HashSet

Collection-----

LinkedList

List------

Vector------Stack

Map------Hashtable----Properties

Collection接口中的普遍性方法:

publicbooleanadd(Ee):向集合添加一个元素,加入成功返回true,否则返回false;、

publicbooleanremove(Objecto:从元素中删除指定的元素,删除成功返回true,否则false

publicvoidclear():删除集合中的所有元素;

publicbooleancontains(Objecto):判断集合是否包含指定元素;

publicObject[]toArray():返回包含当前集合中所有元素的数组。

publicbooleanisEmpty():判断集合是否为空;

publicintsize:返回集合中的元素个数;

/*

集合:就是一个容器。

1,用于存储对象的。

2,该容器的长度是可变的。

因为装对象的容器内部的数据结构不同,

这些容器很很多中,经过了不断的抽取,就形成了体系。

这个体系我们称之为集合框架。

当我们学习一个体系的时候,

先要明确该体系的基本功能。

演示Collection中的方法。

*/

importjava.util.*;

classArrayListDemo

{

publicstaticvoidmain(String[]args)

{

//1,通过Collection的子类对象,创建一个容器。其实集合对象中存放都是元素的引用(地址)

ArrayListal=newArrayList();

//2,可以Collection的共性方法添加一些元素。

al.add("abc1");

al.add("abc2");

al.add("abc3");

al.add("abc4");

ArrayListal1=newArrayList();

al1.add("qq1");

al1.add("qq2");

al1.add("qq3");

//al1.add("abc1");

//添加一堆元素。

//al.addAll(al1);

//3,删除一个元素。

al.remove("abc3");

//4,交集。

//booleanb=al.retainAll(al1);

//System.out.println("b="+b);

//5,清空集合。

//al.clear();

//获取集合的长度。

//6,判断一个元素。其实用的还是equals方法。判断元素是否相同。

//booleanb=al.contains("abc22");

//System.out.println("b="+b);

//System.out.println(al.size());

iteratorDemo();

//System.out.println(al.toString());//[abc1,abc2,abc4]

/*

Iteratorit=al.iterator();

while(it.hasNext())

{

System.out.println(it.next()+"..");

}

al.retainAll(al1);

Iteratorit1=al.iterator();

while(it1.hasNext())

{

System.out.println(it1.next()+"--");

}

*/

}

publicstaticvoiditeratorDemo()

{

ArrayListal=newArrayList();

al.add("abc1");

al.add("abc2");

al.add("abc3");

al.add("abc4");

/*

什么是迭代器?

就是用于取出集合中元素的对象。

该对象因为每一个容器的数据结构不同,所以实现方式也不一样,而且

迭代器是容器中的内容,所以是通过内部类来实现,也即是进行了容器内部封装。

我们只要通过iterator()方法获取该对象即可操作容器中的元素。

迭代器就如同大型游戏机中的抓布娃娃的游戏机,迭代器就是该游戏机中的那个夹子!

*/

Iteratorit=al.iterator();

while(it.hasNext())

{

System.out.println(it.next());

}

for(Iteratorit1=al.iterator();it1.hasNext();)

{

System.out.println(it1.next());

}

// System.out.println(it.next());

// System.out.println(it.next());

// System.out.println(it.next());

// System.out.println(it.next());

// System.out.println(it.next());//.NoSuchElementException

}

}

/*

建立两个容器,AB

分别给容器中添加元素。

要求:获取A容器中的与B容器交集的元素,

将这些元素打印。

*/

首先讲一下列表List

List集合有ArrayListLinkListVector

List方法:

voidadd(intindex,Eelement):在索引号index后插入element对象。

booleanadd(Ee:将对象e插入到链表的最后;

Eremove(intindex):删除链表里指定索引号的元素;

booleanremove(Objecto):删除链表里的第一个指定内容的元素;

Eget(intindex):得到链表里的指定索引号的元素;

intsize():返回链表里的元素的个数;

intindexOf(Objectobj):如果在链表里找到了obj元素,则返回这个元素的索引值;如果找不到返回-1

List<E>subList(intfromIndex,inttoIndex):得到链表里的从formindex开始,到toIndex结束的子链表;

voidclear():将链表里的存储的元素全部清除掉;

该示例演示的是List接口中的特有方法。

可以通过角标操作元素的方法。

*/

importjava.util.*;

classArrayListDemo2

{

publicstaticvoidmain(String[]args)

{

ArrayListal=newArrayList();

al.add("abc1");

al.add("abc2");

al.add("abc3");

System.out.println(al);

System.out.println("-----------------------");

//1,在指定位置添加元素。

al.add(1,"haha");

//2,修改指定位置的元素。

al.set(2,"qq");

//3,删除指定位置的元素。

al.remove(0);

//4,获取指定元素的位置。

intindex=al.indexOf("qq");

System.out.println("index="+index);

//5,获取子列表。

Listll=al.subList(1,2);

System.out.println("ll:"+ll);

//6,获取元素。

for(intx=0;x<al.size();x++)

{

System.out.println("get("+x+"):"+al.get(x));

}

System.out.println(al);

}

}

实例:ArrayList

构造方法

ArrayList()

构造一个初始容量为10的空列表。

ArrayList(Collection<?extendsE>c)

构造一个包含指定collection的元素的列表,这些元素是按照该collection的迭代器返回它们的顺序排列的。

ArrayList(intinitialCapacity)

构造一个具有指定初始容量的空列表。

方法:(上面重复的太多,只写他新增的了)

intlastIndexOf(Objecto)

返回此列表中最后一次出现的指定元素的索引,或如果此列表不包含索引,则返回-1

Eremove(intindex)

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

booleanremove(Objecto)

移除此列表中首次出现的指定元素(如果存在)。

protectedvoidremoveRange(intfromIndex,inttoIndex)

移除列表中索引在fromIndex(包括)和toIndex(不包括)之间的所有元素。

voidtrimToSize()

将此ArrayList实例的容量调整为列表的当前大小。

importjava.util.*;

classCollectionDemo

{

publicstaticvoidmain(String[]args)

{

ArrayListarr=newArrayList();

arr.add("abc1");

arr.add("abc2");

arr.add("abc3");

arr.add("abc4");

System.out.println(arr.size());

arr.remove("abc3");

arr.clear();

System.out.println(arr.contains("abc3"));

System.out.println(arr);

}

}

LinkedList类:

构造方法:

LinkedList()

构造一个空列表。

LinkedList(Collection<?extendsE>c)

构造一个包含指定collection中的元素的列表,这些元素按其collection的迭代器返回的顺序排列。

普通方法:

voidaddFirst(Ee)

将指定元素插入此列表的开头。

voidaddLast(Ee)

将指定元素添加到此列表的结尾。

voidclear()

从此列表中移除所有元素。

booleancontains(Objecto)

如果此列表包含指定元素,则返回true

Eelement()

获取但不移除此列表的头(第一个元素)。

Eget(intindex)

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

EgetFirst()

返回此列表的第一个元素。

EgetLast()

返回此列表的最后一个元素。

intindexOf(Objecto)

返回此列表中首次出现的指定元素的索引,如果此列表中不包含该元素,则返回-1

intlastIndexOf(Objecto)

返回此列表中最后出现的指定元素的索引,如果此列表中不包含该元素,则返回-1

ListIterator<E>listIterator(intindex)

返回此列表中的元素的列表迭代器(按适当顺序),从列表中指定位置开始。

Eremove()

获取并移除此列表的头(第一个元素)。

Eremove(intindex)

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

booleanremove(Objecto)

从此列表中移除首次出现的指定元素(如果存在)。

EremoveFirst()

移除并返回此列表的第一个元素。

importjava.util.*;

classLinkedListDemo

{

publicstaticvoidmain(String[]args)

{

LinkedListlink=newLinkedList();

link.addFirst("abc1");

link.addFirst("abc2");

link.addFirst("abc3");

link.addFirst("abc4");

while(!link.isEmpty()){

System.out.println(link.removeFirst());

}

}

}

/*输出

abc4

abc3

abc2

abc1*/

未完。。。。。。。。待续。。。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值