MyArray.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package com.loveCoding.j20250607_method;
  2. import java.util.Arrays;
  3. /**
  4. * @author WanJl
  5. * @version 1.0
  6. * @title MyArray
  7. * @description
  8. * @create 2025/6/7
  9. */
  10. public class MyArray {
  11. static int length=0;
  12. static int[] myArray=new int[length];
  13. /**
  14. * 返回数组长度的方法
  15. */
  16. public static int size(){
  17. return length;
  18. }
  19. /**
  20. * 判断数组是否为空的方法
  21. * 数组为空,代表两个含义:
  22. * 1、数组长度为0
  23. * 2、数组为null
  24. * @return true就是为空,false就是不为空
  25. */
  26. public static boolean isEmpty(){
  27. return myArray.length==0||myArray==null||length==0?true:false;
  28. }
  29. /**
  30. * 判断数组不为空
  31. * @return true就是不为空,false就是为空
  32. */
  33. public static boolean isNotEmpty(){
  34. return !isEmpty();
  35. }
  36. /**
  37. * 该方法用来像myArray数组中添加一个元素,
  38. * 并且数组长度会随着添加元素的个数进行变化。
  39. * @param element
  40. */
  41. public static void add(int element){
  42. //1、建立一个临时数组,临时数组的长度是旧数组长度+1
  43. int[] tempArray=new int[length+1];
  44. //2、把原来的数组中的元素复制到新数组中
  45. for (int i = 0; i < myArray.length; i++) {
  46. tempArray[i]=myArray[i];
  47. }
  48. //3、把新元素添加到新数组中最后一位
  49. tempArray[tempArray.length-1]=element;
  50. //4、用新数组替换掉旧的数组,赋值给myArray
  51. myArray=tempArray;
  52. //5、把数组长度赋值给length属性
  53. length=myArray.length;
  54. }
  55. /**
  56. 编写一个方法,调用该方法,删除数组中最后一个元素
  57. */
  58. public static void removeLast(){
  59. //1、创建新的临时数组
  60. int[] tempArray=new int[length-1];
  61. //2、把原来的数组中的元素复制到新数组中
  62. for (int i = 0; i < tempArray.length; i++) {
  63. tempArray[i]=myArray[i];
  64. }
  65. //3、用新数组替换掉旧的数组,赋值给myArray
  66. myArray=tempArray;
  67. //4、把数组长度赋值给length属性
  68. length=myArray.length;
  69. }
  70. /**
  71. 编写一个方法,调用该方法,删除数组中第一个元素
  72. */
  73. public static void removeFirst(){
  74. //1、创建新的临时数组
  75. int[] tempArray=new int[length-1];
  76. //2、把原来的数组中的元素复制到新数组中
  77. for (int i = 0; i < tempArray.length; i++) {
  78. tempArray[i]=myArray[i+1];
  79. }
  80. //3、用新数组替换掉旧的数组,赋值给myArray
  81. myArray=tempArray;
  82. //4、把数组长度赋值给length属性
  83. length=myArray.length;
  84. }
  85. /**
  86. * 向指定位置添加元素
  87. * @param element 元素值
  88. * @param index 索引值
  89. */
  90. public static void add(int element,int index){
  91. //1、创建新的临时数组
  92. int[] tempArray=new int[length+1];
  93. //2、把原来的数组中的元素复制到新数组中
  94. for (int i = 0; i < myArray.length; i++) {
  95. if(i<index){
  96. tempArray[i]=myArray[i];
  97. }else{
  98. tempArray[i+1]=myArray[i];
  99. }
  100. }
  101. //在指定位置添加元素
  102. tempArray[index]=element;
  103. //3、用新数组替换掉旧的数组,赋值给myArray
  104. myArray=tempArray;
  105. //4、把数组长度赋值给length属性
  106. length=myArray.length;
  107. }
  108. /**
  109. * 根据索引值删除数组中指定元素
  110. * @param index
  111. */
  112. public static void remove(int index){
  113. //1、创建新的临时数组
  114. int[] tempArray=new int[length-1];
  115. //2、把原来的数组中的元素复制到新数组中
  116. for (int i = 0; i < tempArray.length; i++) {
  117. if (i<index){
  118. tempArray[i]=myArray[i];
  119. }else if (i>=index){
  120. tempArray[i]=myArray[i+1];
  121. }
  122. }
  123. //3、用新数组替换掉旧的数组,赋值给myArray
  124. myArray=tempArray;
  125. //4、把数组长度赋值给length属性
  126. length=myArray.length;
  127. }
  128. /**
  129. * 遍历输出数组的方法
  130. * @param array
  131. */
  132. public static void printArray(int[] array){
  133. System.out.print("[\t");
  134. for (int i = 0; i < array.length; i++) {
  135. System.out.print(array[i]+"\t");
  136. }
  137. System.out.println("]");
  138. }
  139. /**
  140. * 获取指定元素,根据索引位置获取指定的元素
  141. * @param index
  142. */
  143. public static int get(int index){
  144. return myArray[index];
  145. }
  146. public static void main(String[] args) {
  147. MyArray.add(15);
  148. MyArray.add(66);
  149. MyArray.add(77);
  150. MyArray.add(88);
  151. MyArray.add(10);
  152. MyArray.add(2);
  153. //printArray(MyArray.myArray);
  154. System.out.println(Arrays.toString(MyArray.myArray));
  155. //MyArray.removeLast();
  156. //System.out.println(Arrays.toString(MyArray.myArray));
  157. //MyArray.removeFirst();
  158. //System.out.println(Arrays.toString(MyArray.myArray));
  159. //MyArray.remove(5);
  160. MyArray.add(9999,3);
  161. System.out.println(Arrays.toString(MyArray.myArray));
  162. }
  163. }