에 대한 Doctor d = doctorMap.get("firstname")
에있는이 솔루션은 아니지만,에서 하나를 도출 사용할 수 있습니다 뭔가
콘솔의 출력 내용을 밝히지 않았기 때문에 kn을 할 수 없습니다. 특정 오류가있는 이유. 항목의 값을 되 Doctor
대상으로 그런 일이없는 것처럼 컴파일러는 역할을한다는
public class StartingPoint {
static String[] doctorNames = {"Potato", "Chocolate", "Something", "Name", "Unnamed"};
public static void main(String[] args) {
ArrayList<Doctor> doctorList = new ArrayList<>(5);
for (int i = 0; i < 5; i++) {
doctorList.add(new Doctor(doctorNames[i], doctorNames[i] + " last name", String.valueOf(i)));
}
HashMap<String, Doctor> someHashMap = getDoctorHash(doctorList);
for (int i = 0; i < 5; i++) {
System.out.println("The ID of the doctor number " + String.valueOf(i + 1) + " is: ");
System.out.println(someHashMap.get(doctorNames[i]).getId());
}
}
public static HashMap<String, Doctor> getDoctorHash(ArrayList<Doctor> doctorList) {
HashMap<String, Doctor> hm = new HashMap<>();
for(Doctor doctor : doctorList) {
System.out.println(doctor);
hm.put(doctor.getId(), doctor);
}
return hm;
}
}
그것은 밝혀 :
그럼에도 불구하고, 나는 나 자신에게 아이디어를 제공하기위한 다음 코드를 생성 ID (키 역할을 함). 그럼에도 불구하고, 함수에 전달 된 ArrayList
의 항목 중 Doctor
항목 각각의 메모리에있는 위치를 인쇄하려고하면 아무런 문제가 없음을 알 수 있습니다. 나는 이것의 이유가 무엇인지에 대해 조금이라도 생각하지 못했습니다.
하지만, 대신 우리는 그 방법 중 하나를 이용하여 얻을 수있는 String
들 중 하나를 사용하는 값으로 Doctor
객체를 사용하는 모든 것이 잘 밝혀지면 : doctorList에
public class StartingPoint {
static String[] doctorNames = {"Potato", "Chocolate", "Something", "Name", "Unnamed"};
public static void main(String[] args) {
ArrayList<Doctor> doctorList = new ArrayList<>(5);
for (int i = 0; i < 5; i++) {
doctorList.add(new Doctor(doctorNames[i], doctorNames[i] + " last name", String.valueOf(i)));
}
HashMap<String, String> someHashMap = getDoctorHash(doctorList);
for (int i = 0; i < 5; i++) {
System.out.println("The first name of the doctor number " + String.valueOf(i + 1) + " is: ");
System.out.println(someHashMap.get(String.valueOf(i)));
}
}
public static HashMap<String, String> getDoctorHash(ArrayList<Doctor> doctorList) {
HashMap<String, String> hm = new HashMap<>();
for(Doctor doctor : doctorList) {
hm.put(doctor.getId(), doctor.getFirstName());
}
return hm;
}
}
변경에는 EmployeeList을 and Doctor.size() to doctorList.size() – developer
['HashMap'] (https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html)은 매핑을 정의하고, 예 당신의 예제에서 당신은'String'으로 질의를하고'Doctor'를 얻을 것입니다. 따라서 [put (K key, V value)'] (https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html#put-KV-)에는 두 개의 매개 변수가 필요합니다 . 어쩌면 ['HashSet'] (https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html)을 사용하고 싶습니까? – Turing85
열쇠는 무엇이되어야합니까? Map의 포인트는 값을'Key'와 연결하는 것입니다.이 경우'String'으로 지정했습니다. 그래서 특정'Doctor' (일명'put ("Bob",/* some doctor * /))와 연관되는'String' 값이 필요합니다.'get ("Bob")' – Rogue