|
@@ -0,0 +1,152 @@
|
|
|
|
+package j1_java_basic.J20250608;
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+import java.util.Arrays;
|
|
|
|
+
|
|
|
|
+public class MyArray {
|
|
|
|
+ //数组长度,每次操作(增加元素、删除元素)都要变动数组长度
|
|
|
|
+ static int length = 0;
|
|
|
|
+ //创建一个数组,每次操作的都是这个数组
|
|
|
|
+ static int[] arr = new int[length];
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 添加元素的方法,每调用一次该方法,传入参数,就会向数组中添加一个元素。
|
|
|
|
+ *
|
|
|
|
+ * @param e
|
|
|
|
+ */
|
|
|
|
+ public static void add(int e) {
|
|
|
|
+//1、创建一个临时的数组,长度是原数组长度+1
|
|
|
|
+ int[] tempArray = new int[length + 1];
|
|
|
|
+ //2、把原数组中的元素赋值到新数组中
|
|
|
|
+ for (int i = 0; i < arr.length; i++) {
|
|
|
|
+ tempArray[i] = arr[i];
|
|
|
|
+ }
|
|
|
|
+ //3、把要添加的元素e赋值到新数组的最后
|
|
|
|
+ tempArray[tempArray.length - 1] = e;
|
|
|
|
+ //4、用新数组替换掉旧的数组
|
|
|
|
+ arr = tempArray;
|
|
|
|
+ //5、修改长度变量length
|
|
|
|
+ length = arr.length;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 删除最后1个元素的方法
|
|
|
|
+ */
|
|
|
|
+ public static void removeLast() {
|
|
|
|
+ //1、创建一个临时的数组,长度是原数组长度-1
|
|
|
|
+ int[] tempArray = new int[length - 1];
|
|
|
|
+ //2、把原数组中的元素赋值到新数组中
|
|
|
|
+ for (int i = 0; i < tempArray.length; i++) {
|
|
|
|
+ tempArray[i] = arr[i];
|
|
|
|
+ }
|
|
|
|
+ //3、用新数组替换掉旧的数组
|
|
|
|
+ arr = tempArray;
|
|
|
|
+ //4、修改长度变量length
|
|
|
|
+ length = arr.length;
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 删除第1个元素的方法
|
|
|
|
+ */
|
|
|
|
+ public static void removeFirst() {
|
|
|
|
+ //1、创建一个临时的数组,长度是原数组长度-1
|
|
|
|
+ int[] tempArray = new int[length - 1];
|
|
|
|
+
|
|
|
|
+ //2、把原数组中的元素赋值到新数组中
|
|
|
|
+ for (int i = 0; i < tempArray.length; i++) {
|
|
|
|
+ //原数组跳过第一个元素,后面的依次赋值给新数组
|
|
|
|
+ tempArray[i] = arr[i + 1];
|
|
|
|
+ }
|
|
|
|
+ //3、用新数组替换掉旧的数组
|
|
|
|
+ arr = tempArray;
|
|
|
|
+ //4、修改长度变量length
|
|
|
|
+ length = arr.length;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 删除指定位置元素的方法
|
|
|
|
+ *
|
|
|
|
+ * @param index 索引值
|
|
|
|
+ */
|
|
|
|
+ public static void remove(int index) {
|
|
|
|
+ //1、创建一个临时的数组,长度是原数组长度-1
|
|
|
|
+ int[] tempArray = new int[length - 1];
|
|
|
|
+
|
|
|
|
+ //2、把原数组中的元素赋值到新数组中
|
|
|
|
+ for (int i = 0; i < tempArray.length; i++) {
|
|
|
|
+ if(i<index){
|
|
|
|
+ tempArray[i] = arr[i];
|
|
|
|
+ }else {
|
|
|
|
+ //原数组跳过第一个元素,后面的依次赋值给新数组
|
|
|
|
+ tempArray[i] = arr[i + 1];
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ //3、用新数组替换掉旧的数组
|
|
|
|
+ arr = tempArray;
|
|
|
|
+ //4、修改长度变量length
|
|
|
|
+ length = arr.length;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 快速输出数组元素到控制台的方法
|
|
|
|
+ */
|
|
|
|
+ public static void printArray() {
|
|
|
|
+ System.out.println(Arrays.toString(arr));
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 返回数组的长度
|
|
|
|
+ * @return 数组长度
|
|
|
|
+ */
|
|
|
|
+ public static int size(){
|
|
|
|
+ return length;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 判断数组是否为空
|
|
|
|
+ * @return true(空) | false(不为空)
|
|
|
|
+ */
|
|
|
|
+ public static boolean isEmpty(){
|
|
|
|
+ if (arr==null||length==0){
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 判断数组是否不为空
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static boolean isNotEmpty(){
|
|
|
|
+ return !isEmpty();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 根据索引值查找数组元素,如果数组中没有这个元素就返回-1
|
|
|
|
+ * @param index 索引值
|
|
|
|
+ * @return 数组元素
|
|
|
|
+ */
|
|
|
|
+ public static int getElement(int index){
|
|
|
|
+ if(length<index) {
|
|
|
|
+ return arr[index];
|
|
|
|
+ }
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 查找数组中是否包含指定元素,包含则返回索引值,不包含则返回-1
|
|
|
|
+ * @param element 要查找的元素
|
|
|
|
+ * @return 索引值
|
|
|
|
+ */
|
|
|
|
+ public static int getIndex(int element){
|
|
|
|
+ for (int i = 0; i < arr.length; i++) {
|
|
|
|
+ if (arr[i]==element){
|
|
|
|
+ return i;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return -1;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+}
|