我喜歡這個在android系統的arrray Arrays.asList的使用是不同的,因爲我做了不同的類「人」的陣列從一個數組得到一個元素的索引:如何使用Arrays.asList
people = new Person[]{
new Person("Python"),
new Person("PHP"),
new Person("Pascol"),
new Person("PyCrust"),
new Person("C"),
new Person("C#"),
new Person("C++")
};
,我用Arrays.asList這樣
int index= Arrays.asList(people).indexOf("Pascol");
String tags = Integer.toString(index);
Toast.makeText(getApplication(),tags,Toast.LENGTH_SHORT).show();
,但我得到的值「-1」的祝酒辭。 我無法找到故障。
INT指數= Arrays.asList(人).indexOf(新的人( 「Pascol」)); –
您需要重寫Person類中的equals()和hashcode(),並使用「pascol」而不是僅字符串發送Person對象。 – kosa
[indexOf()可能重複將找不到自定義對象類型](http://stackoverflow.com/questions/24957813/indexof-will-not-find-a-custom-object-type) – kosa