특정 gpa에 대해 ExArrayList
을 검색하고 싶습니까? ArrayList에 있으면 true 또는 false의 대답을 원합니다.항목에 대한 ArrayList를 검색하려면 어떻게해야합니까?
public class ExArrayList {
private String Name;
private double GPA;
public ExArrayList(String name, double gpa) {
this.Name = name;
this.GPA = gpa;
}
public void setGPA(double gpa) {
this.GPA = gpa;
}
public double getGPA() {
return GPA;
}
public void setName(String name) {
this.Name = name;
}
public String getName() {
return Name;
}
@Override
public String toString() {
return String.format("%s\t%f", this.Name, this.GPA);
}
}
public class Main {
public static void main(String[] args) {
ArrayList<ExArrayList> psy101 = new ArrayList<>();
psy101.add(new ExArrayList("Bob", 2.9));
psy101.add(new ExArrayList("Steve", 3.9));
psy101.add(new ExArrayList("Charles", 4.0));
System.out.println();
System.out.printf("Student\tGPA\n");
for(ExArrayList s : psy101) {
System.out.printf("%s\n", s);
}
boolean binFound = psy101.contains(2.9); // This is what I am using to search the ArrayList. It's not working.
System.out.println("Does the list contain GPA of 2.9? " + binFound);`
정렬하고 덮어 쓸 필요가 –
이진 검색 ['메소드의 개요 boolean equals (Object o)를'] (https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html# equals-java.lang.Object-)뿐만 아니라 ['int hashCode()'] (https://docs.oracle.com/javase/8/docs/api/java/lang/Object.html#hashCode--) [ArrayList]에서 [''int indexOf (Object o)'를 사용하려면 (http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html#indexOf-java .lang.Object-). – Turing85