为什么我的阵列列表第二次打印不正确?

时间:2016-11-27 00:56:58

标签: java android arraylist

我在这里找不到有关Array List属性的内容吗?

我在列表中添加了自定义对象列表,每个对象都有一个唯一的名称。

当我第二次访问列表时,列表中的每个对象都是相同的。我测试时,访问之间没有代码。

   // 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 Item Class中,Restaurant变量为 static

1 个答案:

答案 0 :(得分:2)

我假设在TastrItem类中,您将项目保存为静态变量。因此,尽管在每个循环中正确创建了新的TastrItem,但静态变量始终设置为最后一个值。要解决此问题,只需编辑TastrItem类,然后删除静态关键字。