2011-06-23 31 views
-2
class Main 
{ 

    public static void main() 
    { 
     Student one=new Student(); 
     one.setName("firstname"); 

     AllStudent all=new AllStudent(); 
     all.add(one); 

     // Now changing name of student 

     one.setName("secondname"); 

     // Here I getAll Students 

     Collection<Student> cs=all.getAll(); 


      java.util.Iterator<Student> itr=cs.iterator(); 


      while(itr.hasNext()){ 
       Student rgc=itr.next(); 
       System.out.println(rgc.getName()); 
      } 
    } 

    clas public Student 
    { 

     String name; 

     public void setName(String name) 
     { 
      this.name=name; 
     } 

     public String getName() 
     { 
      return name; 
     } 
    } 

    clas public AllStudent 
    { 

     Collection<Student> stds; 

     public void addAll(Collection<Student> stds) 
     { 
      this.stds=stds; 
     } 

     public void add(Student std) 
     { 
      stds.add(std); 
     } 

     public Collection<Student> getAll() 
     { 
      return stds; 
     } 
    } 
} 

現在這會給我「名字」。引用不會getUpdated傳遞給類後?

但我想獲得「secondname」。那我該怎麼辦?如果有API更改或創建新類?

+1

顯示爲構造函數學生和所有學生班。有些東西告訴我問題出在你的構造函數中......除非這些問題存在於未提及的代碼中。 –

+0

這應該不起作用。你永遠不會初始化集合'stds'。 – trutheality

+0

你是什麼意思得到'secondname'。你只有在你的班級學生的名字。如果你想要了解更多有關學生的信息,請將它們添加到本課程中,認爲這應該足夠了 – Balanivash

回答

1

試試這個它是你的代碼,我只需要實現的東西在

package pkg; 

import java.util.Collection; 
import java.util.HashSet; 

public class Main { 
    public static void main(String arg[]){ 
     Student one=new Student(); 
     one.setName("firstname"); 

     AllStudent all=new AllStudent(); 
     all.add(one); 

     // Now changing name of student 

     one.setName("secondname"); 

     // Here I getAll Students 

     Collection<Student> cs=all.getAll(); 

     java.util.Iterator<Student> itr=cs.iterator(); 

     while(itr.hasNext()){ 
      Student rgc=itr.next(); 
      System.out.println(rgc.getName()); 
     } 

    } 
    static class Student{ 
     private String name; 
     void setName(String name){ 
      this.name = name; 
     } 
     String getName(){ 
      return name; 
     } 
    }  
    static class AllStudent{ 
     Collection<Student> stds = new HashSet<Main.Student>(); 

     void addAll(Collection<Student> stds){ 
      this.stds = stds; 
     } 

     void add(Student std){ 
      this.stds.add(std); 
     } 
     public Collection<Student> getAll(){ 
      return stds; 
     } 

    } 
} 

我已經運行,並得到了secondname

+0

你能解釋一下AllStudent和Student對象在堆和堆棧中的存儲位置嗎?以及我如何獲得第二名。 – Nitul

+0

@Nitul它不會將該對象添加爲值,但它將添加該對象作爲參考。這裏是關於這個http://www.chetanasforum.com/index.php?showtopic=1474 – Pratik

+0

的更多解釋現在假設我的學生,所有學生和主要班級都在單獨的包中。 – Nitul

1

問題是你還沒有真正編寫一個編譯和運行的程序。如果修復編譯錯誤,改變main簽名和初始化你的收藏,你會發現程序始終打印

secondname 

這是你想要的。

1
public class Student 
{ 
    private String name; 
    public Student(String pName) 
    { 
     name = pName; 
    } 

    public void setName(string pName) 
    { 
     name = pName; 
    } 

    public String getName() 
    { 
     return name; 
    } 

    public String toString() 
    { 
     return name; 
    } 
} 

public class StudentCollection 
{ 
    private LinkedList<Student> studs; 

    public StudentCollection() 
    { 
     studs = new LinkedList<Student>(); 
    } 

    public void add(Student stud) 
    { 
     if (stud != null) 
     { 
      studs.addLast(stud); 
     } 
    } 
} 

public class Driver 
{ 
    public static void main(String[] args) 
    { 
     Student one = new Student("Matt"); 
     StudentCollection students = new StudentCollection(); 
     students.add(one); 

     one.setName("Kyle"); 
     for(Student stud : students) 
     { 
      System.out.println(stud); 
     } 
    } 
} 

OUTPUT:

凱爾