我有一个简单的CRUD操作任务,我被卡住了。我们不应该在这个中使用ArrayList。 该程序应允许用户将新车添加到阵列中,并且信息应包含车辆ID,品牌,发布年份和价格。
到目前为止,我不确定以下内容:
如何将包含用户输入的所有信息的新项目添加到现有数组中?
我不幸地指出代码有什么问题,因为它根本不会执行。
import java.util.Scanner;
import java.util.Arrays;
public class Car_main {
public static void main(String[] args) {
// TODO Auto-generated method stub
int cont = 0;
String cBrand, cId, cYear, searchBrand, searchYear, removeId, updateId;
String cPrice;
Car carArr[] = new Car[0];
int counter = 0;
int choice;
Scanner scanObj = new Scanner(System.in);
while (cont == 0) {
System.out.println("Fill in: \n1 Add a new car\n2 "
+ "if you want to change car ID\n3 "
+ "if you want to remove car ID \n4 "
+ "if you want to search by the brand\n5 "
+ "if you want to search by the year of release \n6 "
+ "calculate the total sum \n7"
+ "show the list");
choice = scanObj.nextInt();
switch (choice) {
case 1:
System.out.println("Your choice is 1. Add a new car. Fill in car id, car brand, release year and price);");
cId= scanObj.next();
cBrand = scanObj.next();
cYear = scanObj.next();
cPrice = scanObj.next();
Car c_obj = new Car (cId, cBrand, cYear, cPrice);
counter++;
谢谢!
答案 0 :(得分:1)
Java中的数组具有固定的大小。因此,您必须创建一个具有足够大小的数组,或者每次添加新元素并将现有值复制到新数组时都必须实例化一个新数组。