文章目录
java集合类一般都在java .util包底下
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ThkQGj5M-1663506389475)(D:/%E4%BD%A0%E5%A5%BDJava/305.png)]
注意一:ArrayList 的构造方法
ArrayList的底层还是一个数组(泛型数组);
public static void main(String[] args) {
ArrayList<Integer> arrayList0 = new ArrayList<>(12);
arrayList0.add(1);
arrayList0.add(2);
arrayList0.add(3);
System.out.println(arrayList0); //里面已经重写了toString()方法
ArrayList<String> arrayList1 = new ArrayList<>();
arrayList1.add("hello");
arrayList1.add("xiaobai");
System.out.println(arrayList1);
ArrayList<Integer> arrayList2 = new ArrayList<>(arrayList0);
//ArrayList<Integer> arrayList3 = new ArrayList<>(arrayList1);
System.out.println(arrayList2);
//System.out.println(arrayList3);
}
/*
[1, 2, 3]
[hello, xiaobai]
[1, 2, 3]
*/
注意:
ArrayList<Integer> arrayList2 = new ArrayList<>(arrayList0);
//ArrayList<Integer> arrayList3 = new ArrayList<>(arrayList1);
System.out.println(arrayList2);
//System.out.println(arrayList3);
这是第二种构造方法,意思是把 arrayList0 里面的数据传递给 arrayList2;
ArrayList arrayList2 ---- > arrayList2的类型是 ArrayList 里面的元素类型是 Integer ;
arrayList0 满足: Collection<? extends E> c 注意类型的匹配,这里的E是 arrayList2 中的 Integer ,所以 arrayList0 内类型的上界应该
是Integer 。
private static final int DEFAULT_CAPACITY = 10;
private static final Object[] EMPTY_ELEMENTDATA = {};
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
transient Object[] elementData;
private int size;
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) != 0) {
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}
注意二:ArrayList的常用方法
boolean add(E e) 尾插 e
void add(int index, E element) 将 e 插入到 index 位置
boolean addAll(Collection<? extends E> c) 尾插 c 中的元素
E remove(int index) 删除 index 位置元素
boolean remove(Object o) 删除遇到的第一个 o
E get(int index) 获取下标 index 位置元素
E set(int index, E element) 将下标 index 位置元素设置为 element
void clear() 清空
boolean contains(Object o) 判断 o 是否在线性表中
int indexOf(Object o) 返回第一个 o 所在下标
int lastIndexOf(Object o) 返回最后一个 o 的下标
List<E> subList(int fromIndex, int toIndex) 截取部分 list
注意: remove( ) 方法
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<>(5);
arrayList.add(1);
arrayList.add(2);
arrayList.add(3);
arrayList.add(4);
System.out.println(arrayList);
System.out.println("----------------");
//remove(int index); 删除下标为index的元素
arrayList.remove(0);
System.out.println(arrayList);
System.out.println("----------------");
//remove(object index); 删除index对象 里面要是对象/对象的引用
arrayList.remove(new Integer(4));
System.out.println(arrayList);
}
/*
[1, 2, 3, 4]
----------------
[2, 3, 4]
----------------
[2, 3]
/*
注意:subList(index1 , index2)的部分截取功能
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<>(5);
arrayList.add(1);
arrayList.add(2);
arrayList.add(3);
arrayList.add(4);
System.out.println(arrayList);
System.out.println("----------------");
//注意:subList(index1,index2) 的部分截取功能,左闭右开。返回值用lIst接收
List<Integer> list = arrayList.subList(1,3);
System.out.println(list);
}
注意:ArrayList内存是如何置空的
注意三: 打印ArrayList中的每一个元素
//打印arrayList当中的元素
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(1);
arrayList.add(2);
arrayList.add(3);
arrayList.add(4);
arrayList.add(5);
arrayList.add(6);
arrayList.add(7);
arrayList.add(8);
arrayList.add(9);
arrayList.add(10);
//方法一:
for (int i = 0; i < arrayList.size(); i++) {
System.out.print(arrayList.get(i)+" ");
}
//方法二:
for (Integer x: arrayList) {
System.out.print(x+" ");
}
//方法三
Iterator<Integer> it = arrayList.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
/*
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
*/
迭代器的删除
练习一:
package demo;
public class Student implements Comparable <Student> {
private String name;
private int age;
private double score;
public Student(String name, int age, double score) {
this.name = name;
this.age = age;
this.score = score;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setScore(double score) {
this.score = score;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public double getScore() {
return score;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +'\'' +"scores='" + score+
'}';
}
@Override
public int compareTo(Student o) {
return (int) (this.score - o.score);
}
}
public class Test {
public static void main(String[] args) {
ArrayList<Student> arrayList = new ArrayList<>(3);
arrayList.add(0,new Student("xiaobai",10,90));
arrayList.add(1,new Student("xiaohong",20,80));
arrayList.add(2,new Student("xiaogang",30,70));
Collections.sort(arrayList); //用它对顺序表排序,arrayList里面的对象那个要重写compareTo()方法
Iterator<Student> it = arrayList.iterator();
while (it.hasNext()){
System.out.println(it.next());
}
}
}
/*
Student{name='xiaogang', age=30'scores='70.0}
Student{name='xiaohong', age=20'scores='80.0}
Student{name='xiaobai', age=10'scores='90.0}
*/
练习二:
public static ArrayList<Character> func(String str1 , String str2){
ArrayList<Character> arrayList = new ArrayList<>();
for (int i = 0; i < str1.length(); i++) {
if(!str2.contains(str1.charAt(i)+"")){
arrayList.add(str1.charAt(i));
}
}
return arrayList;
}
public static void main(String[] args) {
ArrayList<Character> arrayList = Test.func("welcome to bit", "come");
System.out.println(arrayList);
}
总结:
练习三:
class Poker
package demo;
public class Poker {
private String suit;
private int rank;
public Poker(String suit, int rank) {
this.suit = suit;
this.rank = rank;
}
public void setSuit(String suit) {
this.suit = suit;
}
public void setRank(int rank) {
this.rank = rank;
}
public String getSuit() {
return suit;
}
public int getRank() {
return rank;
}
@Override
public String toString() {
return this.suit+" "+this.rank;
}
}
class Pokers
package demo;
import java.util.ArrayList;
import java.util.Random;
public class Pokers {
public static final String[] SUITS = {"♥","♦","♣","♠"};
public ArrayList<Poker> func (){
//创建一个纸盒
ArrayList<Poker> arrayList = new ArrayList<>();
//一共有四个花色,每个花色13张牌
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= 13; j++) {
arrayList.add(new Poker(SUITS[i],j ));
//在这个地方花色要随着I的不同而改变。所以设立一个数组
}
}
return arrayList;
}
public void shuffle(ArrayList<Poker>arrayList){
Random random = new Random();
for(int i = arrayList.size()-1; i > 0; i--){
int ret = random.nextInt(i);
//交换
swap(arrayList , ret , i);
}
}
public static void swap(ArrayList<Poker> arrayList, int ret,int i){
Poker temp = arrayList.get(ret);
arrayList.set(ret,arrayList.get(i));
arrayList.set(i,temp);
}
public static void main(String[] args) {
Pokers pokers = new Pokers();
//建立一个纸盒
ArrayList<Poker> arrayList = pokers.func();
System.out.println(arrayList);
//开始洗牌
pokers.shuffle(arrayList);
System.out.println(arrayList);
//开始揭牌
//3个人每个人抽5张牌
ArrayList<Poker> hand1 = new ArrayList<>();
ArrayList<Poker> hand2 = new ArrayList<>();
ArrayList<Poker> hand3 = new ArrayList<>();
ArrayList<ArrayList<Poker>> hands = new ArrayList<>();
hands.add(hand1);
hands.add(hand2);
hands.add(hand3);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
hands.get(j).add(i,arrayList.get(0));
arrayList.remove(0);
}
}
for (int i = 0; i < hands.size(); i++) {
System.out.println(hands.get(i));
System.out.println();
}
System.out.println("剩余牌的数目");
System.out.println(arrayList);
}
}
/*
[♥ 1, ♥ 2, ♥ 3, ♥ 4, ♥ 5, ♥ 6, ♥ 7, ♥ 8, ♥ 9, ♥ 10, ♥ 11, ♥ 12, ♥ 13, ♦ 1, ♦ 2, ♦ 3, ♦ 4, ♦ 5, ♦ 6, ♦ 7, ♦ 8, ♦ 9, ♦ 10, ♦ 11, ♦ 12, ♦ 13, ♣ 1, ♣ 2, ♣ 3, ♣ 4, ♣ 5, ♣ 6, ♣ 7, ♣ 8, ♣ 9, ♣ 10, ♣ 11, ♣ 12, ♣ 13, ♠ 1, ♠ 2, ♠ 3, ♠ 4, ♠ 5, ♠ 6, ♠ 7, ♠ 8, ♠ 9, ♠ 10, ♠ 11, ♠ 12, ♠ 13]
[♥ 3, ♥ 11, ♠ 13, ♥ 12, ♥ 9, ♣ 1, ♠ 4, ♣ 3, ♦ 5, ♠ 11, ♠ 2, ♠ 5, ♠ 12, ♥ 1, ♦ 3, ♦ 11, ♦ 1, ♣ 11, ♥ 13, ♠ 8, ♣ 13, ♠ 10, ♠ 1, ♦ 12, ♦ 7, ♦ 9, ♥ 7, ♦ 10, ♠ 7, ♠ 9, ♦ 8, ♣ 5, ♠ 3, ♦ 6, ♥ 6, ♦ 4, ♦ 13, ♣ 7, ♥ 5, ♣ 9, ♣ 8, ♥ 4, ♦ 2, ♠ 6, ♥ 8, ♥ 2, ♣ 4, ♣ 10, ♣ 12, ♣ 2, ♥ 10, ♣ 6]
[♥ 3, ♥ 12, ♠ 4, ♠ 11, ♠ 12]
[♥ 11, ♥ 9, ♣ 3, ♠ 2, ♥ 1]
[♠ 13, ♣ 1, ♦ 5, ♠ 5, ♦ 3]
剩余牌的数目
[♦ 11, ♦ 1, ♣ 11, ♥ 13, ♠ 8, ♣ 13, ♠ 10, ♠ 1, ♦ 12, ♦ 7, ♦ 9, ♥ 7, ♦ 10, ♠ 7, ♠ 9, ♦ 8, ♣ 5, ♠ 3, ♦ 6, ♥ 6, ♦ 4, ♦ 13, ♣ 7, ♥ 5, ♣ 9, ♣ 8, ♥ 4, ♦ 2, ♠ 6, ♥ 8, ♥ 2, ♣ 4, ♣ 10, ♣ 12, ♣ 2, ♥ 10, ♣ 6]
*/
总结 1 :
public static final String[] SUITS = {"♥","♦","♣","♠"};
//一共有四个花色,每个花色13张牌
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= 13; j++) {
arrayList.add(new Poker(SUITS[i],j ));
//在这个地方花色要随着I的不同而改变。所以设立一个数组
}
}
一共有4个花色,每种花色有13张牌,很显然用两个for循环,
arrayList.add(new Poker(SUITS[i],j ));
我们想 i == 0 时候一个花色对应13个数字, i == 1 时候一个花色对应13个数字,所以有了上面的静态的数组(花色4中订好了)
总结二:
//开始揭牌
//3个人每个人抽5张牌
ArrayList<Poker> hand1 = new ArrayList<>();
ArrayList<Poker> hand2 = new ArrayList<>();
ArrayList<Poker> hand3 = new ArrayList<>();
ArrayList<ArrayList<Poker>> hands = new ArrayList<>();
hands.add(hand1);
hands.add(hand2);
hands.add(hand3);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
hands.get(j).add(i,arrayList.get(0));
arrayList.remove(0);
}
}
for (int i = 0; i < hands.size(); i++) {
System.out.println(hands.get(i));
System.out.println();
}
三个人每个人摸五张牌:—> 创建一个二维数组:三个人在一个数组中,三个人每个人手中有5张牌,这5张牌放在一个数组中
for (int i = 0; i < 5; i++) { //摸5次
for (int j = 0; j < 3; j++) {
hands.get(j).add(i,arrayList.get(0));//每次都是get(0),因为每次都是摸第一张,第一张一直都在改变
arrayList.remove(0); //摸一次少一张牌
}
}
这个地方为什么倒着写
因为我们想实现的是一个人抹完之后另一个人摸,正着写不可以实现这个功能。