2016-09-26 4 views
-2
public class MainClass 
{ 
    private String name=""; 

    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public static void main(String[] args) 
    { 
     ArrayList<String> al=new ArrayList<String>(); 
     String str=new String("abc"); 
     al.add(str); 
     str="def"; 
     al.add(str); 
     str="ghi"; 
     al.add(str); 
     str="jkl"; 
     al.add(str); 
     System.out.println(al); 

     ArrayList<MainClass> al1=new ArrayList<MainClass>(); 
     MainClass mainclass=new MainClass(); 
     mainclass.setName("Abhi"); 
     al1.add(mainclass); 
     mainclass.setName("Sajith"); 
     al1.add(mainclass); 
     mainclass.setName("Sridhar"); 
     al1.add(mainclass); 

     for(MainClass main:al1) 
      System.out.println(main.getName()); 
    } 
} 

Ausgang:Wie funktionieren die Objekte in diesem Code?

[abc, def, ghi, jkl] 
Sridhar 
Sridhar 
Sridhar 

Warum mein Objekt in der zweiten Fall überwiegende bekommen, wenn es nicht in ersten Fall geschieht?

+5

Sie erstellen immer nur ein 'neues MainClass();' - Objekt, so dass Sie erwarten, dass Sie dieses ein Objekt setzen, wenn Sie es mit drei verschiedenen Werten einstellen. Sie sollten in der Lage sein, den Code in Ihrem Debugger zu durchlaufen, um genau zu verstehen, was passiert. –

+0

Ob Sie 'new String' verwenden oder ein String-Literal verwenden, Sie haben vier verschiedene Strings im ersten Fall. Wenn Sie also diese Referenzen hinzufügen, haben Sie immer noch 4 verschiedene Strings. –

+0

Beachten Sie, dass ArrayList al = neue ArrayList (); ist dasselbe wie ArrayList al = new ArrayList <>(); und schlechte Übung zu String str = new String ("abc"); aber string str = "abc"; – Tokazio

Antwort

1
ArrayList<String> al=new ArrayList<String>(); 
    //a1 is empty ArrayList of Strings. 
    String str=new String("abc"); 
    //str is now a reference to "abc" 
    al.add(str); 
    //a1 has now reference to "abc" 
    str="def"; 
    //str has now reference to "def" 
    al.add(str); 
    //a1 has now reference to "abc" and reference to "def" 
    str="ghi"; 
    //str has now reference to "ghi" 
    al.add(str); 
    //a1 has now three different references 
    str="jkl"; 
    //str has now reference to "jkl" 
    al.add(str); 
    //a1 has now four different references. 
    System.out.println(al); 

    ArrayList<MainClass> al1=new ArrayList<MainClass>(); 
    //al1 is now empty 
    MainClass mainclass=new MainClass(); 
    //mainclass has now reference to an object with an empty String 
    mainclass.setName("Abhi"); 
    //mainclass' reference didn't change. It's still the same, however the string is different 
    al1.add(mainclass); 
    //al1 has now one reference to the mainclass 
    mainclass.setName("Sajith"); 
    //mainclass' reference didn't change. It's still the same, however the string is different 
    al1.add(mainclass); 
    //al1 has now two references to the mainclass 
    mainclass.setName("Sridhar"); 
    //mainclass' reference didn't change. It's still the same, however the string is different 
    al1.add(mainclass); 
    //al1 has now three references to the mainclass 

    for(MainClass main:al1) 
     System.out.println(main.getName()); 

also Ihre erste ArrayList hat vier verschiedene Referenzen, die auf unterschiedliche Werte. Sie zweite ArrayList hat dreimal die gleiche Referenz, die mainClass, die am Ende eine String darstellt, die "Sridhar" ist.

Verwandte Themen