目前我有一个这样构造的数组:
Car[] garage = new Car[5];
garage[0] = new Car("Ford", "Focus", "S301 ABN");
garage[1] = new Car("Opel", "Astra", "WA55 AAP");
garage[2] = new Car("Ford", "Explorer", "L66 4ABE");
garage[3] = new Car("Dodge", "Viper", "FA55 SAM");
garage[4] = new Car("Ferrari", "F50", "SAS3 47S");
但是,我需要由来自 JComboBox 的用户定义值设置数组的大小。我已经让 JComboBox 设置了值,但它抛出了一个,ArrayIndexOutOfBoundsException
因为存在不在数组索引范围内的元素。所以我的问题是,在用户选择数组的大小后,我应该如何将元素添加到数组中?