+ 1

Iterator.next() возвращает одно и тоже значение

Список cards состоит из разных значений, но iter.next() возвращает значение из конца списка. Пробовали с помощью cards.get(n), но проблема та же. public void update(List<Card> cards, String[] players, short[] playersCardsCount, boolean isPortal) {  String resOut = " Карты Игроки\n";  short cardsSize = (short) cards.size();  short playersSize = (short) players.length;  short maxSize;  if (cardsSize < playersSize)   maxSize = playersSize;  else   maxSize = cardsSize;  Iterator<Card> iter = cards.iterator();  for (short n = 0; n < maxSize; n++)   if (cardsSize > n && playersSize > n) {    Card card = iter.next();    resOut += makeCard(n, card.color, card.numberNormal, isPortal) + spaceCardPlayer + makePlayer(n, players[n], playersCardsCount[n]) + "\n";   } else if (cardsSize > n) {    Card card = iter.next();    resOut += makeCard(n, card.color, card.numberNormal, isPortal) + "\n";   } else    resOut += spaceVoidPlayer + makePlayer(n, players[n], playersCardsCount[n]) + "\n";  clear(resOut); }

30th Mar 2022, 4:09 PM
Operand
Operand - avatar
4 Antworten
+ 2
nice error you store Card objects to cards list but all fields in Card are static !! so when you create next Card, it changes values for all Card objects to same, because static means: it will be common and shared so delete static keyword in Card fields declarations class Card { public static short color; public static int number; public static String numberNormal;
31st Mar 2022, 1:47 AM
zemiak
+ 2
//Operand iterator is working fine. May be list data or other function may causing problem. check list size. //I cant check full link //sample test.. import java.util.Iterator; import java.util.List; import java.util.Scanner; class Main{ public static void update(List<Object> cards, String players, short playersCardsCount, boolean isPortal) { String resOut = " Карты Игроки\n"; short cardsSize = (short) cards.size(); short playersSize = (short) players.length(); short maxSize; if (cardsSize < playersSize) maxSize = playersSize; else maxSize = cardsSize; Iterator iter = cards.iterator(); System.out.println(maxSize); for (short n = 0; n < maxSize; n++) { if (cardsSize > n && playersSize > n) { Object card = iter.next(); resOut += card + players + ":\n"; } else if (cardsSize > n) { Object card = iter.next(); resOut += card + "::\n"; } else resOut += players + "\n"; } System.out.println(resOut); } public static void main(String a[]){ update(List.of(1,2,3,4),"array",(short)5,true); } }
30th Mar 2022, 5:49 PM
Jayakrishna 🇮🇳
0
какие там еще значения? Я думаю, лучше опубликовать полный код, если это возможно. what is other values there? better to post full code if posible, i think.
30th Mar 2022, 4:25 PM
Jayakrishna 🇮🇳