- public class MyArrayList {
- public int[] array;
- public int usedSize;//记录有效数据
- public static final int DEFAULT_CAPACITY=2;
- public MyArrayList(){
- this.array=new int[DEFAULT_CAPACITY];
- }
- //打印顺序表
- public void display() {
- for (int i = 0; i < this.usedSize; i++) {
- System.out.print(this.array[i]);
- }
- System.out.println();
- }
- // 新增元素,默认在数组最后新增
- //在数据结构中,每次添加数据前面都要有前驱信息,不能跳着放
- public void add(int data) {
- try{
- if(isFull()){
- array= Arrays.copyOf(array,array.length*2);
- }
- }catch (NegativeArraySizeException e){
- e.printStackTrace();
- }
- array[usedSize++]=data;
- }
- public boolean isFull(){
- return usedSize==array.length;
- }
- public void isPosLegal(int pos){
- if(pos<0||pos>usedSize){
- throw new PosIndexNotLegal("pos下标不合法");
- }
- }
- // 在 pos 位置新增元素
- public void add(int pos, int data) {
- try {
- isPosLegal(pos);
- //判断是否满了
- if (isFull()){
- array=Arrays.copyOf(array,array.length*2);
- }
- for (int i = usedSize-1; i >=pos ; i--) {
- array[i+1]=array[i];
- }
- array[pos]=data;
- usedSize++;
- }catch (PosIndexNotLegal e){
- e.printStackTrace();
- }
- }
- // 判定是否包含某个元素
- public boolean contains(int toFind) {
- for (int i = 0; i < usedSize; i++) {
- if(array[i]==toFind){
- return true;
- }
- }
- return false;
- }
- // 查找某个元素对应的位置
- public int indexOf(int toFind) {
- for (int i = 0; i < usedSize; i++) {
- if(array[i]==toFind){
- return i;
- }
- }
- return -1;
- }
- // 获取 pos 位置的元素
- public void checkGetPos(int pos){
- if(pos<0||pos>=usedSize){
- throw new PosIndexNotLegal("pos下标不合法");
- }
- }
- public int get(int pos) {
- /**
- try {
- checkGetPos(pos);
- }catch (PosIndexNotLegal e){
- //处理异常的代码
- e.printStackTrace();
- }
- return array[pos];
- */
- checkGetPos(pos);
- return array[pos];
- }
- // 给 pos 位置的元素设为 value
- public void set(int pos, int value) {
- checkGetPos(pos);
- array[pos]=value;
- }
- //删除第一次出现的关键字key
- public void remove(int key) {
- int index=indexOf(key);
- if(index==-1){
- System.out.println("没有这个数字");
- return;
- }else {
- for (int i = index; i < usedSize-1; i++) {
- array[i]=array[i+1];
- }
- }
- //array[usedSize-1]=null如果是引用类型,要置为null,防止内存泄露
- usedSize--;
- }
- // 获取顺序表长度
- public int size() {
- return usedSize;
- }
- // 清空顺序表
- public void clear() {
- /**
- for (int i = 0; i < usedSize; i++) {
- array[i]=null;
- }
- */
- usedSize=0;
- }
- }
1.ArrayList()无参构造
- public static void main(String[] args) {
- ArrayList<Integer> arrayList=new ArrayList<>();//顺序表大小是0
- arrayList.add(1);//大小变为10
- arrayList.add(2);
- //.........当添加到10个时,要再次扩容
- }
通过源码分析整个构造过程(按照我的箭头,从上到下分析扩容原理)

2.ArrayList(int initialCapacity) 指定大小的构造方法
- public static void main(String[] args) {
-
- ArrayList<Integer> arrayList=new ArrayList<>(15);
- }
3.ArrayList(Collection<? extends E> c) 利用Collection接口构造
- public static void main(String[] args) {
- /**
- * 都实现了Collection接口
- */
- ArrayList<Integer> arrayList2=new ArrayList<>(15);
- ArrayList<Integer> arrayList3=new ArrayList<>(arrayList2);
- LinkedList<Integer>linkedList=new LinkedList<>();
- ArrayList<Integer> arrayList4=new ArrayList<>(linkedList);
- }
- public static void main(String[] args) {
- List<String> list=new ArrayList<>();
- list.add("javase");
- list.add("javaee");
- list.add("javaweb");
- list.add("jvm");
- list.add("测试课程");
- /**
- * 直接打印
- */
- System.out.println(list);
- /**
- * for循环
- */
- for (int i = 0; i < list.size(); i++) {
- System.out.print(list.get(i)+" ");
- }
- System.out.println();
- /**
- * for each
- * 冒号的左边是集合元素,冒号的右边是集合
- */
- for (String x: list) {
- System.out.print(x+" ");
- }
- /**
- *迭代器1
- */
- Iterator<String> it=list.iterator();
- while (it.hasNext()){
- System.out.print(it.next());
- }
- System.out.println();
- /**
- * 迭代器2
- */
- ListIterator<String> it2= list.listIterator();
- while (it2.hasNext()){
- System.out.print(it2.next());
- }
- }
扑克牌(铁甲版本)
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Random;
-
- /**
- * 一张扑克牌
- */
- class Card{
- public int number;
- public String color;
-
- public Card(int number, String color) {
- this.number = number;
- this.color = color;
- }
-
- @Override
- public String toString() {
- return ""+number+" "+color;
- }
- }
-
- /**
- * 一套扑克牌
- */
- public class CardDemo {
-
- public static final String[] colors=new String[]{"♥","♠","♦","♣"};//花色
-
- /**
- * 准备一套扑克牌
- * @return
- */
- public List<Card> cardList(){
- List<Card> cards=new ArrayList<>();
- for (int i = 1; i <=13; i++) {
- for (int j = 0; j < 4; j++) {
- Card card=new Card(i,colors[j]);
- cards.add(card);
- }
- }
- return cards;
- }
-
- /**
- * 洗牌
- * @param cards
- * @param i
- * @param j
- */
- private 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 void washCard(List<Card> cards){
- Random random=new Random();
- for (int i = cards.size()-1; i >0 ; i--) {
- int index= random.nextInt(i);;
- swap(cards,i,index);
- }
- }
- /**
- * 分牌,每人5张
- */
- public List<List<Card>> DistributeCard(List<Card> cards){
- List<Card>person1=new ArrayList<>();
- List<Card>person2=new ArrayList<>();
- List<Card>person3=new ArrayList<>();
- List<List<Card>> persons=new ArrayList<>();//persons里面的元素是person,所以<>放的元素是List<Card>
- /**
- * 给persons数组里面放3个人,每个人又是一个数组,也就是二维数组
- */
- persons.add(person1);
- persons.add(person2);
- persons.add(person3);
- for (int i = 0; i < 5; i++) {//分5次
- for (int j = 0; j < 3; j++) {//3个人
- Card card= cards.remove(0);//不断删除0位置的牌,从0位置取牌
- persons.get(j).add(card);
- }
- }
- return persons;
- }
- }
- import java.util.List;
-
- public class Test {
- public static void main(String[] args) {
- CardDemo cardDemo=new CardDemo();
- List<Card>ret=cardDemo.cardList();
- System.out.println("洗牌前");
- System.out.println(ret);
- System.out.println("洗牌后");
- cardDemo.washCard(ret);
- System.out.println(ret);
- List<List<Card>> ret2=cardDemo.DistributeCard(ret);
- for (int i = 0; i <3; i++) {
- System.out.println("第"+(i+1)+"个人的牌"+ret2.get(i));
- }
- }
- }
上面问题可以用链表来解决
链表:是以链式的形式进行存储
顺序表:是以顺序的形式进行存储