ArrayList에 저장 한 개체에서 항목을 검색하려고합니다. 코드를 살펴보십시오.개체를 ArrayList 밖으로 검색
MyList.add(new Car(year,make,available));
이제 myList (arraylist)에 새 자동차 객체를 저장했습니다. 예를 들면 어떻게 만들 수 있습니까?
감사합니다.
ArrayList에 저장 한 개체에서 항목을 검색하려고합니다. 코드를 살펴보십시오.개체를 ArrayList 밖으로 검색
MyList.add(new Car(year,make,available));
이제 myList (arraylist)에 새 자동차 객체를 저장했습니다. 예를 들면 어떻게 만들 수 있습니까?
감사합니다.
당신과 함께 목록을 반복 할 수
for (Car car : myList)
{
car.getMake(); //if you added getter, if you had not you can use addedCar.make;
}
아니면 그냥 항목의 인덱스를 사용하여 하나의 특정 항목에 액세스 할 수 있습니다 : 당신이 할 수 전에 언급 한 다른 답변으로
Car addedCar = myList.get(0); // 0 because you want the first item.
addedCar.getMake(); //if you added getter, if you had not you can use addedCar.make;
고맙습니다. Anthony Raymond. – user3460769
문제가 해결되면 게시물을 해결 된 것으로 표시하십시오 (대답의 왼쪽에있는 녹색 엄지 확인) –
내 변형 :
final Map<Integer, Car> myMap = new HashMap<>();
final Car newCar = new Car(year,make,available);
myMap.put(index, newCar); // where 'index' whatever you want that is unique for this collection year or brand or object ID
// get your object from collection
System.out.println(myMap.get(index)); // output is newCar
감사합니다 Lugaru. – user3460769
사용'MyList.get (인덱스) .make,' – Raghunandan
이 아니에요 당신이 수에 의해 (목록에서 특정 요소를 얻는 방법을 요구하는지 여부를 명확로? 다른 것?) 또는'Car' 참조가있을 때 make를 얻는 방법. –
Java 참조의 첫 번째 페이지를 여는 것보다 SO에 대해 사람들에게 묻는 것이 정말 빠릅니까? – EvilDuck