2014-11-28 2 views
2

내 배열을 처리하는 클래스 (ListDePersonnes)를 작성하고 싶습니다. 현재 개체를 추가하는 방법과 배열 내용을 화면에 인쇄하는 방법 두 가지만 있습니다.개체 배열에 멤버 추가

ListDePersonnes

import java.util.Scanner; 

public class ListDePersonnes { 
    int count,t; 
    personne liste[]; 
    Scanner s=new Scanner(System.in); 

    public void initialize() {     // 
     System.out.println("entrer la taille"); // 
     t = s.nextInt();      // I'm not so sure about this part. 
     personne[] liste = new personne[t];  // 

    } 


    public void addpersonne(){ 

     System.out.println("what is the name?"); 
     String nom= s.next(); 
     System.out.println("what is the age?"); 
     int age= s.nextInt(); 
     System.out.println("what is the weight?"); 
     double poid= s.nextDouble(); 

     liste[count] = new personne(nom,age,poid); // weight is poid in frensh 
     count++; 
    } 



    public void showAll(){ 
     for (int i=0;i<t;i++){ 
      System.out.println("name: "+ liste[i].getNom() + "/age: "+liste[i].getAge()+"/poid:  "+liste[i].getPoid()); 
     } 
    } 
} 

personne

public class personne { 

    private String nom; 
    private int age; 
    private double poid; 

    public personne(String nom, int age, double poid) { 

     this.nom = nom; 
     this.age = age; 
     this.poid = poid; 
    } 

} 

Main

import java.util.Scanner; 
public class LaListe { 

    public static void main(String[] args) { 
     Scanner s=new Scanner(System.in); 
     ListDePersonnes lper= new ListDePersonnes(); 
     lper.initialize(); 
     lper.addpersonne(); 
    } 

} 
,174,

발생 오류 : LaListe.main (LaListe.java:16)에서 ListDePersonnes.addpersonne (ListDePersonnes.java:29) 에서 스레드에서

예외 "주"java.lang.NullPointerException이

+3

'personne [] liste = new personne [t];'. 클래스 필드 변수를 섀도 잉하고 있습니다. 그것은'liste = new personne [t];이어야합니다. –

+0

많은 남자, 삭제하는 방법에 대한 아이디어에 고마워요? –

+1

나에게 쉬운 걸. 나는 이것에 처음이다 : 웃음 그래서 무서운되고 ... 그냥 코드를 포맷하고 어떤 노력을 보여, 사람들은 downvote 또는 닫지 않을 것입니다 – mprabhat

답변

4

당신은 인 shadowing입니다.

liste = new Personne[t]; 
+0

많은 사람, 배열에서 personne을 삭제하는 방법에 어떤 아이디어를 주셔서 감사합니다? –

+1

컬렉션에서 개체를 삭제해야하는 경우 '목록'을 사용할 수 있습니다 (예 : 'ArrayList' – Reimeus

+0

내가 간단한 배열로했던 것과 똑같은 것을 선언합니까? –

2

자바 더 쉽게 작업을 할 수있는 클래스 목록 (docs)을 포함하여

Personne[] liste = new Personne[t]; 

교체합니다. (here에서)

사용 예제 :

는 목록에 항목을 추가하려면 : 액세스하려면

listA.remove(personee1); //Remove by object 
listA.remove(0); //Remove by index 

/:

List<Personee> listA = new ArrayList<Peronsee>(); //New ArrayList 
listA.add(new Personee("Bob", 29, 165)); //Add element 
listA.add(new Personee("Alice", 25, 124)); 
listA.add(0, new Personee("Eve", 34, 136)); //This adds an element to the beginning of the list, index 0. 

이 요소를 제거하려면 목록을 반복합니다.

//access via index 
Peronsee element0 = listA.get(0); 
Personee element1 = listA.get(1); 
Personee element3 = listA.get(2); 


//access via Iterator 
Iterator iterator = listA.iterator(); 
while(iterator.hasNext(){ 
    Personee personee = (Personee) iterator.next(); 
} 


//access via new for-loop 
for(Personee personee : listA) { 
    System.out.println(personee.nom + "," + personee.age + "," + personee.poid); 
} 
+1

과 같은 것 같습니다. 그건 arraylist입니다. 객체 "personnes"목록을 얻으려면 어떻게해야합니까? –

+0

@IslamBouderbala 내 대답이 업데이트되었습니다. List 인터페이스는 목록에 추가 및 읽을 수있는 유형을 제한하는 일반 목록 (List )으로 만들 수 있습니다. 이 경우'List' 객체는'Personee' 유형의 객체 만 처리 할 수 ​​있습니다. –