当前位置: 首页 > news >正文

零基础学JAVA--Day31(Collection类+List类)

集合

简单介绍

数组
1)长度开始时必须指定,而且一旦指定,不能更改
2)保存的必须为同一类型的元素
3)使用数组进行增加/删除元素的示意代码-比较麻烦

集合
1)可以动态保存任意多个对象,使用比较方便!
2)提供了一系列方便的操作对象的方法:add、remove、set、get等
3)使用集合添加,删除新元素的示意代码-简洁了

1、集合主要是两组(单列集合,双列集合)
2、Collection接口有两个重要的子接口ListSet,他们的实现子类都是单列集合
3、Map接口的实现子类是双列集合,存放的K-V(键值对)

Collection方法

简单介绍

public interface Collection<E> extends Iterable<E>
1)collection实现子类可以存放多个元素,每个元素可以是Object
2)有些Collection的实现类,可以存放重复的元素,有些不可以
3)有些Collection的实现类,有些是有序的(List)(存入顺序=取出顺序),有些不是有序(Set)
4)Collection接口没有直接的实现子类,是通过它的子接口Set和List来实现的

常用方法

以ArrayList为例(只要是继承了Collections接口的类都可以使用)

package com.Collection1;import java.util.ArrayList;public class CollectionMethods {@SuppressWarnings("all")public static void main(String[] args) {ArrayList list = new ArrayList();//add 添加对象list.add("aaa");list.add(1);list.add(false);//remove 删除对象list.remove(1);//按照索引删除list.remove(false);//按照元素删除//contains 查找元素对象  返回True Falselist.contains("aaa");//size 获取集合元素个数list.size();//isEmpty 判断集合是否为空list.isEmpty();//clear 清空数组list.clear();//addAll 添加多个元素ArrayList list1 = new ArrayList();list1.add("adada");list.addAll(list1);//containsAll 查找多个元素,返回True Falselist.containsAll(list1);//removeAll  删除多个元素list.removeAll(list1);}
}

遍历的两种方法

迭代器遍历

Collection接口遍历元素方式1-使用Iterator(迭代器)只要是继承了Collections接口的类都能使用

1)Iterator对象称为迭代器,主要用于遍历Collection集合中的元素,。
2)所有实现了Collection接口的集合类都有一个iterator()方法,用以返回一个实现了Iterator接口的对象,即可以返回一个迭代器。
3)Iterator的结构

4)Iterator仅用于遍历集合,Iterator本身并不存放对象。

package com.Collection1;import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;public class CollectionIterator {@SuppressWarnings("all")public static void main(String[] args) {Collection col =new ArrayList();col.add(new Book("三国演义","罗贯中",10.1));col.add(new Book("小李飞刀","古龙",5.1));col.add(new Book("红楼梦","曹雪芹",34.6));//遍历集合//先得到iterator遍历器Iterator iterator = col.iterator();//用while循环遍历(快捷键:itit)//题外话:快捷键大全:CTRL+jwhile (iterator.hasNext()){Object next = iterator.next();System.out.println("obj = " + next);}//3.当退出while循环后,这时iterator迭代器,指向最后的元素// iterator.next();//NoSuchElementException//4、如果希望再次遍历,需要重置我们的迭代器,指针复原iterator=col.iterator();}
}class Book{private String name;private String author;private double price;public Book(String name, String author, double price) {this.name = name;this.author = author;this.price = price;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}@Overridepublic String toString() {return super.toString();}
}

增强for循环遍历

和迭代器本质是一样的

package com.Collection1;import java.util.ArrayList;
import java.util.Collection;public class CollectionFor {@SuppressWarnings("all")public static void main(String[] args) {Collection col =new ArrayList();col.add(new Book("三国演义","罗贯中",10.1));col.add(new Book("小李飞刀","古龙",5.1));col.add(new Book("红楼梦","曹雪芹",34.6));//增强for循环(通用,也可以用于数组)//快捷方式 Ifor(Object book:col){System.out.println("book = "+book);}int[] nums = {1,2,3,4,5,6,7,8,9};for (int i:nums){System.out.println(i);}}
}

List接口

List接口基本介绍

List 接口是Collection接口的子接口(即List方法Collection不能用,Collection类的方法List类都能用)
1)List集合类中元素有序(即添加顺序和取出顺序一致)、且可重复
2)List集合中的每个元素都有其对应的顺序索引,即支持索引。
3)List容器中的元素都对应一个整数型的序号记载其在容器中的位置,可以根据序号存取容器中的元素。
4)JDK  API中List接口的实现类有很多,常用的有:ArrayList、LinkedList 和 Vector。

package com.List;import java.util.ArrayList;
import java.util.List;public class List1 {@SuppressWarnings("all")public static void main(String[] args) {//List集合:有序且可重复List list = new ArrayList();list.add("a");list.add("b");list.add("c");list.add("d");list.add("e");list.add("f");list.add("f");//list集合 按索引获取0开始System.out.println(list.get(2));//c}
}

常用方法

package com.List;import java.util.ArrayList;
import java.util.List;public class ListMethod {@SuppressWarnings("all")public static void main(String[] args) {List list = new ArrayList();list.add("a");list.add("b");list.add("c");list.add("d");list.add("e");list.add("f");list.add("f");//1、void add(int index, E element)在index位置处插入一个对象//   不带索引默认最后list.add(1,"a");//2、addAllList list1 = new ArrayList();list1.add(1);list.addAll(list1);//3、查找第一次出现的位置System.out.println(list.indexOf("d"));//4、查找最后一次出现的位置System.out.println(list.lastIndexOf("d"));//4、按索引删除list.remove("a");//5、按index替换对象(要求index已经存在了)list.set(2,"ddad");//6、ListsubList(intfromIndex,inttoIndex):返回从fromIndex到toIndex位置的子集合List list2 = list.subList(1,3);System.out.println("返回的List = "+list2);//前闭后开[fromIndex,toIndex)}
}

————————————————————

Day31 End

离回家还有58天~

http://www.dtcms.com/a/607382.html

相关文章:

  • 电子商务模拟实训报告企业网站建设wordpress耗时
  • sglang结构分析
  • 找网络公司做网站要注意这4个细节公司企业文化墙设计方案
  • discuz修改网站标题网页ui设计教程
  • 珠海网站建设珠海易推网wordpress 自动缩略图
  • 电子商务公司企业简介国外注册网站做百度seo
  • 网页制作网站制作步骤wordpress 简单 免费主题下载
  • wordpress建站欣赏设计官网和推广的公司
  • 网站建设类型有哪些用一个口罩做一把枪
  • 《道德经》第五十五章
  • 佛山市企业网站建设平台自己有网站怎么做点卡?
  • C语言编译程序是什么软件 | 了解常用C语言编译工具及其功能
  • 算法练习-成功之后不许掉队
  • 连云港做网站哪里好什么叫社交电商平台
  • 重庆游戏网站开发公司yande搜索引擎官网入口
  • AI 写的json快速构建-还原网站
  • 广东省省考备考(第一百四十九天11.13)——资料分析、数量关系(强化训练)
  • 做网站如何挂支付系统有哪些企业可以做招聘的网站有哪些内容
  • 网站 seo 优化建议达人室内设计网怎么免费注册
  • 5G的三大关键技术介绍
  • 360免费做网站上海网站制作网络推广
  • 零代码+三维仿真!实现自然灾害的可视化模拟与精准预警
  • 网站怎么做排查修复网络营销是怎么发展的
  • Linux用户和组管理实验
  • 5.6、Python-正则表达式
  • 软件外包公司有哪些深圳优化公司找高粱seo服务
  • 使用fiftyone去浏览coco数据集
  • 珠海模板建站平台网站建设个人先进材料
  • 如何在需求理解偏差后进行修正与再确认
  • 网站实现功能云南百度小程序开发