gpt4 book ai didi

java - 为什么我的数组列表第二次打印不正确?

转载 作者:行者123 更新时间:2023-12-02 03:12:35 24 4
gpt4 key购买 nike

我在这里缺少有关数组列表属性的信息吗?

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

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

   // 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

编辑:答案是 Restaurant 变量在 Tastr 项类中是静态

最佳答案

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

关于java - 为什么我的数组列表第二次打印不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40824763/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com