import java.util.ArrayList;import java.util.LinkedList;import java.util.List;import java.util.Random;public class Table { //private LinkedList<Food> foods = new LinkedList<>(); String[] foods=new String[3]; private String[] foodNames = new String[] { "麻婆豆腐", "干煸四季豆", "折耳根炒腊肉", "小白菜炒肉" }; private final static int MAX_FOOD_COUNT = 3; Random random = new Random(); public void producer() throws InterruptedException { synchronized (foods) { if (foods.length == MAX_FOOD_COUNT) { foods.wait(); } int position = random.nextInt(foodNames.length); String foodName = foodNames[position]; Food food = new Food(foodName); foods=foodNames; System.out.println("厨师炒了一盘" + foodName); foods.notifyAll(); } synchronized (foods) { } } public void Consumer() throws InterruptedException { synchronized (foods) { if (foods.length==0) { foods.wait(); } Food food=foods.remove(0); System.out.println("服务员端走了" +food.toString()); foods.notifyAll(); } }}
Java 数组怎么删除元素
第1个回答 2018-04-16
从要删除的数组元素下标开始用后一个元素去替代前一个元素然后数组下标-1
第2个回答 2018-04-16
数组一经创建不可改变,不可增加或减少长度,如果需要动态可变长的数组请用ArrayList类。本回答被网友采纳