2017-10-24 11 views
-2

配列はカウンタで宣言されています。オブジェクトがアレイに追加されるたびにカウンタを使用する必要があります。Javaで新しい配列オブジェクトを作成するにはどうすればよいですか?

// declare Parcel array and parcel count variable 
    private static Parcel[] parcels = new Parcel[10]; 
    private static int parcelCount = 0; 

スキャナ内の情報が新しいパーセルオブジェクトを作成し、アレイパーセル内の次に使用可能な(空の)場所に格納する必要があります。

私は何を持っていますか?それは動作していないようです。私は間違って何をしていますか?

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 ++; 
+1

あなたは、配列がどのように機能するかについてのチュートリアルを読んで考えがあります/tutorial/java/nutsandbolts/arrays.html –

+0

...新しいオブジェクトを作成するコードを見せているうちに、その方法を教えてほしいですか? – Stultuske

+1

なぜ '小包 'を作り直しているのですか?あなたはちょうど最後にそれを作成してみませんか?小包数にはどのように名前と住所が関係していますか? – Thomas

答えて

1

たぶん、あなたは:)

https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.htmlを必要とするかのようなものを使用します。https://docs.oracle.com/javase:

class ParcelHelper { 
    static long count = 0; 
    static Parcel[] parcels = new Parcel[10] 

    public static void addParcels(Parcel parcel) { 
     parcels[count] = parcel; 
     count ++; 
    } 
} 
関連する問題