請問各位
我下面的程式碼為什麼每次執行SearchStudent(array,num)
回傳的Loop的值都不會改變
一值都是第一次執行尋找到的在ArrayList裡面的位置座標??
ArrayList<StudentInfo> array = new ArrayList<StudentInfo>();
Scanner in = new Scanner(System.in);
StudentInfo S1=new StudentInfo("AAA", "1000");
StudentInfo S2=new StudentInfo("BBB", "2000");
StudentInfo S3=new StudentInfo("CCC", "3000");
StudentInfo S4=new StudentInfo("DDD", "4000");
StudentInfo S5=new StudentInfo("EEE", "5000");
array.add(S1);
array.add(S2);
array.add(S3);
array.add(S4);
array.add(S5);
String num="2000";
System.out.println(SearchStudent(array,num));
System.out.println(array.size());
String num1="4000";
System.out.println(SearchStudent(array,num));
System.out.println(array.size());
String num2="5000";
System.out.println(SearchStudent(array,num));
System.out.println(array.size());
public static int SearchStudent(ArrayList<StudentInfo> array, String number)
//尋找學生位置在ArrayList
{
int loop=1;
for (int x = 0; x < array.size(); x++)
{
if (array.get(x).getNumber().equals(number))
{
System.out.println(array.get(x).getNumber());
System.out.println("loop "+loop);
loop=1;
break;
}
loop++;
}
return loop;
}
}