|
@@ -0,0 +1,187 @@
|
|
|
+package com.loveCoding.j20250607_method;
|
|
|
+
|
|
|
+import java.util.Arrays;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author WanJl
|
|
|
+ * @version 1.0
|
|
|
+ * @title MyArray
|
|
|
+ * @description
|
|
|
+ * @create 2025/6/7
|
|
|
+ */
|
|
|
+public class MyArray {
|
|
|
+ static int length=0;
|
|
|
+ static int[] myArray=new int[length];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 返回数组长度的方法
|
|
|
+ */
|
|
|
+ public static int size(){
|
|
|
+ return length;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断数组是否为空的方法
|
|
|
+ * 数组为空,代表两个含义:
|
|
|
+ * 1、数组长度为0
|
|
|
+ * 2、数组为null
|
|
|
+ * @return true就是为空,false就是不为空
|
|
|
+ */
|
|
|
+ public static boolean isEmpty(){
|
|
|
+ return myArray.length==0||myArray==null||length==0?true:false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 判断数组不为空
|
|
|
+ * @return true就是不为空,false就是为空
|
|
|
+ */
|
|
|
+ public static boolean isNotEmpty(){
|
|
|
+ return !isEmpty();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 该方法用来像myArray数组中添加一个元素,
|
|
|
+ * 并且数组长度会随着添加元素的个数进行变化。
|
|
|
+ * @param element
|
|
|
+ */
|
|
|
+ public static void add(int element){
|
|
|
+ //1、建立一个临时数组,临时数组的长度是旧数组长度+1
|
|
|
+ int[] tempArray=new int[length+1];
|
|
|
+ //2、把原来的数组中的元素复制到新数组中
|
|
|
+ for (int i = 0; i < myArray.length; i++) {
|
|
|
+ tempArray[i]=myArray[i];
|
|
|
+ }
|
|
|
+ //3、把新元素添加到新数组中最后一位
|
|
|
+ tempArray[tempArray.length-1]=element;
|
|
|
+ //4、用新数组替换掉旧的数组,赋值给myArray
|
|
|
+ myArray=tempArray;
|
|
|
+ //5、把数组长度赋值给length属性
|
|
|
+ length=myArray.length;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ 编写一个方法,调用该方法,删除数组中最后一个元素
|
|
|
+ */
|
|
|
+ public static void removeLast(){
|
|
|
+ //1、创建新的临时数组
|
|
|
+ int[] tempArray=new int[length-1];
|
|
|
+ //2、把原来的数组中的元素复制到新数组中
|
|
|
+ for (int i = 0; i < tempArray.length; i++) {
|
|
|
+ tempArray[i]=myArray[i];
|
|
|
+ }
|
|
|
+ //3、用新数组替换掉旧的数组,赋值给myArray
|
|
|
+ myArray=tempArray;
|
|
|
+ //4、把数组长度赋值给length属性
|
|
|
+ length=myArray.length;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ 编写一个方法,调用该方法,删除数组中第一个元素
|
|
|
+ */
|
|
|
+ public static void removeFirst(){
|
|
|
+ //1、创建新的临时数组
|
|
|
+ int[] tempArray=new int[length-1];
|
|
|
+ //2、把原来的数组中的元素复制到新数组中
|
|
|
+ for (int i = 0; i < tempArray.length; i++) {
|
|
|
+ tempArray[i]=myArray[i+1];
|
|
|
+ }
|
|
|
+ //3、用新数组替换掉旧的数组,赋值给myArray
|
|
|
+ myArray=tempArray;
|
|
|
+ //4、把数组长度赋值给length属性
|
|
|
+ length=myArray.length;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 向指定位置添加元素
|
|
|
+ * @param element 元素值
|
|
|
+ * @param index 索引值
|
|
|
+ */
|
|
|
+ public static void add(int element,int index){
|
|
|
+ //1、创建新的临时数组
|
|
|
+ int[] tempArray=new int[length+1];
|
|
|
+ //2、把原来的数组中的元素复制到新数组中
|
|
|
+ for (int i = 0; i < myArray.length; i++) {
|
|
|
+ if(i<index){
|
|
|
+ tempArray[i]=myArray[i];
|
|
|
+ }else{
|
|
|
+ tempArray[i+1]=myArray[i];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //在指定位置添加元素
|
|
|
+ tempArray[index]=element;
|
|
|
+ //3、用新数组替换掉旧的数组,赋值给myArray
|
|
|
+ myArray=tempArray;
|
|
|
+ //4、把数组长度赋值给length属性
|
|
|
+ length=myArray.length;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据索引值删除数组中指定元素
|
|
|
+ * @param index
|
|
|
+ */
|
|
|
+ public static void remove(int index){
|
|
|
+ //1、创建新的临时数组
|
|
|
+ int[] tempArray=new int[length-1];
|
|
|
+ //2、把原来的数组中的元素复制到新数组中
|
|
|
+ for (int i = 0; i < tempArray.length; i++) {
|
|
|
+ if (i<index){
|
|
|
+ tempArray[i]=myArray[i];
|
|
|
+ }else if (i>=index){
|
|
|
+ tempArray[i]=myArray[i+1];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //3、用新数组替换掉旧的数组,赋值给myArray
|
|
|
+ myArray=tempArray;
|
|
|
+ //4、把数组长度赋值给length属性
|
|
|
+ length=myArray.length;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 遍历输出数组的方法
|
|
|
+ * @param array
|
|
|
+ */
|
|
|
+ public static void printArray(int[] array){
|
|
|
+ System.out.print("[\t");
|
|
|
+ for (int i = 0; i < array.length; i++) {
|
|
|
+ System.out.print(array[i]+"\t");
|
|
|
+ }
|
|
|
+ System.out.println("]");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取指定元素,根据索引位置获取指定的元素
|
|
|
+ * @param index
|
|
|
+ */
|
|
|
+ public static int get(int index){
|
|
|
+ return myArray[index];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ MyArray.add(15);
|
|
|
+ MyArray.add(66);
|
|
|
+ MyArray.add(77);
|
|
|
+ MyArray.add(88);
|
|
|
+ MyArray.add(10);
|
|
|
+ MyArray.add(2);
|
|
|
+ //printArray(MyArray.myArray);
|
|
|
+ System.out.println(Arrays.toString(MyArray.myArray));
|
|
|
+ //MyArray.removeLast();
|
|
|
+ //System.out.println(Arrays.toString(MyArray.myArray));
|
|
|
+ //MyArray.removeFirst();
|
|
|
+ //System.out.println(Arrays.toString(MyArray.myArray));
|
|
|
+ //MyArray.remove(5);
|
|
|
+ MyArray.add(9999,3);
|
|
|
+ System.out.println(Arrays.toString(MyArray.myArray));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|