使用计数器声明数组。每次将对象添加到数组中时都需要使用计数器。
// declare Parcel array and parcel count variable
private static Parcel[] parcels = new Parcel[10];
private static int parcelCount = 0;
扫描仪中的信息需要创建一个新的Parcel对象并将其存储在数组地块的下一个可用(空)点中。
继承人我得到了什么。它似乎没有起作用。我做错了什么?
System.out.print("Enter Parcel Number: ");
String parcelNumber = sc.nextLine();
String [] parcels = new String[0];
parcelCount ++;
System.out.print("Enter Sender Name: ");
String senderName = sc.nextLine();
String [] parcels = new String[1];
parcelCount ++;
System.out.print("Enter Return Address: ");
String returnAddress = sc.nextLine();
String [] parcels = new String[2];
parcelCount ++;
System.out.print("Enter Recipient Name: ");
String recipientName = sc.nextLine();
String [] parcels = new String[3];
parcelCount ++;
System.out.print("Enter Delivery Address: ");
String deliveryAddress = sc.nextLine();
String [] parcels = new String[4];
parcelCount ++;
答案 0 :(得分:0)
也许你需要https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html:)
或者使用类似的东西:
class ParcelHelper {
static long count = 0;
static Parcel[] parcels = new Parcel[10]
public static void addParcels(Parcel parcel) {
parcels[count] = parcel;
count ++;
}
}