2014-04-25 2 views
0

나는 얼마 동안 배열을 가지고 놀았으며이 문제는 나를 괴롭혔다.배열의 객체 초기화

나는 사용자 정의 된 객체를 생성하고 다음과 같은 배열로 선언했다 :`Property regesteredAssets [] = new Property [200];

그리고 여기 내 생성자의`

public Property(String newPropertyName,String newPropertyAddress,String newPropertyType, String newPropertyDescription) 
    { 

    propertyName[arraySequence] = newPropertyName; 
    propertyFullAddress[arraySequence] = newPropertyAddress; 
    propertyType[arraySequence] = newPropertyType; 
    propertyDescription[arraySequence] = newPropertyDescription; 

     arraySequence++; 



} 

내가 내 욕망에 따라 각 배열 regesteredAsssets[]을 초기화 할 수 있습니다. 내가 어떻게 해? Property 클래스의 속성에도 배열을 사용해야합니까?

+0

의미는 있지만 유형을 선언하지 않는 것이 좋습니다. 'Property [] registeredAssets;와는 반대로,''PropertyAssets [];' '와는 반대이다. 배열 및 단일 객체를 선 아래로 쉽게 배치 할 수 있습니다. – indivisible

답변

0

특정 애셋에 여러 항목이없는 경우가 아니면 속성이 배열이 아니어도됩니다. 이 경우, 나는 그렇게 생각하지 않습니다. 다음과 같이 코드를 크게 단순화 할 수 있습니다.

public class Property { 
    private String name, address, type, description; 

    public Property(String name, String address, String type, String description) { 
     this.name = name; 
     this.address = address; 
     this.type = type; 
     this.description = description; 
    } 

    public static void main(String[] args) { 
     Property[] registeredAssets = new Property[200]; 

     registeredAssets[0] = new Property("Joe Bloggs", "555 Fake St.", "IMPORTANT", "Lorem Ipsum Dolor"); 
     // etc. 
    } 
} 
+0

많은 도움이됩니다! 고맙습니다! – user3519322

0

당신이 유형 속성의 배열이있는 경우, 다음과 같은 코드를 사용하여 요소까지 각각 설정할 수 있습니다

regesteredAssets[0] = new Property(enterYourParametersHere); 

나는 당신의 속성 생성자의 필드를 가정 한 필드는을, 따라서 당신은하지 않습니다 배열 표기법 field[index] = value을 사용하여 설정해야하며 실제로 Property 클래스가 일관성이 있다고 생각하면 컴파일 오류가 발생합니다.

for(int i = 0; i < 10; i++) 
{ 
    regesteredAssets[i] = new Property(enterYourParametersHere); 
} 

난이 도움이되기를 바랍니다 : 당신이 당신의 배열에 여러 항목을 설정하고자한다면

, 당신은 다음과 같이 배열의 인덱스에 루프 인덱스를 제공하는 루프 내부의 초기화 단계를 수행 할 수 ...

+0

감사! 그래서 매번 새로운 regesteredAsset [i]에 새로운 인수를 전달합니다. 그 논쟁은 그 특정한 물건에 할당됩니까? – user3519322

+0

예, 인수는 생성자를 통해 제공되어야하며'regesteredAsset [i]'가 히트 될 때마다 배열의 인덱스'i'에있는 Property 인스턴스는 생성자 코드에 따라 이러한 인수를 갖습니다. –