diff --git a/data_structure/pom.xml b/data_structure/pom.xml new file mode 100644 index 0000000..4e18962 --- /dev/null +++ b/data_structure/pom.xml @@ -0,0 +1,12 @@ + + + 4.0.0 + + org.example + data_structure + 1.0-SNAPSHOT + + + \ No newline at end of file diff --git a/data_structure/src/main/java/sort/MyArr.java b/data_structure/src/main/java/sort/MyArr.java new file mode 100644 index 0000000..1ebdcf7 --- /dev/null +++ b/data_structure/src/main/java/sort/MyArr.java @@ -0,0 +1,95 @@ +package sort; + +import java.util.Arrays; + +/** + * @author Sawyer + * @date 2022/7/21 + */ +public class MyArr { + private int[] elements; + + public MyArr() { + elements = new int[0]; + } + + /** + * 获取数组长度 + * @return 返回数组长度 + */ + public int size(){ + return elements.length; + } + + /** + * 往数组末尾添加一个元素 + * @param element 新元素 + */ + public void add(int element){ + int[] newArr = new int[elements.length+1]; + System.arraycopy(elements, 0, newArr, 0, elements.length); + newArr[elements.length] = element; + elements = newArr; + } + + /** + * 打印元素列表 + */ + public void show(){ + System.out.println(Arrays.toString(elements)); + } + + /** + * 通过数组下标删除元素 + * @param index 数组下标 + */ + public void remove(int index){ + checkIndex(index); + int[] newArr = new int [elements.length-1]; + for (int i = 0; i elements.length){ + throw new RuntimeException("下标越界"); + } + } +} diff --git a/data_structure/src/main/java/sort/MyArrTest.java b/data_structure/src/main/java/sort/MyArrTest.java new file mode 100644 index 0000000..77d9554 --- /dev/null +++ b/data_structure/src/main/java/sort/MyArrTest.java @@ -0,0 +1,21 @@ +package sort; + +/** + * @author Sawyer + * @date 2022/7/21 + */ +public class MyArrTest { + public static void main(String[] args) { + MyArr myArr = new MyArr(); + myArr.add(1); + myArr.add(2); + myArr.show(); + int count = myArr.getElement(0); + System.out.println(count); + myArr.size(); + myArr.insert(0,22); + myArr.show(); + myArr.remove(0); + myArr.show(); + } +}