【发布时间】:2016-08-19 13:40:16
【问题描述】:
我在 android 中有一个这样的数组,Arrays.asList 的用法是不同的,因为我用不同的类“Person”制作了数组:
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 index= Arrays.asList(people).indexOf(new Person("Pascol"));
-
您需要在 Person 类中重写 equals() 和 hashcode() 并使用“pascol”而不是字符串来发送 Person 对象。
-
@Nambari 是的,我们都是对的 :)