123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- 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));
- }
- }
|