Worker.java 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package com.lc.day10.dt04;
  2. import java.util.Arrays;
  3. import static java.lang.System.*;
  4. /**
  5. * ClassName: Worker
  6. *
  7. * @Author 爱扣钉-陈晨
  8. * @Create 2023/12/24 11:45
  9. * @Version 1.0
  10. */
  11. public class Worker {
  12. //水果集合
  13. static Fruit[] fruits = new Fruit[10];
  14. //索引
  15. static int count = 0;
  16. public static void plantingFruit( Fruit f ){
  17. fruits[count++] = f;
  18. }
  19. public static Fruit plantingFruit( String name ){
  20. int index = getFruitName(name);
  21. Fruit fruit = fruits[index];
  22. //删除
  23. // [1,2,3,4] a 原数组 arraycopy a , 0 , a ,
  24. // [0,1,0,0] 新数组
  25. arraycopy(fruits, index+1, fruits, index, count-index);
  26. //位置置为空
  27. fruits[count] = null;
  28. count--;
  29. //返回
  30. return fruit;
  31. }
  32. public static int getFruitName(String name){
  33. for (int i = 0; i < count; i++) {
  34. if (fruits[i].name.equals(name)){
  35. return i;
  36. }
  37. }
  38. return -1;
  39. }
  40. public static void main(String[] args) {
  41. int[] arr1 = {1,2,3,4}; // {1,3,4}
  42. System.arraycopy(arr1,3, arr1, 2, 1);
  43. System.out.println(Arrays.toString(arr1));
  44. }
  45. }