顺序表 ArrayList java实现

目录

自己实现顺序表结构

顺序表方法

调用顺序表

异常抛出

ArrayList的构造

ArrayList的常用操作

ArrayList的遍历

ArrayList的扩容机制(重要!)

ArrayList的具体使用

1.给出两个字符串,求去掉相同元素后的字符串

2.杨辉三角

3.简单的洗牌算法

文件架构:

实现Poker类:

方法调用(main方法):

方法实现:


 

自己实现顺序表结构

架构

顺序表方法

import java.util.Arrays;

public class MyArrayList {
    public int[] elem;
    public int usedSize;  //  表示当前数组有多少个有效元素
    public static final int DEFAULT_SIZE = 10;

    public MyArrayList(){
        this.elem = new int[DEFAULT_SIZE];
    }
    // 打印顺序表,注意:该方法并不是顺序表中的方法,为了方便看测试结果给出的
    public void display() {
        for (int i = 0; i < usedSize; i++) {
            System.out.println(this.elem[i] + " ");
        }
    }

    // 获取顺序表长度
     public int size() {
        return this.usedSize;
    }
    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < usedSize; i++) {
            if(this.elem[i] == toFind){
                return true;
            }
        }
        return false;
    }

    // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < usedSize; i++) {
        if(this.elem[i] == toFind){
            return i;
            }
        }
        return -1;
    }

    // 判断 是否 为满
    public boolean isFull(){
        return this.usedSize == this.elem.length;
    }



    // 新增元素,默认在数组最后新增
    public void add(int data) {
        if(this.isFull()){
            // 线性表扩容
            this.elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[usedSize] = data;
        usedSize++;
    }

    // 检查 pos 的位置是否在合法的范围
    public boolean checkPos(int pos){
        if(pos < 0 || pos > usedSize){
            return true;
        }
        return false;
    }

    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        if(checkPos(pos)){
            System.out.println("pos的位置不合法,插入失败");
        }else {
            for (int i = usedSize-1; i >= pos; i--) {
                elem[i+1] = elem[i];
            }
            elem[pos] = data;
            usedSize++;
        }
    }

    // 判断 pos 的位置 是否合法
    private void checkGetPos(int pos){
        if(pos < 0 || pos >= usedSize){
            throw new IndexOutOfException
                    ("pos的位置不合法,获取元素失败"){ };
        }
    }
    // 获取 pos 位置的元素
    public int get(int pos) {
        checkGetPos(pos);
        return elem[pos];
    }
    // 给 pos 位置的元素设为 value
    public void set(int pos, int value) {
        checkGetPos(pos);
        elem[pos] = value;
    }

    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        int index = indexOf(toRemove);
        if(index == -1){
            System.out.println("没有这个数据");
        }else{
            for (int i = index; i <= usedSize-1; i++) {
                elem[i] = elem[i+1];
            }
            usedSize--;
        }
    }

    // 清空顺序表
    public void clear() {
        for (int i = 0; i < usedSize; i++) {
            elem[i] = 0;
        }
        usedSize = 0;
    }

}
调用顺序表

public class test {
    public static void main(String[] args) {
        MyArrayList myArrayList = new MyArrayList();
        myArrayList.add(1);
        myArrayList.add(2);
        myArrayList.add(3);
        myArrayList.add(4);
        myArrayList.display();
        System.out.println("================");
        //System.out.println(myArrayList.get(2));

        myArrayList.remove(2);
        myArrayList.remove(1);
        myArrayList.display();
        myArrayList.clear();
        myArrayList.display();

    }
}
异常抛出


// 负责抛出异常
public class IndexOutOfException extends RuntimeException{
    public IndexOutOfException(){

    }

    public IndexOutOfException(String msg){
        super(msg);
    }

}

ArrayList的构造

// ArrayList创建,推荐写法
// 构造一个空的列表
List<Integer> list1 = new ArrayList<>();
// 构造一个具有10个容量的列表
List<Integer> list2 = new ArrayList<>(10);
list2.add(1);
list2.add(2);
list2.add(3);

ArrayList的常用操作

ArrayList的遍历

常使用for循环+下标 以及 foreach 来遍历顺序表

List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
// 使用下标+for遍历
for (int i = 0; i < list.size(); i++) {
System.out.print(list.get(i) + " ");
}
System.out.println();
// 借助foreach遍历
for (Integer integer : list) {
System.out.print(integer + " ");
}
System.out.println();
Iterator<Integer> it = list.listIterator();
while(it.hasNext()){
System.out.print(it.next() + " ");
}
System.out.println();

ArrayList的扩容机制(重要!)

        Ctrl + 点击arrayList.add() 进入源码查看其实现流程,关键是要理解其思想 (待学

ArrayList的具体使用

1.给出两个字符串,求去掉相同元素后的字符串

package dataStructArrayList;

import java.util.ArrayList;

public class test {
    public static void main(String[] args) {
        ArrayList<Character> List = new ArrayList<>();

        String s1 = "welcome to bit";
        String s2 = "come";

        for (int i = 0; i < s1.length(); i++) {
            char ch = s1.charAt(i);
            if(!s2.contains(ch+"")){
                List.add(ch);
            }
        }
        for (int i = 0; i < List.size(); i++) {
            System.out.print(List.get(i));
        }

    }
}
2.杨辉三角

118. 杨辉三角 - 力扣(LeetCode)

class Solution {
    public List<List<Integer>> generate(int numRows) {
        //  实现杨辉三角
        List<List<Integer>> ret = new ArrayList<>();
        List<Integer> row = new ArrayList<>();
        
        //先设置第一行,第一列
        row.add(1);
        ret.add(row);

        //  从第二行开始
        for (int i = 1; i < numRows; i++) {
            List<Integer> preRow = ret.get(i-1); 
            List<Integer> nowRow = new ArrayList<>();
            
            //  每行第一个元素是1
            nowRow.add(1);
            //  求中间的元素
            for (int j = 1; j < i; j++) {
                int num = preRow.get(j-1) + preRow.get(j);
                nowRow.add(num);
            }
            //   每行最后一个元素是1
            nowRow.add(1);
            ret.add(nowRow);
        }
        return ret;
    }
}
3.简单的洗牌算法

生成四种花色,共52张扑克牌,要求实现 洗牌,发牌功能。 每次每个人发五张牌。

文件架构:

实现Poker类:
package dataStructArrayList;

public class Poker {
    // 扑克牌算法

    private String suit; // 花色
    private int rank; //  数字

    public Poker(String suit, int rank) {
        this.suit = suit;
        this.rank = rank;
    }

    @Override
    public String toString() {
        return "Poker{" +
                "suit='" + suit + '\'' +
                ", rank=" + rank +
                '}';
    }
}
方法调用(main方法):
package dataStructArrayList;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class test {

    public static void main(String[] args) {
        Game game = new Game();
        // 生成 52 张扑克牌
        List<Poker> pokers = game.buyPoker();
        System.out.println(pokers);
        System.out.println("洗牌后:");
        game.swapPoker(pokers);
        System.out.println(pokers);;

        System.out.println("===========");
        List<List<Poker>> hand = game.takePoker(pokers);
        System.out.println(hand.get(0));
        System.out.println(hand.get(1));
        System.out.println(hand.get(2));

    }

}
方法实现:
package dataStructArrayList;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class Game {

    private static final String[] suits = {"♥","♣","♦","♠"};

    //  生成扑克牌
    public List<Poker> buyPoker(){
        List<Poker> pokers = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 1; j <= 13 ; j++) {
//                String suit = suits[i];
//                int rank = j;
//                Poker poker = new Poker(suit,rank);
                Poker poker = new Poker(suits[i],j);
                pokers.add(poker);
            }
        }
        return pokers;
    }

    //  洗牌
    public void swapPoker(List<Poker> pokers){
        for (int i = pokers.size()-1; i > 0 ; i--) {
            // 找到随机 下标j
            Random random = new Random();
            int j = random.nextInt(i);
            //  创建一个临时变量接收 i下标 的值
            Poker tmp = pokers.get(i);
            //  将j位置的值给到 i位置
            pokers.set(i,pokers.get(j));
            //  将i位置的值给到 j位置
            pokers.set(j,tmp);
        }
    }

    //  揭牌,发牌
    public List<List<Poker>> takePoker(List<Poker> pokers){
        List<List<Poker>> hand = new ArrayList<>();
        List<Poker> hand1 = new ArrayList<>();
        List<Poker> hand2 = new ArrayList<>();
        List<Poker> 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++) {
                Poker removePoke = pokers.get(0);
                pokers.remove(0);
                hand.get(j).add(removePoke);
            }
        }
        return hand;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值