java - Need to use "new" when adding to a List? -


I am new to Java and am trying to know if I need to pass new instances of class Is a list entry, such as:

  CCustEntry entry = new CCustEntry (); & Lt; CCustEntry & gt; Castlist = New Arrestist & lt; CCustEntry & gt; (); Do {// enter all entry fields entry.id = [...]; [Fill in other areas of entry from DB] // Ent Entry (Constructor Copy Entry) CustList.add (new secytree); } While (C.moveToNext () == true);   

Or ... I can remove new entry and bus entry in this way:

  CustList.add (entry) ;    

I'm reading about lists, but I'm more confused than before. Coming from Delphi and C ++, I'm fine with syntax, but some "grammar" of Java is still scratching my head.

Thanks for any help.

It depends whether the entry will be modified or not It seems that you have a bean Which makes the shallow copy of the entry, so if the field of entry will be modified, then you should add a copy otherwise it will be ruining the memory just to create a new one.

Here, it seems that you are using the same entry in the entire loop, so you can make a copy. Otherwise, there will be only one item in the entire list - that is, every object is the same object.

Another comment: In Java, the name is normal for values ​​starting with the field and lowercase letters.

Comments

Popular posts from this blog

excel vba - How to delete Solver(SOLVER.XLAM) code -

github - Teamcity & Git - PR merge builds - anyway to get HEAD commit hash? -

ios - Replace text in UITextView run slowly -