2014-10-20 5 views
0

내 프로그램을 단순화하려고 시도했습니다. 다른 클래스의 추가 및 제거 방법을 테스트하고 내가 붙어있는 배열 목록의 속성 목록을 인쇄하기 위해이 테스터를 만들려고합니다.배열 및 인쇄에 항목을 계속 추가하는 방법

을 진행하는 방법 (어떤 이름의 ArrayList를 가지고 여기

public class Tester 
{ 
    public static void main (String[] args) 
    { 
    } 

    public Tester() 
    { 
     Apartment testingApartment = new Apartment(); 
     Basement testingBasement = new Basement(); 
     Company testingCompany = new Company();// Contains list of 
               // properties to sell 
               // in arraylist 

     // elements with a 2 after the name are used for the Basement 
     // elements 

     String Apartmentneighbourhood = "Parkdale"; 
     String Basementneighbourhood = "Rexdale"; 
     double price = 400000.99; 
     double price2 = 5000000.99; 
     int numberofbaths = 3; 
     int numberofbaths2 = 5; 
     int numberofbedrooms = 2; 
     int numberofbedrooms2 = 4; 
     int squarefoot = 3000; 
     int squarefoot2 = 5000; 
     int floors = 4; 
     int floorlevel = 4; 

     testingApartment.setneighbourhood(apartmentneighbourhood); 
     testingBasement.setneighbourhood(Basementneighbourhood); 
     testingApartment.setprice(price); 
     testingBasement.setprice(price2); 
     testingApartment.setbathNum(numberofbaths); 
     testingBasement.setbathNum(numberofbaths2); 
     testingApartment.setbedNum(numberofbedrooms); 
     testingBasement.setbedNum(numberofbedrooms2); 
     testingApartment.setsqrft(squarefoot); 
     testingBasement.setsqrft(squarefoot2); 
     testingApartment.setNumFloors(floors); 
     testingBasement.setFloorLevel(floorlevel); 
    } 
} 

이 아파트의 목록을 가정 내 아파트 클래스

public void addApartment(Apartment newApartment) 
    { 
     Apartment ApartmentInput = new Apartment(); 
     ApartmentInput = newApartment; 
    Arraylist.add(ApartmentInput); 
    } 
+0

코드가 아직 컴파일되지 않았다고 생각합니다. 주된 방법을보고있었습니다. 아직 완성되지 않은 것 같습니다. 편집 할 수 있습니까? – DreadHeadedDeveloper

+0

훨씬 좋았어, 고마워, 나에게 몇 분만 주렴. 내가 나중에 뭔가를 가지고있을거야. 지금 바빠. – DreadHeadedDeveloper

+0

편집 가능한 코드를 게시 할 수 있습니까? – m0skit0

답변

0

에서 내 추가 방법입니다 제발 도와주세요 권장하지 않음)

for(Apartment apartment in Arraylist) { 
    System.out.println(apartment.toString()); 
} 

시작됩니다. 아파트 클래스에 toString 메서드를 구현하여 해당 작업을 수행 할 수 있습니다.

또 다른 경고 - 방법

public void addApartment(Apartment newApartment) 
{ 
    Apartment ApartmentInput = new Apartment(); 
    ApartmentInput = newApartment; 
    Arraylist.add(ApartmentInput); 
} 

몇 가지 문제가 있습니다. 일반적인 Java 명명 규칙을 따르지 않습니다. 변수는 소문자로 시작해야합니다. 코드 블록 new Apartment()은 입력 매개 변수 newApartment의 값으로 ApartmentInput을 즉시 재 할당하므로 부적절합니다. 마찬가지로 newApartment의 값을 이미 가지고 있기 때문에 ApartmentInput 로컬 변수가 중복됩니다.

희망이 유용합니다. 내가 ArrayList를이 ArrayList 클래스의 인스턴스의 이름이라고 가정 한

하지 않을 경우, 다음 클래스 정의 어딘가에

ArrayList<Apartment> apartmentList = new ArrayList<>(); 

을 넣어.

0

귀하의 addApartment 방법은 Company 클래스에 있어야합니다 (귀하가 말한 클래스는 아파트의 속성 목록을 보유 할 것이며, ArrayList을 보유하는 클래스). 주석 처리 한 코드를 실행하려면 addApartment 메소드의 인수를 Apartment에서이 아파트를 "구성"하는 인수로 변경해야합니다.

testingCompany.addApartment(Apartmentneighbourhood, price, numberofbaths, numberofbedrooms, squarefoot, floornumbers); 

또는 당신은

Apartment newApartment = new Apartment(Apartmentneighbourhood, price, numberofbaths, numberofbedrooms, squarefoot, floornumbers); 
testingCompany.addApartment(newApartment); 

마지막으로, 당신의 addApartment 방법을 변경하려면 필요가 올바른 변수에 걸릴 및/또는 입력을 사용 할 수 있도록 주석 코드를 변경해야 Apartment 새로운 것을 만드는 대신에. 두 방법 모두에서, 당신은 당신의 Company 클래스 배열 목록의 인스턴스에 아파트를 추가

public void addApartment(Apartment newApartment) { 
    instanceOfArrayList.add(newApartment); 
} 

또는

public void addApartment(String nbhd, double price, int baths, int beds, int squareFt, int floors) { 
    Apartment toAdd = new Apartment(nbhd, price, baths, beds, squareFt, floors); 
    instanceOfArrayList.add(toAdd); 
} 

참고. 이 ArrayList은 클래스 생성자에서 초기화해야 원하는 경우 언제든지 추가 메소드를 호출 할 수 있습니다. add 메서드는 정적 메서드가 아니므로 ArrayList.add()을 호출하면 안됩니다 (잘못 입력하지 않는 한). 배열 목록의 인스턴스에서 추가 된 아파트를 추적하십시오.클래스는 전역 변수 ArrayList<Apartment> instanceOfArrayList;을해야하고, 생성자를 초기화해야합니다

마지막으로
instanceOfArrayList = new ArrayList<Apartment>(); 

는 아파트에 대한 모든 정보를 인쇄하려면, 당신이 할 수있는 모든 ArrayList의 요소 (instanceOfArrayList.size()), 및 인쇄를 통해 단지 루프 당신의 선택의 좋은 방법으로 각 Apartment의 정보를 밖으로.

관련 문제