首页 > 编程知识 正文

集合类型,常用的集合

时间:2023-05-06 18:46:26 阅读:162238 作者:4277

虽然可以将集合类(容器)集合理解为动态数组,但他将其与传统数组区分开。 传统阵列是静态、线性的,长度是固定的。 传统数组通过下标控制元素,数组的线性顺序由下标决定。 动态数组,顾名思义,是可以改变长度的数组。 可以实现多个对象的存储。 传统排列的长度决定了就不能改变。 如果以后需要将元素添加到此数组中,或者需要删除元素,则会受到很大限制。 数组的长度是固定的,意味着分配空间是固定的,内存空间是有大小的。

添加元素:添加长度大于此长度的元素将导致ArrayIndexOutOfBoundsException异常,并且无法正常工作。

删除要素:删除原理是某个要素为空指向状态,指针不指向它。 将该位置的元素修改为null不等于删除。

在传统的数组中,不能指向天空。

因此,从JDK1.2中引入了集合类

利用JDK1.8改进算法

集合类摘要集合类主要用于全面实现和包装常见的数据结构,并提供一组接口和子类。 依次帮助用户减少数据结构带来的开发困难。

的类都继承Object,集合类也是。

List集合List集合由Collection (单值集合操作的最大父接口)继承

List集合的特征要素允许各要素重复的顺序是插入对象的顺序List接口的安装类即ArrayList类。 List收藏的使用率为90%

优点:实现了可以保存包括null在内的所有元素的可变数组。 可以根据索引位置快速随机访问集合

缺点:指向索引位置插入或删除对象的速度很慢。

2 .链接列表类(8% ) ) ) ) ) ) )。

使用链表结构保存对象。 链表的顺序由每个对象中的指针决定。 也就是说,通过指针前后相连,不能像数组那样直接根据脚标记选择指定的对象。

优点:这种结构的优点是向集合插入、删除对象容易且高效

缺点:使用LinkedList类对随机访问集合中的对象实现List集合效率不高。 (查询单个对象时,时间复杂度为o(n ),并且必须遍历整个集合,因为链表只能以前后相邻的面向对象的方式找到。)

3 .载体类(2% ) ——转载

向量被同步访问

向量包含许多传统方法,这些方法不属于集合框架。

Vector主要用于事先不知道数组的大小,或者需要可以调整大小的数组的情况。

Vector类支持四种结构方法。

详情请参阅3359 www.run OOB.com/Java/Java-vector-class.html .我想我们可以根据各自的优缺点,根据各自的优点,在需求不同的时候使用。 例如,ArrayList的访问很快,查询数据时,ArrayListArrayList比较合适。 链接列表用于修改等。

ArrayList子类继承结构

示例import java.util.ArrayList; import java.util.List; public class test { publicstaticvoidmain (string args [ ] ) liststringall=newarrayliststring ); all.add(ab ); all.add(CD ); all.add(ef ); system.out.println(all; system.out.println(all.get(0) ); //集合的索引也同样从0开始。 }

Java中没有指针。 实现链表练习代码interface ILinkE {//以通用地避免安全问题的公共语音添加(e ); //数据public int size (增加; //获取数据的个数publicvoiddelete(e ); //输出链表数据public boolean isEmpty (); //空集合public Object toArray (判断是否为; //获得集合数据publiceget(intindex )//获得对应索引的数据publicvoidset(intindex,E data ); //基于给定索引修改该索引上的数据publicbooleancontains(edata ); //确定数据簿中是否存在公共void客户端(); //收藏清除} classlinkimpleimplementsilinke {// ——3354——3——3——3——33——33——33——33——333——33——3333——33333——33——33——333——3333——333——333333——333333 3——3——3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333.////需要存储的/需要存储的数据privatprivivivivate NTE NTE natintivintid NTE n //下一个引用private Object returnData[]; 公共节点(edata )//存储数据this.data=data; //外部连接publicvoidaddnode(nodenewnode )//判断当前所在节点if ) this.next==null { this.next=new node; //将下一个节点指向添加的节点元素}

else {this.next.addNode(newNode);//如果下一个节点有元素了,继续向后递归调用此方法,直到下一个节点为空,然后添加元素}}//连接外部,实现集合内容获取public void toArrayNode() {LinkImpl.this.returnData[LinkImpl.this.foot ++] = this.data ;//将每个节点的数据依次存入数组,遇到空元素时停止存入if(this.next != null) {//当下一个节点还有元素时this.next.toArrayNode();//递归存入}}//连接外部,实现指定索引的内容获取public E getNode(int index) {if(LinkImpl.this.foot ++ == index) {//根据脚标是否相等,返回相应元素return this.data;} else {return this.next.getNode(index);//脚标不相等,递归,脚标向后指}}//连接外部的set方法,实现内容修改public void setNode(int index ,E data) {//类似getNode 方法if(LinkImpl.this.foot ++ == index) {this.data = data;} else {this.next.setNode(index ,data);}}//连接外部的contains方法,实现内容查询public boolean containsNode(E data) {if(this.data.equals(data)) {return true;}else {if(this.next == null) {return false;}else {return this.next.containsNode(data);//递归,直到内容相同}}}//连接外部,实现指定内容的删除public void deleteNode(Node prev ,E data) {if(this.data.equals(data)) {prev.next = this.next;} else {if(this.next != null) {this.next.deleteNode(this, data);//向后继续删除}}}}//————————————————————————Link类中定义的成员和方法————————————————————private Node root;//保存根元素private int count;//次数private Object[] returnData;//数组存储信息private int foot ;//存储脚标public void add(E e) {//添加数据方法if(e == null) {//如果添加的没有数据,则结束方法return ;}//如果添加的有数据,则开始检测Node newNode = new Node(e);//首节点没有数据时,此时已经将调用该方法的数据赋予给临时内部类对象newNodeif(this.root == null) {//首节点为空时,即此时只有空的表头,应该先将表头给值。this.root = newNode;//将第一个节点作为根节点}else {this.root.next = newNode;}this.count ++;//调用多少次add方法即增加多少数据}public int size() {//获得数据个数return count;}public boolean isEmpty() {//返回是否为空集合判断return this.root == null;}public Object[] toArray() {//获取集合信息if(this.isEmpty()) {return null;}else {this.foot = 0 ;this.returnData = new Object[this.count] ;//根据现在已有额长度开辟一个数组//利用Node类进行数据递归存取this.root.toArrayNode();return this.returnData;}}public E get(int index) {//根据索引输出元素if(index > this.count) {return null;}//索引类的数据获取应该由Node类完成this.foot = 0;return this.root.getNode(index);}//修改指定索引数据public void set(int index ,E data) {if(index >= this.count) {return;}this.foot = 0;this.root.setNode(index, data);}//判断数据是否存在public boolean contains(E data) {if(data == null)return false;return this.root.containsNode(data);}//删除集合元素public void delete(E data) {if(this.contains(data)) {if(this.root.data.equals(data)) {this.root = this.root.next;} else {this.root.next.deleteNode(this.root, data);//递归调用内部类删除方法,下一个继续查询,直到有与data内容相同的元素}this.count --;}}//集合清空public void clean() {this.root = null;this.count = 0;}}public class Test{public static void main(String[] args) {//LinkImpl<String> out = new LinkImpl<String>();System.out.println("数据集合创建完毕");System.out.println("数据添加之前拥有个数:" + out.size() + "t空集合?" + out.isEmpty());out.add("数据一");out.add("数据二");System.out.println("添加后拥有数据个数:" + out.size() + "t空集合?" + out.isEmpty());//修改第二个数据为666out.set(1, "666");//输出集合所有数据System.out.println("数据集合:");{Object res[] = out.toArray();for(Object obj : res) {System.out.println(obj);}}//获取集合第一个数据System.out.println("集合第一个数据:" + out.get(1));//查询是否有“六零五”这一个数据System.out.println("数据“六零五”是否存在?" + out.contains("六零五"));//删除数据一out.delete("数据一");System.out.println("删除数据一后的集合:");{Object res[] = out.toArray();for(Object obj : res) {System.out.println(obj);}}out.clean();//已清空集合System.out.println(out.get(0));}}

运行结果

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。