Tell me please. There is an array of ArrayList. I declare it in the class of variables:

private ArrayList<Part> parts; 

How to write a check whether it is initialized or not? and if not, initialize. The following code is incorrect:

 public ArrayList<Part> initArrayList(Part part){ if (parts.isEmpty()) { parts = new ArrayList<>(); parts.add(part); } else parts.add(part); return parts; 
  • Empty (isEmpty) is already initialized. It is necessary to check equality with null as in the answer below. new ArrayList () each time creates an array that will be isEmpty (if nothing is put into it of course) - Sergey

3 answers 3

How to write a check whether it is initialized or not? and if not, initialize.

For example:

 public ArrayList<Part> initArrayList(ArrayList<Part> parts) { if (parts == null) { return new ArrayList<>(); } return parts; } 

UPD .

Due to the level of the question asked, I did not specifically want to confuse the author with a ternary operator , but if you know about him, the initArrayList(...) method can be implemented somewhat shorter:

 public static ArrayList<Part> initArrayList(ArrayList<Part> parts) { return parts == null ? new ArrayList<>() : parts; } 

In general, it is so-called. syntactic sugar , no more than that.

  • Return parts == null? new ArrayList (): parts; Yes, and the extra transfer was sent to the method to make a check, well, well ... no? How does this differ from if (parts! = Null) parts.add (new Part); in theory, although unlikely, there is an opportunity to catch a stick and it can strike with both sides, well, okay, plus, and that’s good)) - Shwarz Andrei
  • @ShwarzAndrei, 1. I deliberately did not confuse the person who asks questions of this level with a ternary operator. 2. At the author's code was issued in the form of a method - and left. - post_zeew 2:55 pm
  • @ m.vokhm, Please explain about "if the parts not initialized, it will remain null after executing such a code." - post_zeew
  • @post_zeew, I inattentively looked, did not notice that parts is an argument of a method, it seemed that it is a question of a field of object. Rented my comment. - m. vokhm
 // Запрос списка Ρ‡Π΅Ρ€Π΅Π· Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ элСмСнта с Π»Π΅Π½ΠΈΠ²ΠΎΠΉ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠ΅ΠΉ списка public ArrayList<Part> initArrayList(Part part){ if (parts == null) parts = new ArrayList<>(); parts.add(part); return parts; } // Π Π°Π±ΠΎΡ‚Π°Ρ‚ΡŒ Π±ΡƒΠ΄Π΅Ρ‚, Π½ΠΎ ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½Π΅Π΅ разнСсти Π΄Π²Π΅ Ρ€Π°Π·Π»ΠΈΡ‡Π½Ρ‹Π΅ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ ΠΏΠΎ Ρ€Π°Π·Π½Ρ‹ΠΌ ΠΌΠ΅Ρ‚ΠΎΠ΄Π°ΠΌ // Запрос списка с Π»Π΅Π½ΠΈΠ²ΠΎΠΉ ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠ΅ΠΉ public List<Part> getParts() { if (parts == null) parts = new ArrayList<>(); return parts; } // Π”ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΠ΅ элСмСнта Π² список public void addPart(Part part) { getParts().add(part); } 
  • Comment on the code in the answers - justify your decision - tutankhamun
  • Added comments to the code and an explanation of why this is not quite right. - gmatagmis

In general, in theory, your parts field is initialized from the very beginning, since All object fields in java are initialized by their default value. That's just because ArrayList is an object, then the default value will be null . And if you want to have just a working copy of an ArrayList, then you can proceed as in the previous answer, or you can set the field value in general when declaring:

 private ArrayList<Part> parts = new ArrayList<>(); 
  • one
    Sometimes it is necessary to distinguish whether we have already worked with him or not, and it makes sense to keep null there, as a sign that we have not yet applied. And when accessing it, do this: if (parts == null) parts = new ArrayList(); return parts; if (parts == null) parts = new ArrayList(); return parts; - m. vokhm
  • This is understandable, just in this case, the author of the post apparently tries to run ahead without really understanding what ArrayList is, and the object as a whole, so I tried to explain - selya