集合框架与泛型笔记
装箱与拆箱
装箱是把基本数据类型转换成包装类型
拆箱是把包装类转换成基本数据类型
集合框架与泛型
为什么要使用集合框架
数组在创建的时候就定了长度了,不可变
集合创建时为可长度的
List Set Map的区别:
实线是继承、虚线是实现
List Set实现了Collection接口的集合
Map为独立出去的一个接口
Collections为java.util包中的一个工具类,可以对集合中的数据进行修改
Collection接口储存这一组不唯一的,无序的对象
List实现Collection接口 储存一组有序的,不唯一的对象
Set实现Collection接口 储存一组无序的,不唯一的对象
Map接口储存一组键值对,提供key到value的映射
集合中只能储存引用数据类型 不能存储基本数据类型 可以用包装类
ArrayList
ArrayList实现了长度可变的 数组 在内存中分配一个 连续的空间 遍历数组和随机访问的效率高,有下标
ArrayList<泛型 要存入的数据类型> 变量名 =
new ArrayList<泛型(可以不写)默认和前面一样>();
LinkedList
LinkedList采用链表储存方式,在内存中地址不连续,采用指向方式访问链接,在 插入,删除元素时效率高,没下标
LinkedList<泛型 要存入的数据类型> 变量名 =
new LinkedList<泛型(可以不写)默认和前面一样>();
Set
Set接口储存一组唯一,无序的对象
HashSet是Set接口常用的实现类
Set采用equals()方法比较两个方法是否相等
Set**不存在get()**方法
遍历采用迭代器或增强for循环
Set<泛型 集合储存的类型> 变量名 = new HashSet<泛型(可以不写默认为前面的)>();
ArrayList测试
public class News {
private Integer id;
private String title;
private String author;
public News(Integer id, String title, String author) {
this.id = id;
this.title = title;
this.author = author;
}
@Override
public String toString() {
return "News{" +
"id=" + id +
", title='" + title + '\'' +
", author='" + author + '\'' +
'}';
}
}
import java.util.ArrayList;
/**
* @author JS
*/
public class Test {
public static void main(String[] args) {
/*创建新闻对象准备存入集合中*/
News news1 = new News(1,"今天天气晴","admin");
News news2 = new News(2,"空气质量好","admin");
News news3 = new News(3,"微风2级","admin");
News news4 = new News(1,"今天愚人节","test");
News news5 = new News(2,"阿巴阿巴","test");
/*TODO 创建ArrayList集合储存新闻对象
* ArrayList<泛型 要存入的数据类型> 变量名 = new ArrayList<泛型(可以不写)默认和前面一样>();*/
ArrayList<News> news = new ArrayList<News>();
/*向集合中添加数据*/
news.add(news1);
news.add(news2);
news.add(news3);
/*向指定位置添加数据*/
news.add(0,news4);
news.add(1,news5);
/*输出所有数据,需要对象类重写toString否则输出地址*/
System.out.println(news);
/*输出指定下标的数据*/
System.out.println(news.get(0));
/*通过for each遍历整个数组*/
for (News n : news) {
System.out.println(n);
}
/*获取集合长度(从1开始)*/
System.out.println("当前新闻个数为:"+news.size());
/*判断集合中是否存在指定元素*/
System.out.println("当前集合中是否存在新闻3?"+news.contains(news3));
/*删除元素*/
System.out.println("删除愚人节新闻=>"+news.remove(news1));
System.out.println("当前集合长度:"+news.size());
}
}
LinkedList测试
public class News {
private Integer id;
private String title;
private String author;
public News(Integer id, String title, String author) {
this.id = id;
this.title = title;
this.author = author;
}
@Override
public String toString() {
return "News{" +
"id=" + id +
", title='" + title + '\'' +
", author='" + author + '\'' +
'}';
}
}
import java.util.LinkedList;
/**
* @author JS
* LinkedList
*/
public class Test {
public static void main(String[] args) {
/*创建新闻对象准备存入集合中*/
News news1 = new News(1,"今天天气晴","admin");
News news2 = new News(2,"空气质量好","admin");
News news3 = new News(3,"微风2级","admin");
News news4 = new News(1,"今天愚人节","test");
News news5 = new News(2,"阿巴阿巴","test");
/*创建LinkedList集合储存新闻对象*/
/*LinkedList<泛型 要存入的数据类型> 变量名 = new LinkedList<泛型(可以不写)默认和前面一样>();*/
LinkedList<News> news = new LinkedList<News>();
/*想集合中添加数据*/
news.add(news1);
news.add(news2);
news.add(news3);
/*向集合最前面添加数据*/
news.addFirst(news4);
/*向集合尾部添加数据*/
news.addLast(news5);
/*返回指定下标的数据*/
System.out.println(news.get(2));
/*输出第一位数据*/
System.out.println(news.getFirst());
/*输出最后一位数据*/
System.out.println(news.getLast());
/*输出所有数据,需要重写toString否则输出地址*/
System.out.println(news);
/*输出集合长度*/
System.out.println(news.size());
/*删除指定下标的数据*/
news.remove(3);
/*删除第一个数据*/
news.removeFirst();
/*删除最后一个数据*/
news.removeLast();
}
}
Set测试
import java.util.HashSet;
import java.util.Set;
/**
* @author JS
* Set
*/
public class Test {
public static void main(String[] args) {
/*创建Set集合*/
/*Set<泛型 集合储存的类型> 变量名 = new HashSet<泛型(可以不写默认为前面的)>();*/
Set<String> set = new HashSet<String>();
/*创建三个对象*/
String s1 = "java";
String s2 = s1;
String s3 = "JAVA";
/*向Set集合添加数据*/
set.add(s1);
/*s2为重复数据 Set将忽略不插入*/
set.add(s2);
set.add(s3);
/*输出Set集合的数据*/
System.out.println(set);
/*输出Set集合长度*/
System.out.println(set.size());
/*for循环遍历集合*/
for (String s : set) {
System.out.println(s);
}
/*删除指定元素*/
set.remove(s1);
/*删除所有元素*/
set.clear();
}
}
字符串查找替换
/*字符串替换*/
str = str.replace("JS","[JS]");
仅为本人学习时的笔记,仅供参考