From ebe7a767c5b32e084121078f62851067fe3bdd10 Mon Sep 17 00:00:00 2001 From: sawyergore <3077528351@qq.com> Date: Thu, 21 Jul 2022 21:23:37 +0800 Subject: [PATCH] =?UTF-8?q?#001=5F=E9=9D=A2=E5=90=91=E5=AF=B9=E8=B1=A1?= =?UTF-8?q?=E6=95=B0=E7=BB=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data_structure/pom.xml | 12 +++ data_structure/src/main/java/sort/MyArr.java | 95 +++++++++++++++++++ .../src/main/java/sort/MyArrTest.java | 21 ++++ 3 files changed, 128 insertions(+) create mode 100644 data_structure/pom.xml create mode 100644 data_structure/src/main/java/sort/MyArr.java create mode 100644 data_structure/src/main/java/sort/MyArrTest.java 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(); + } +}