一、类和接口总览
Java 集合框架的优点及作用
- 使用成熟的集合框架,有助于我们便捷、快速的写出高效、稳定的代码
- 学习背后的数据结构知识,有助于我们理解各个集合的优缺点及使用场景
二、Collection 接口
1、Collection 常用方法
import java.util.ArrayList;
import java.util.Collection;
public class TestDemo {
public static void main(String[] args) {
Collection<String> collection = new ArrayList<>();
collection.add("hello");
collection.add("world");
System.out.println(collection.size());
Collection<Integer> collection1 = new ArrayList<>();
collection1.add(10);
}
}
import java.util.ArrayList;
import java.util.Collection;
public class TestDemo {
public static void main(String[] args) {
Collection<String> collection = new ArrayList<>();
collection.add("hello");
collection.add("world");
System.out.println(collection);
collection.clear();
System.out.println(collection);
System.out.println(collection.isEmpty());
}
}
不建议进行整体的强制类型转换
import java.util.ArrayList;
import java.util.Collection;
public class TestDemo {
public static void main(String[] args) {
Collection<String> collection = new ArrayList<>();
collection.add("hello");
collection.add("world");
Object[] objects = collection.toArray();
System.out.println(Arrays.toString(objects));
}
}
import java.util.ArrayList;
import java.util.Collection;
public class TestDemo {
public static void main(String[] args) {
Collection<String> collection = new ArrayList<>();
collection.add("hello");
collection.add("world");
collection.remove("world");
System.out.println(collection);
}
}
2、Map 接口
方法签名 | 说明 |
---|
V get(Object k) | 根据指定的 k 查找对应的 v | V getOrDefault(Object k, V defaultValue) | 根据指定的 k 查找对应的 v,没有找到用默认值代替 | V put(K key, V value) | 将指定的 k-v 放入 Map | boolean containsKey(Object key) | 判断是否包含 key | boolean containsValue(Object value) | 判断是否包含 value | Set<Map.Entry<K, V>> entrySet() | 将所有键值对返回 | boolean isEmpty() | 判断是否为空 | int size() | 返回键值对的数量 |
import java.util.ArrayList;
import java.util.Collection;
public class TestDemo {
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("及时雨", "宋江");
map.put("豹子头", "林冲");
System.out.println(map.isEmpty());
System.out.println(map.size());
String ret = map.get("及时雨");
System.out.println(ret);
System.out.println(map.getOrDefault("花和尚", "鲁智深"));
System.out.println(map.containsKey("豹子头"));
System.out.println(map.containsValue("林冲"));
System.out.println(map);
Set<Map.Entry<String, String>> entrySet = map.entrySet();
for (Map.Entry<String, String> entry : entrySet) {
System.out.println("key: "+entry.getKey()+" value:"+entry.getValue());
}
}
}
三、预备知识-泛型(Generic)
class MyArrayList<E> 代表这个类是一个泛型类,此时的这个E,就是一个占位符而已
public class Test {
public static void main(String[] args) {
MyArrayList<String> myArrayList1 = new MyArrayList<>();
MyArrayList<Integer> myArrayList2 = new MyArrayList<>();
MyArrayList<Boolean> myArrayList3 = new MyArrayList<>();
}
泛型的意义: 1. 自动对类型进行检查 2. 自动对类型进行了强制类性转换
class MyArrayList<E> {
private E[] elem;
private int usedSize;
public MyArrayList() {
this.elem = (E[])new Object[10];
}
public void add(E val) {
this.elem[usedSize] = val;
usedSize++;
}
public E get(int pos) {
return this.elem[pos];
}
}
public class Test {
public static void main(String[] args) {
MyArrayList<String> myArrayList = new MyArrayList<>();
myArrayList.add("ABC");
myArrayList.add("bit");
String ret = myArrayList.get(1);
System.out.println(ret);
MyArrayList<Integer> myArrayList1 = new MyArrayList<>();
myArrayList1.add(11);
myArrayList1.add(22);
int ret2 = myArrayList1.get(1);
System.out.println(ret2);
}
}
关于Object[] 强制类型转换的思考
String[] strings = new String[10];
Object o1 = new String[10];
Object[] o2 = new String[10];
JAVA提高六:泛型
泛型是怎么编译的?
泛型是编译时期的一种机制,擦除机制(-> Object)
Creating a Generic Array in Java 使用泛型数组时的注意事项: 数组和泛型之间的一个重要区别是它们如何强制执行类型检查。 具体来说,数组在运行时存储和检查类型信息。 然而,泛型在编译时检查类型错误,并且在运行时没有类型信息。
四、预备知识-包装类(Wrapper Class)
基本数据类型 | 包装类 |
---|
byte | Byte | short | Short | int | Integer | long | Long | float | Float | double | Double | char | Character | boolean | Boolean |
public class TestDemo {
public static void main(String[] args) {
String str = "123";
int ret = Integer.valueOf(str);
System.out.println(ret+1);
}
}
装箱(boxing)和拆箱(unboxing)
public class TestDemo {
public static void main(String[] args) {
Integer a = 123;
int b = a;
System.out.println(a+" " + b);
System.out.println("=============");
Integer a2 = Integer.valueOf(123);
Integer a3 = new Integer(123);
int b2 = a2.intValue();
double d = a2.doubleValue();
int i = 10;
}
}
一道面试题:
public class TestDemo {
public static void main(String[] args) {
Integer a = 128;
Integer b = 128;
System.out.println(a == b);
}
}
五、List
public class Test {
public static void main(String[] args) {
List<String> list = new ArrayList<>(20);
ArrayList<String> list2 = new ArrayList<>();
}
}
- ArrayList实现了RandomAccess接口,表明ArrayList支持随机访问
- ArrayList实现了Cloneable接口,表明ArrayList是可以clone的
- ArrayList实现了Serializable接口,表明ArrayList是支持序列化的
- 和Vector不同,ArrayList不是线程安全的,在单线程下可以使用,在多线程中可以选择Vector或者CopyOnWriteArrayList
- ArrayList底层是一段连续的空间,并且可以动态扩容,是一个动态类型的顺序表
六、ArrayList使用
1、ArrayList的构造
public class Test {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("hello");
list.add("bit");
list.add("haha");
System.out.println(list);
ArrayList<String> list3 = new ArrayList<>(list);
}
}
2、ArrayList的遍历
public class Test {
public static void main(String[] args) {
ArrayList<String> list2 = new ArrayList<>();
list2.add("hello");
list2.add("bit");
list2.add("haha");
System.out.println(list2);
System.out.println("================");
for(int i = 0; i < list2.size(); i++) {
System.out.print(list2.get(i)+" ");
}
System.out.println();
System.out.println("==================");
for (String s : list2) {
System.out.print(s+" ");
}
System.out.println();
System.out.println("========迭代器打印==========");
Iterator<String> it = list2.iterator();
while (it.hasNext()) {
System.out.print(it.next()+" ");
}
System.out.println();
System.out.println("========迭代器List相关打印==========");
ListIterator<String> it2 = list2.listIterator();
while (it2.hasNext()) {
System.out.print(it2.next()+" ");
}
}
}
Iterator 和 ListIterator 的区别
public class Test {
public static void main(String[] args) {
ArrayList<String> list2 = new ArrayList<>();
list2.add("hello");
list2.add("bit");
list2.add("haha");
Iterator<String> it = list2.iterator();
while (it.hasNext()) {
String ret = it.next();
if(ret.equals("hello")) {
it.remove();
}else {
System.out.print(ret + " ");
}
}
ListIterator<String> it2 = list2.listIterator();
while (it2.hasNext()) {
String ret = it2.next();
if(ret.equals("hello")) {
it2.remove();
}else {
System.out.print(ret + " ");
}
}
}
}
public class Test {
public static void main(String[] args) {
ArrayList<String> list2 = new ArrayList<>();
list2.add("hello");
list2.add("bit");
list2.add("haha");
ListIterator<String> it2 = list2.listIterator();
while (it2.hasNext()) {
String ret = it2.next();
if(ret.equals("bit")) {
it2.add("gaobo");
}else {
System.out.print(ret + " ");
}
}
System.out.println("=================");
System.out.println(list2);
}
}
3、ArrayList常见操作
public class Test {
public static void main(String[] args) {
ArrayList<String> list2 = new ArrayList<>();
list2.add("a");
list2.add("b");
list2.add("c");
System.out.println(list2);
list2.add(0, "hello");
System.out.println(list2);
ArrayList<String> list3 = new ArrayList<>();
list3.add("我是测试List1");
list3.add("我是测试List2");
list3.add("我是测试List3");
list2.addAll(list3);
System.out.println(list2);
}
}
public class Test {
public static void main(String[] args) {
ArrayList<String> list2 = new ArrayList<>();
list2.add("a");
list2.add("b");
list2.add("c");
String ret = list2.remove(0);
System.out.println(ret);
System.out.println(list2);
boolean flag = list2.remove("c");
System.out.println(flag);
System.out.println(list2);
}
}
public class TestDemo {
public static void main(String[] args) {
ArrayList<String> list2 = new ArrayList<>();
list2.add("a");
list2.add("b");
list2.add("c");
String ret = list2.get(0);
System.out.println(ret);
System.out.println(list2);
String ret2 = list2.set(0, "p");
System.out.println("原来的字符串是:"+ret2);
System.out.println(list2);
System.out.println(list2.contains("p"));
System.out.println(list2.indexOf("c"));
System.out.println(list2.lastIndexOf("c"));
list2.clear();
System.out.println(list2);
}
}
public class Test {
public static void main(String[] args) {
ArrayList<String> list2 = new ArrayList<>();
list2.add("a");
list2.add("b");
list2.add("c");
list2.add("f");
list2.add("g");
List<String> sub = list2.subList(1, 3);
System.out.println(sub);
sub.set(0, "bit");
System.out.println(sub);
System.out.println(list2);
}
}
4、ArrayList的扩容机制
public class Test {
public static void main(String[] args) {
ArrayList<String> list1 = new ArrayList<>();
list1.add("bit");
System.out.println(list1);
ArrayList<String> list2 = new ArrayList<>(13);
}
结论:
- 如果ArrayList调用,不带参数的构造方法,那顺序表的大小0,第一次add时,整个顺序表才变为了10
当这10个放满了,开始扩容,以1.5倍的方式扩容 - 如果调用的是给定容量的构造方法,顺序表的大小就是你给定的容量,放满了还是以1,5倍进行扩容
5、模拟实现ArrayList
import java.util.Arrays;
class MyArrayList<E> {
private Object[] elementData;
private int usedSize;
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
public MyArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
public MyArrayList(int capacity) {
if(capacity > 0) {
this.elementData = new Object[capacity];
}else if(capacity == 0) {
this.elementData = new Object[0];
}else {
throw new IllegalArgumentException("初始化的容量不能为负数");
}
}
public boolean add(E e) {
ensureCapacityInternal(usedSize+1);
elementData[usedSize] = e;
usedSize++;
return true;
}
private void ensureCapacityInternal(int minCapacity) {
int capacity = calculateCapacity(elementData,minCapacity);
ensureExplicitCapacity(capacity);
}
private void ensureExplicitCapacity(int minCapacity) {
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE-8;
private void grow(int minCapacity) {
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE> 0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity);
}
private static int hugeCapacity(int minCapacity) {
if (minCapacity < 0)
throw new OutOfMemoryError();
return (minCapacity > MAX_ARRAY_SIZE) ?
Integer.MAX_VALUE :
MAX_ARRAY_SIZE;
}
private static int calculateCapacity(Object[] elementData, int minCapacity) {
if(elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(10,minCapacity);
}
return minCapacity;
}
public void add(int index,E e) {
rangeCheckForAdd(index);
ensureCapacityInternal(usedSize+1);
copy(index,e);
usedSize++;
}
private void copy(int index,E e) {
for (int i = usedSize-1; i >= index ; i--) {
elementData[i+1] = elementData[i];
}
elementData[index] = e;
}
private void rangeCheckForAdd(int index) {
if(index < 0 || index > size()) {
throw new IndexOutOfBoundsException("index位置不合法,不能插入!");
}
}
public int size() {
return this.usedSize;
}
}
public class TestDemo {
public static void main(String[] args) {
}
}
七、练习
1、自定义数据类型
比特科技有若干学生(学生对象放在一个List中),每个学生有一个姓名(String)、班级(Strin和考试成绒属性(double) 某次考试结束后,每个学生都获得了一个考试成绩 遍历list集合,并把学生对象的属性打印出来
class Student {
private String name;
private String classes;
private double score;
public Student(String name, String classes, double score) {
this.name = name;
this.classes = classes;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getClasses() {
return classes;
}
public void setClasses(String classes) {
this.classes = classes;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", classes='" + classes + '\'' +
", score=" + score +
'}';
}
}
public class Test {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("bit", "102-1", 10.9));
students.add(new Student("zhangsan", "102-2", 70.9));
students.add(new Student("lisi", "102-1", 50.9));
System.out.println(students);
}
}
2、使用Collections排序
public static void main(String[] args) {
ArrayList<Integer> integers = new ArrayList<>();
integers.add(12);
integers.add(53);
integers.add(37);
Collections.sort(integers);
System.out.println(integers);
Collections.reverse(integers);
System.out.println(integers);
}
3、Welcome to CVTE
删除第一个字符串中出现的第二个字符串中的字符 例: String str1 = “welcome to CVTE”; String str2 = “come”; 输出:wl t CVTE
public class Test {
public static void main2(String[] args) {
String str1 = "welcome to CVTE";
String str2 = "come";
ArrayList<Character> list = new ArrayList<>();
for (int i = 0; i < str1.length(); i++) {
char ch = str1.charAt(i);
if(!str2.contains(ch+"")) {
list.add(ch);
}
}
for (char ch : list) {
System.out.print(ch);
}
}
public static void main(String[] args) {
String str1 = "welcome to CVTE";
String str2 = "come";
StringBuffer sb = new StringBuffer();
for(int i = 0; i < str1.length(); i++) {
char ch = str1.charAt(i);
if(!str2.contains(ch+"")) {
sb.append(ch);
}
}
System.out.println(sb);
}
}
5、扑克牌
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class Card {
private int rank;
private String suit;
public Card(int rank, String suit) {
this.rank = rank;
this.suit = suit;
}
@Override
public String toString() {
return "[ "+this.suit+":"+this.rank+" ]";
}
}
public class TestDemo {
public static final String[] suits = {"?","?","?","?"};
public static List<Card> budCard() {
ArrayList<Card> cards = new ArrayList<>();
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= 13; j++) {
cards.add(new Card(j, suits[i]));
}
}
return cards;
}
public static void swap(List<Card> cards, int i, int j) {
Card tmp = cards.get(i);
cards.set(i, cards.get(j));
cards.set(j, tmp);
}
public static void shuffle(List<Card> cards) {
int size = cards.size();
for (int i = size - 1; i > 0; i--) {
Random random = new Random();
int rand = random.nextInt(i);
swap(cards, i, rand);
}
}
public static void main(String[] args) {
List<Card> cards = budCard();
System.out.println("买牌"+cards);
shuffle(cards);
System.out.println("洗牌"+cards);
System.out.println("揭牌:3个人每人轮流揭5张牌");
ArrayList<List<Card>> hand = new ArrayList<>();
List<Card> hand1 = new ArrayList<>();
List<Card> hand2 = new ArrayList<>();
List<Card> hand3 = new ArrayList<>();
hand.add(hand1);
hand.add(hand2);
hand.add(hand3);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
Card card = cards.remove(0);
hand.get(j).add(card);
}
}
System.out.println("第1个人的牌:"+hand1);
System.out.println("第2个人的牌:"+hand2);
System.out.println("第3个人的牌:"+hand3);
System.out.println("剩下的牌:"+cards);
}
}
6、杨辉三角
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> ret = new ArrayList<>();
List<Integer> list1 = new ArrayList<>();
list1.add(1);
ret.add(list1);
for (int i = 1; i < numRows; i++) {
List<Integer> list = new ArrayList<>();
list.add(1);
List<Integer> preRow = ret.get(i-1);
for (int j = 1; j < i; j++) {
int num = preRow.get(j) + preRow.get(j-1);
list.add(num);
}
list.add(1);
ret.add(list);
}
return ret;
}
}
|