[英]Java: Paint application, the code doesn't work, what am I doing wrong?
[英]Official Java excercize solution doesn't work… what I am doing wrong?
atm 我正在做一個 Java 練習,這是關於將 integer 與 ArrayList 的所有元素進行比較。 根據書本的解決方案應該是與“int index = ArrayList.indexOf(comparisonValue);”的比較和“索引> = 0”。 但對我來說,它不起作用。 我還讀到“indexof”只檢查 ArrayList 的第一個 position。 但是,如果這是真的,為什么在我的書中將其標記為正確的解決方案?
也許我有一個錯誤。 這是我的代碼,謝謝:)
ArrayList<Integer> tiplist = new ArrayList<Integer>(20);
int des = 0;
int limit = 20;
int tipc = 0;
int index = tiplist.indexOf(tip);
//program loop:
while (des == 0 && tipc < limit) {
tip = 6; //I made it easy to read, normally the number is generated
tipc++; //tip count
if (tipc > 1) { //if it is not the first tip
那是行不通的:
if (index >= 0) {
System.out.println("Nothing new");
}
但是那個確實,雖然我讀到“包含”也使用“indexof”:
if (tiplist.contains(tip) == true) {
System.out.println("Nothing new");
}
最后幾行...
}
tiplist.add(tip);
// -->do something
}
您需要使用int index = tiplist.indexOf(tip);
就在tip = 6; //I made it easy to read, normally the number is generated
tip = 6; //I made it easy to read, normally the number is generated
在您的情況下,值在計算/生成之前被分配給index
,因此可能使用您之前設置的默認值。
在您知道提示是什么並填寫列表之前,您無法在提示列表中找到提示的索引。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.