当前位置:网站首页>[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;
}
}
}
边栏推荐
猜你喜欢
随机推荐
error in ./node_modules/cesium/Source/ThirdParty/zip.js
第五次实验
2021-07-09
OSPF实验
集合 set接口
Analysis of the investment value of domestic digital collections
行盒子的盒模型
毫米波雷达基础概念学习
连接 Nacos 报超时错误
R简单统计计算--笔记
Canal 报错 Could not find first log file name in binary log index file
impdp 导入数据
Using sqlplus to operate database in shell script
Collection Map
栈和队列
redis集群模式
The submenu of the el-cascader cascade selector is double-clicked to display the selected content
Index Notes【】【】
操作表 函数的使用
tinymce rich text editor