当前位置:网站首页>[Notes] Collection Framework System Collection
[Notes] Collection Framework System Collection
2022-08-10 05:51:00 【hagong9】
目录
集合的理解和好处
Previously to hold multiple data we used arrays,但是数组不够灵活,
Collections have a set of methods,需要使用的时候调用即可.
集合框架体系图
Collocation基础方法
package com.zhang.collection_;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@SuppressWarnings({"all"})
public class collectionDemo01 {
public static void main(String[] args) {
List list = new ArrayList();
//添加单个元素
System.out.println("=======================添加单个元素=================================");
list.add("jack");
list.add(100);
list.add(true);
list.add(18.9);
list.add("集合");
System.out.println("list="+list);
//删除元素 两种方法
System.out.println("=======================删除元素=================================");
list.remove("jack");//删除指定元素
System.out.println("list="+list);
list.remove(0);//删除第一个元素
System.out.println("list="+list);
System.out.println("========================查找元素是否存在================================");
//查找元素是否存在
System.out.println(list.contains(18.9));//存在输出true
//获取元素个数 size
System.out.println("========================获取元素个数 size================================");
System.out.println(list.size());
//判断是否是空 isEmpty
System.out.println("========================判断是否是空 isEmpty================================");
System.out.println(list.isEmpty());
//清空 clear
System.out.println("========================清空 clear================================");
list.clear();
System.out.println("list="+list);
//添加多个元素 addAll
System.out.println("========================添加多个元素 addAll================================");
ArrayList list2 = new ArrayList();
list2.add("红楼梦");
list2.add("三国演义");
list.addAll(list2);
System.out.println("list="+list);
//查找多个元素是否都存在 containsAll
System.out.println("========================查找多个元素是否都存在 containsAll================================");
System.out.println(list.containsAll(list2));
//删除多个元素 removeall()
System.out.println("========================查找多个元素是否都存在 containsAll==========================");
list.add("西游记");
list.removeAll(list2);
System.out.println("list="+list);
}
}
Collection遍历方式 iteration
Iterator usage example
package com.zhang.collection_.iteration_;
import java.awt.print.Book;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class CollectionIteration {
public static void main(String[] args) {
Collection col = new ArrayList();
col.add(new Books("三国演义", "罗贯中", 160));
col.add(new Books("红楼梦", "曹雪芹", 100));
col.add(new Books("西游记", "吴承恩", 81));
//System.out.println(col);
System.out.println(col);
//想要遍历col集合
//1.先获得col 的迭代器
Iterator iterator = col.iterator();
//2.使用while循环遍历
while (iterator.hasNext()) {
//返回下一个元素 类型是obj
Object obj = iterator.next();
System.out.println(obj);
}
//3.当退出while循环后,这时iteration迭代器指向最后的元素
//Traverse again if needed,需要重置迭代器.
iterator = col.iterator();
System.out.println("==========第二次遍历==============");
while (iterator.hasNext()) {
Object obj = iterator.next();
System.out.println(obj);
}
}
}
class Books {
private String name;
private String author;
private double price;
@Override
public String toString() {
return "Books{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
public Books(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;
}
}
集合增强for
package com.zhang.collection_;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
public class iterationFor {
public static void main(String[] args) {
Collection col = new ArrayList();
col.add(new Book("三国演义","罗贯中",198));
col.add(new Book("红楼梦","曹雪芹",98));
//使用增强for循环
for (Object book : col) {
System.out.println(book);
}
//增强forLoops can also be used with arrays
int[] nums = {1,2,3,4};
for (int i : nums) {
System.out.println("i="+i);
}
}
}
class Book{
private String name;
private String author;
private double price;
@Override
public String toString() {
return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + 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;
}
}
List接口方法
package com.zhang.list_;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Demo01 {
public static void main(String[] args) {
List list = new ArrayList();
list.add("三国演义");
list.add("水浒传");
list.add("西游记");
//add (int index,object ele)在index位置插入ele元素
list.add(1,"红楼梦");
System.out.println(list);
//addAll(int index,Collection eles) 从index位置开始把elesAll data is inserted
List list2 = new ArrayList();
list2.add("哈利波特");
list2.add("古剑奇谭");
list.addAll(2,list2);
System.out.println(list);
//get(int index)获取index位置元素
System.out.println(list.get(2));
//int indexOf(object obj)返回obj在集合中首次出现的位置
System.out.println(list.indexOf("哈利波特"));
//int lastIndexOf(object obj)返回obj在集合中末次出现的位置
list.add(6,"哈利波特");
System.out.println(list.lastIndexOf("哈利波特"));
//remove(int index)移除index位置的元素
list.remove(6);
System.out.println(list);
//set(int index,object obj)设置指定index位置的元素为ele,相当于替换
list.set(2,"哈利波特与魔法石");
System.out.println(list);
//三种遍历方法
Iterator iterator = list.iterator();
while (iterator.hasNext()) {
Object next = iterator.next();
System.out.println(next);
}
System.out.println("===================");
for (Object o :list) {
System.out.println(o);
}
System.out.println("===========================");
for (int i =0;i<list.size();i++){
Object obj = list.get(i);
System.out.println(obj);
}
}
}
ArrayList Vector LinkList
ArrayList
ArrayList扩容机制
Vector
Vector扩容
LinkList
LinkListCreate an example of a doubly linked list
package com.zhang.list_;
import org.w3c.dom.Node;
public class LinkList_ {
public static void main(String[] args) {
Node node1 = new Node("小明");
Node node2 = new Node("小红");
Node node3 = new Node("小张");
//双向链表连接node1 node2 node3
node1.next = node2;
node2.next = node3;
node3.pre = node2;
node2.pre = node1;
Node frist = node1;//设置头结点
Node last = node3;//设置尾结点
//遍历
while(true){
if (frist == null){
break;
}else{
System.out.println(frist);
frist = frist.next;
}
}
System.out.println("========================");
//Add a small black between Xiaoming and Xiaohong
Node node4 = new Node("小黑");
node1.next = node4;
node4.next = node2;
node2.pre = node4;
node4.pre = node1;
Node frist1 = node1;//设置头结点
Node last1 = node3;//设置尾结点
while(true){
if (frist1 == null){
break;
}else{
System.out.println(frist1);
frist1 = frist1.next;
}
}
}
//定义一个node类,node 对象,Represents a node in a doubly linked list.
static class Node{
public Object item;//存放数据
public Node next;//指向后一个结点
public Node pre;//指向前一个结点
public Node(Object name){
this.item=name;
}
@Override
public String toString() {
return "Node name="+ item;
}
}
}
边栏推荐
猜你喜欢
The complex "metaverse" will be interpreted for you, and the Link Reading APP will be launched soon!
栈和队列
[List Exercise] Traverse the collection and sort by price from low to high,
Chained Picks: Starbucks looks at digital collectibles and better engages customers
Collection tool class
.las转.txt 再转.pcd,编译运行中出现的错误
IO流【】【】【】
【List练习】遍历集合并且按照价格从低到高排序,
21天挑战杯MySQL-Day05
十年磨一剑!数字藏品行情软件,链读APP正式开放内测!
随机推荐
idm下载器如何使用 idm下载器使用技巧
力扣——统计只差一个字符的子串数目
I use this recruit let the team to improve the development efficiency of 100%!
栈和队列
2021-06-22
el-dropdown下拉菜单样式修改,去掉小三角
视图【】【】【】【】
Consensus calculation and incentive mechanism
win12 修改dns脚本
IO流【】【】【】
Chain Reading Recommendation: From Tiles to Generative NFTs
Privatisation build personal network backup NextCloud
cesium 添加点,移动点
Small program wx.request simple Promise package
Canal reports Could not find first log file name in binary log index file
图片批量添加水印批量加背景缩放批量合并工具picUnionV4.0
复杂的“元宇宙”,为您解读,链读APP即将上线!
笔记1
Batch add watermark to pictures batch scale pictures to specified size
链读推荐:从瓷砖到生成式 NFT