数据结构与算法
Java 数据结构 day2
00 分钟
2024-9-15
2024-9-18
tags
date
type
status
slug
summary
category
password
icon
  • @SuppressWarnings("unchecked")public E get(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("获取位置非法"); } return (E) array[index]; // 将 Object 类型转换为 E 类型返回}
    • (E) 是一个类型转换操作,它将 array[index](一个 Object 类型)强制转换为泛型 E。由于我们在 MyArrayList 中使用了泛型 E,所以我们需要将 Object 类型的元素转成用户指定的类型 E
  • private void expandCapacity() { capacity = capacity + (capacity >> 1); // 扩容为原来的 1.5 倍 Object[] newArray = new Object[capacity]; // 创建新的数组 System.arraycopy(array, 0, newArray, 0, size); // 复制旧数组内容 array = newArray; // 将 array 指向新数组}​
    • System.arraycopy 是一个用于高效复制数组的静态方法,位于 System 类中。它比手动遍历数组一个一个复制元素的效率更高。
    • 参数解析
      • array:原数组(源数组),表示我们要复制的数组。
      • 0:源数组的起始位置,从索引 0 开始复制。
      • newArray:目标数组(目标数组),表示我们要将元素复制到的新数组。
      • 0:目标数组的起始位置,从索引 0 开始存放复制过来的数据。
      • size:表示要复制的元素数量,这里是当前顺序表中已有的元素个数。
  • StringBuilder 是 Java 中用于高效拼接字符串的类。在顺序表的 toString() 方法中,我们使用 StringBuilder 来构建一个包含所有元素的字符串。
    • @Overridepublic String toString() { StringBuilder builder = new StringBuilder(); builder.append("["); for (int i = 0; i < size; i++) { builder.append(array[i]); if (i < size - 1) { builder.append(", "); } } builder.append("]"); return builder.toString();}​
    • StringBuilder 是 Java 中用于字符串拼接的类。它比使用 + 拼接字符串效率高,因为每次 + 拼接字符串时都会创建一个新的 String 对象,而 StringBuilder 可以动态扩展并拼接字符串,避免了频繁创建对象的开销。
    • 构建顺序表的字符串表示:我们希望将顺序表中的所有元素拼接成类似 "[1, 2, 3]" 的字符串,所以使用 StringBuilder 来依次添加元素和逗号。
    • ​package com.test.collection;​public class ArrayList<E> { private int capacity; private int size; private Object[] array;​ public ArrayList(){ this.capacity = 10; this.size = 0; this.array=new Object[capacity]; }​ public void insert(E element,int index){ if(index<0 || index>size){ throw new IndexOutOfBoundsException("插入位置非法"); } if(size==capacity){ expandCapacity(); } for(int i=size;i>index;i--){ array[i]=array[i-1]; } array[index]=element; size++; } public void add(E element){ insert(element,size); }​ //删除指定位置元素 @SuppressWarnings("unchecked") public E remove(int index){ if(index<0 || index>size){ throw new IndexOutOfBoundsException("删除位置非法"); } E removeElment=(E)array[index]; for(int i=index;i<size-1;i++){ array[i]=array[i+1]; } array[size-1]=null; size--; return removeElment; }​ //获取指定位置的元素 @SuppressWarnings("unchecked") public E get(int index){ if (index<0 || index>size){ throw new IndexOutOfBoundsException("获取位置非法"); } return (E)array[index]; }​ @SuppressWarnings("unchecked") public E set(int index,E element){ if(index<0 || index>size){ throw new IndexOutOfBoundsException("更新位置非法"); } E oldElement=(E)array[index]; array[index]=element; return oldElement; }​ private void expandCapacity(){ capacity =capacity+(capacity>>1); Object[] newArray=new Object[capacity]; System.arraycopy(array,0,newArray,0,size); array=newArray; }​ public int size(){ return size; }​ //重写 toString方法,打印顺序表内容 @Override public String toString() { StringBuilder builder =new StringBuilder(); builder.append("["); for(int i=0;i<size;i++){ builder.append(array[i]); if(i!=size-1){ builder.append(","); } } builder.append("]"); return builder.toString(); }}​​​
    • Main
      • package com.test;​import com.test.collection.ArrayList;​public class Main { public static void main(String[] args){ ArrayList<Integer> list=new ArrayList<>();​ //末尾添加元素 for(int i=0;i<10;i++){ list.add(i); } System.out.println("顺序表内容: "+ list);​ //指定位置插入元素 list.insert(100,5); System.out.println("在位置5插入100后的顺序表: " +list);​ //删除指定位置元素 list.remove(3); System.out.println("删除位置3的元素后的顺序表: " + list);​ // 获取指定位置的元素 System.out.println("获取位置2的元素: " + list.get(2));​ // 更新指定位置的元素 list.set(2, 999); System.out.println("将位置2的元素更新为999后的顺序表: " + list);​ // 输出当前顺序表的大小 System.out.println("当前顺序表的大小: " + list.size()); }​​}​
上一篇
左神数据结构算法Day1
下一篇
Java 数据结构第0天

评论
Loading...