Question about using ArrayList in Java? -


I do not know whether the title is correct or not.

I have 2 options: Option 1:

Class A {Areerelist & lt; B & gt; Stain = new array list & lt; B & gt; (); Public B [] getBList () {Return (b []) Blist.toArray (); }} Square mask {public b [] getAllB () {a = new A (); Back a.getBList (); }}

Option 2:

  Class A {array LIST & lt; B & gt; Stain = new array list & lt; B & gt; (); Public irregularities () {Return retrieval; }} Square mask {public b [] getAllB () {a = new A (); Arrayist List = AA.L.List (); Return (b []) Blist.toArray (); }}  

Which option should I use?

Use collection sections, unless you have a specific reason to use the array Therefore, I choose option 2.

There are several other comments that may be about your code.

First of all, it is better - member variables a ArrayList & lt; B & gt; Instead of <> code> A a list & lt; B & gt; Additionally, member variables should be private (unless it is a very good reason not to be private ). Third, use generics whenever possible - why your method returns getBList () a raw ArrayList ? D-Facto Naming standard for variables in Java starts with lower-case letters. Do not call member variable limits , but bList (or some other, better name).

  class A {Private list & lt; P & gt; BList = New Arrestist & lt; B & gt; (); Public listing & lt; B & gt; GetBList () {Return bList; }}  

What is a B [] for the class mask ?

To consider another point is to make your class a unchanged, because if you get the list from the A object , Then there may be unexpected things, and then add or remove elements from the list (your A list inside the object will also be changed and it can be confusing). You can get your getBList () method:

  public list & lt; B & gt; A reading-only view of the list must be returned back. GetBList () {Return Collections.unmodifiableList (bList); }  

Comments