2016-11-27 1 views
0

ここでは見つからないArray Listのプロパティについて何かありますか?2回目のアレイリストの印刷が正しくないのはなぜですか?

カスタムオブジェクトのリストをリストに追加しています。リストにはそれぞれ固有の名前が付けられています。

リストに2度アクセスすると、リスト内のすべてのオブジェクトが同じになります。私はこれをテストしたときにアクセス間にコードはありません。

// Add all the Tastr Items to the Array list using the list of restaurants found by yelp. 
    for (int i = 0; i < yelp.getRestaurants().size(); i++){ 
     TastrItem temp = new TastrItem(); 
     temp.setRestaurant(yelp.getRestaurants().get(i)); 
     itemList.add(temp); 
     System.out.println("First Time Accessing the list --- > " + itemList.get(i).getRestaurant()); 
    } 

    for (int i = 0; i < yelp.getRestaurants().size(); i++) { 
     System.err.println("Second time Accessing the list --- > " + itemList.get(i).getRestaurant()); 
    } 

出力:

I/System.out: First Time Accessing the list --- > Stanton's City Bites 
I/System.out: First Time Accessing the list --- > Aladdin Mediterranean Cuisine 
I/System.out: First Time Accessing the list --- > The Breakfast Klub 
I/System.out: First Time Accessing the list --- > M&M Grill 
I/System.out: First Time Accessing the list --- > Big & Juicy Juice Bar 
I/System.out: First Time Accessing the list --- > Eddie V's Prime Seafood 
I/System.out: First Time Accessing the list --- > Baby Barnaby's Café 
I/System.out: First Time Accessing the list --- > Pappas Bros Steakhouse 
I/System.out: First Time Accessing the list --- > Just Dinner 
I/System.out: First Time Accessing the list --- > Green Seed Vegan 
I/System.out: First Time Accessing the list --- > Local Foods 
I/System.out: First Time Accessing the list --- > Salad Extraveganza 
I/System.out: First Time Accessing the list --- > Luigi's Pizzeria 
I/System.out: First Time Accessing the list --- > Houston's Restaurant 
I/System.out: First Time Accessing the list --- > Hugo's 
I/System.out: First Time Accessing the list --- > Huynh Restaurant 
I/System.out: First Time Accessing the list --- > MKT BAR 
I/System.out: First Time Accessing the list --- > Reef 
I/System.out: First Time Accessing the list --- > Mockingbird Bistro 
I/System.out: First Time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 
W/System.err: Second time Accessing the list --- > Pho Saigon Vietnamese 

編集:答えはレストラン変数がTastrのアイテム・クラスの静的だったということです。

+1

'yelp.getRestaurants()。get(i)'とは何ですか? 'List'インターフェースやカスタムメソッドの' get() 'ですか? – Emmanuel

+0

ArrayListからのgetメソッド。それは大きさを返します、IEのレストランの量が見つかりました。 – Remixt

+2

'TastrItem'のコードを投稿する必要があります。私はそこに静的変数があると思う。 – lionscribe

答えて

2

TastrItemクラスでは、アイテムを静的変数に保存しているとします。したがって、各ループに新しいTastrItemを正しく作成しても、静的変数は常に最後の値に設定されます。これを解決するには、TastrItemクラスを編集し、staticキーワードを削除してください。

関連する問題