我需要幫助創建一個ArrayList並向它們添加一些整數。從本質上講,我有一張紙牌遊戲,可以拉出卡片,然後保存ID(BCID)。我想將此拉ID添加到ArrayList,所以我可以避免兩次拉同一張卡。我有它設置如下,但我不斷得到重複。我知道這些卡拉動正確,因爲它顯示一切正常 - 只是重複卡,不幸的是。在Android中使用整數ArrayList
任何幫助你可以提供將是偉大的!我已盡力包括相關部分。如果您需要更多信息,請告訴我。
public static Integer bcid1, bcid2, bcid3, bcid4, bcid5, bcdcid;
public static List<Integer> usedCards = new ArrayList<Integer>();
在下面的例子中,假定檢測到重複然後初始化序列繪製不同的卡。
public static void setIDs()
{
try
{
bcid1 = Integer.parseInt(bc1);
usedCards.add(bcid1);
}
catch(NumberFormatException nfe)
{ }
try
{
bcid2 = Integer.parseInt(bc2);
if (usedCards.contains(bcid2))
{
try
{
blueCard2(ctx);
} catch (IOException e) {
e.printStackTrace();
}
}
else
{
usedCards.add(bcid2);
}
}
catch(NumberFormatException nfe)
{ }
try
{
bcid3 = Integer.parseInt(bc3);
if (usedCards.contains(bcid3))
{
try
{
blueCard3(ctx);
} catch (IOException e) {
e.printStackTrace();
}
}
else
{
usedCards.add(bcid3);
}
}
catch(NumberFormatException nfe)
{ }
try
{
bcid4 = Integer.parseInt(bc4);
if (usedCards.contains(bcid4))
{
try
{
blueCard4(ctx);
} catch (IOException e) {
e.printStackTrace();
}
}
else
{
usedCards.add(bcid4);
}
}
catch(NumberFormatException nfe)
{ }
try
{
bcid5 = Integer.parseInt(bc5);
if (usedCards.contains(bcid5))
{
try
{
blueCard5(ctx);
} catch (IOException e) {
e.printStackTrace();
}
}
else
{
usedCards.add(bcid5);
}
}
catch(NumberFormatException nfe)
{ }
}
我不遵循你的邏輯。哪裏有'bc1','bc2'等等? –