1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- package com.lc.day10.dt04;
- import java.util.Arrays;
- import static java.lang.System.*;
- /**
- * ClassName: Worker
- *
- * @Author 爱扣钉-陈晨
- * @Create 2023/12/24 11:45
- * @Version 1.0
- */
- public class Worker {
- //水果集合
- static Fruit[] fruits = new Fruit[10];
- //索引
- static int count = 0;
- public static void plantingFruit( Fruit f ){
- fruits[count++] = f;
- }
- public static Fruit plantingFruit( String name ){
- int index = getFruitName(name);
- Fruit fruit = fruits[index];
- //删除
- // [1,2,3,4] a 原数组 arraycopy a , 0 , a ,
- // [0,1,0,0] 新数组
- arraycopy(fruits, index+1, fruits, index, count-index);
- //位置置为空
- fruits[count] = null;
- count--;
- //返回
- return fruit;
- }
- public static int getFruitName(String name){
- for (int i = 0; i < count; i++) {
- if (fruits[i].name.equals(name)){
- return i;
- }
- }
- return -1;
- }
- public static void main(String[] args) {
- int[] arr1 = {1,2,3,4}; // {1,3,4}
- System.arraycopy(arr1,3, arr1, 2, 1);
- System.out.println(Arrays.toString(arr1));
- }
- }
|