MyArray.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package j1_java_basic.J20250608;
  2. import java.util.Arrays;
  3. public class MyArray {
  4. //数组长度,每次操作(增加元素、删除元素)都要变动数组长度
  5. static int length = 0;
  6. //创建一个数组,每次操作的都是这个数组
  7. static int[] arr = new int[length];
  8. /**
  9. * 添加元素的方法,每调用一次该方法,传入参数,就会向数组中添加一个元素。
  10. *
  11. * @param e
  12. */
  13. public static void add(int e) {
  14. //1、创建一个临时的数组,长度是原数组长度+1
  15. int[] tempArray = new int[length + 1];
  16. //2、把原数组中的元素赋值到新数组中
  17. for (int i = 0; i < arr.length; i++) {
  18. tempArray[i] = arr[i];
  19. }
  20. //3、把要添加的元素e赋值到新数组的最后
  21. tempArray[tempArray.length - 1] = e;
  22. //4、用新数组替换掉旧的数组
  23. arr = tempArray;
  24. //5、修改长度变量length
  25. length = arr.length;
  26. }
  27. /**
  28. * 删除最后1个元素的方法
  29. */
  30. public static void removeLast() {
  31. //1、创建一个临时的数组,长度是原数组长度-1
  32. int[] tempArray = new int[length - 1];
  33. //2、把原数组中的元素赋值到新数组中
  34. for (int i = 0; i < tempArray.length; i++) {
  35. tempArray[i] = arr[i];
  36. }
  37. //3、用新数组替换掉旧的数组
  38. arr = tempArray;
  39. //4、修改长度变量length
  40. length = arr.length;
  41. }
  42. /**
  43. * 删除第1个元素的方法
  44. */
  45. public static void removeFirst() {
  46. //1、创建一个临时的数组,长度是原数组长度-1
  47. int[] tempArray = new int[length - 1];
  48. //2、把原数组中的元素赋值到新数组中
  49. for (int i = 0; i < tempArray.length; i++) {
  50. //原数组跳过第一个元素,后面的依次赋值给新数组
  51. tempArray[i] = arr[i + 1];
  52. }
  53. //3、用新数组替换掉旧的数组
  54. arr = tempArray;
  55. //4、修改长度变量length
  56. length = arr.length;
  57. }
  58. /**
  59. * 删除指定位置元素的方法
  60. *
  61. * @param index 索引值
  62. */
  63. public static void remove(int index) {
  64. //1、创建一个临时的数组,长度是原数组长度-1
  65. int[] tempArray = new int[length - 1];
  66. //2、把原数组中的元素赋值到新数组中
  67. for (int i = 0; i < tempArray.length; i++) {
  68. if(i<index){
  69. tempArray[i] = arr[i];
  70. }else {
  71. //原数组跳过第一个元素,后面的依次赋值给新数组
  72. tempArray[i] = arr[i + 1];
  73. }
  74. }
  75. //3、用新数组替换掉旧的数组
  76. arr = tempArray;
  77. //4、修改长度变量length
  78. length = arr.length;
  79. }
  80. /**
  81. * 快速输出数组元素到控制台的方法
  82. */
  83. public static void printArray() {
  84. System.out.println(Arrays.toString(arr));
  85. }
  86. /**
  87. * 返回数组的长度
  88. * @return 数组长度
  89. */
  90. public static int size(){
  91. return length;
  92. }
  93. /**
  94. * 判断数组是否为空
  95. * @return true(空) | false(不为空)
  96. */
  97. public static boolean isEmpty(){
  98. if (arr==null||length==0){
  99. return true;
  100. }
  101. return false;
  102. }
  103. /**
  104. * 判断数组是否不为空
  105. * @return
  106. */
  107. public static boolean isNotEmpty(){
  108. return !isEmpty();
  109. }
  110. /**
  111. * 根据索引值查找数组元素,如果数组中没有这个元素就返回-1
  112. * @param index 索引值
  113. * @return 数组元素
  114. */
  115. public static int getElement(int index){
  116. if(length<index) {
  117. return arr[index];
  118. }
  119. return -1;
  120. }
  121. /**
  122. * 查找数组中是否包含指定元素,包含则返回索引值,不包含则返回-1
  123. * @param element 要查找的元素
  124. * @return 索引值
  125. */
  126. public static int getIndex(int element){
  127. for (int i = 0; i < arr.length; i++) {
  128. if (arr[i]==element){
  129. return i;
  130. }
  131. }
  132. return -1;
  133. }
  134. }