• 【数据结构】——顺序表


    一、认识线性表和顺序表

    1. 线性表是n个具有相同特性的数据元素的有限序列,常见的线性表有:顺序表、链表、栈、队列。
    2. 线性表在逻辑上是线性结构,也就说是连续的一条直线。但在物理结构上并不一定是连续的,线性表在物理上存储时,通常是以数组和链式结构的形式存储的。
    3. 顺序表是用一段物理地址连续的存储单元一次存储数据元素的线性结构,一般情况下采用数组存储,在数组上完成数据的增删查改。

    二、顺序表的接口实现

    • 打印顺序表 
    1. public void display() {
    2. for (int i = 0; i < usedSize; i++) {
    3. System.out.print(elem[i] + " ");
    4. }
    5. System.out.println();
    6. }
    • 新增元素,默认在数组最后新增

    1. public void add(int data) {
    2. if (isFull()){
    3. elem = Arrays.copyOf(elem,2* elem.length);
    4. }
    5. elem[usedSize++] = data;
    6. }
    7. public boolean isFull(){
    8. return usedSize == elem.length;
    9. }
    • 在 pos 位置新增元素

    1. private void checkAddPos(int pos){
    2. if (pos < 0 || pos > usedSize){
    3. throw new PosIndexNotLegalException("pos位置不合法");
    4. }
    5. }
    6. public void add(int pos, int data) {
    7. try {
    8. checkAddPos(pos);
    9. if (isFull()){
    10. elem = Arrays.copyOf(elem,2* elem.length);
    11. }
    12. for (int i = usedSize-1; i >= pos; i++) {
    13. elem[i+1] = elem[i];
    14. }
    15. elem[pos] = data;
    16. usedSize++;
    17. }catch (PosIndexNotLegalException e){
    18. e.printStackTrace();
    19. }
    20. }
    •   判定是否包含某个元素
    1. public boolean contains(int toFind) {
    2. for (int i = 0; i < usedSize; i++) {
    3. if (elem[i] == toFind){
    4. return true;
    5. }
    6. }
    7. return true;
    8. }
    • 查找某个元素对应的位置
    1. public int indexOf(int toFind) {
    2. for (int i = 0; i < usedSize; i++) {
    3. if (elem[i] == toFind){
    4. return i;
    5. }
    6. }
    7. return -1;
    8. }
    •  获取 pos 位置的元素
    1. private void checkGetPos(int pos){
    2. if (pos < 0 || pos > usedSize){
    3. throw new PosIndexNotLegalException("pos位置不合法");
    4. }
    5. }
    6. public int get(int pos) {
    7. int retVal = -1;
    8. try {
    9. checkGetPos(pos);
    10. retVal = elem[pos];
    11. }catch (PosIndexNotLegalException e){
    12. e.printStackTrace();
    13. }
    14. return retVal;
    15. }
    16. // 给 pos 位置的元素设为 value
    17. public void set(int pos, int value) {
    18. checkGetPos(pos);
    19. elem[pos] = value;
    20. }
    •  删除第一次出现的关键字key
    1. public void remove(int key) {
    2. int index = indexOf(key);
    3. if (index == -1){
    4. System.out.println("没有你要删除的数字!");
    5. return;
    6. }
    7. for (int i = index; i < usedSize-1; i++) {
    8. elem[i] = elem[i+1];
    9. }
    10. usedSize--;
    11. }
    12. // 获取顺序表长度
    13. public int size() {
    14. return usedSize;
    15. }
    16. // 清空顺序表
    17. public void clear() {
    18. usedSize = 0;
    19. }

    三、ArrayList的使用

    3.1 ArrayList的构造

    1. 无参构造 :ArrayList()
    2. 利用其他Collection构建ArrayList:ArrayList(Collection c)
    3. 指定顺序表初始容量: ArrayList(int intialCapacity)
    1. public static void main(String[] args) {
    2. List<Integer> list1 = new ArrayList<>();
    3. aList.add(1);
    4. list1.add(2);
    5. list1.add(3);
    6. System.out.println(list1);
    7. List<Integer> list2 = new ArrayList<>(10);//扩容是1.5
    8. // list3构造好之后,与list中的元素一致
    9. ArrayList<Integer> list3 = new ArrayList<>(list2);
    10. }

    3.2 ArrayList常见操作

     3.3 ArrayList的遍历

    1、for循环加下标

    1. public static void main(String[] args) {
    2. List<Integer> list = new ArrayList<>();
    3. list.add(1);
    4. list.add(2);
    5. list.add(3);
    6. list.add(4);
    7. list.add(5);
    8. // 使用下标+for遍历
    9. for (int i = 0; i < list.size(); i++) {
    10. System.out.print(list.get(i) + " ");
    11. }
    12. System.out.println();
    13. }

     2、foreach遍历

    1. public static void main(String[] args) {
    2. List<Integer> list = new ArrayList<>();
    3. list.add(1);
    4. list.add(2);
    5. list.add(3);
    6. list.add(4);
    7. list.add(5);
    8. // 借助foreach遍历
    9. for (Integer integer : list) {
    10. System.out.print(integer + " ");
    11. }
    12. System.out.println();
    13. }

     3、迭代器遍历

    1. public static void main(String[] args) {
    2. List<Integer> list = new ArrayList<>();
    3. list.add(1);
    4. list.add(2);
    5. list.add(3);
    6. list.add(4);
    7. list.add(5);
    8. Iterator<Integer> it = list.listIterator();
    9. while (it.hasNext()) {
    10. System.out.print(it.next() + " ");
    11. }
    12. System.out.println();
    13. }

    注:ArrayList扩容是按照1.5倍大小扩容,乳沟多用户的所需大小超过预估的1.5倍大小,则按照用户所需大小进行扩容,在真正扩容之前检查是否能扩容扩容成功,防止太大导致扩容失败。

  • 相关阅读:
    立足中国,聚焦欧洲,蔚来汽车的如意算盘真会打?
    Java基础--阳光总在风雨后,请相信彩虹
    面试题:什么是this.$nextTick
    【完美解决】IDEA 中 Maven 报错 Cannot resolve xxx 和 Maven 中 Dependencies 报红报错。
    MATLAB运算符
    土壤温湿度传感器
    springboot集成swagger3+解决页面无法访问问题
    SQL Server创建链接服务器的方法
    基于stm32单片机的台历日历计时器万年历Proteus仿真
    【JavaScript系列】01_初识JS
  • 原文地址:https://blog.csdn.net/weixin_62678196/article/details/126381915