Tell me, is there a way to write all Class objects into an array with one method? After each created object, write list.add with your hands as it seems to be completely wrong.

 ArrayList<Human> humans = new ArrayList(); Human grandpa1 = new Human("Ded1", true, 99); Human grandma1 = new Human("Ba1", false, 90); Human grandpa2 = new Human("Ded2", true, 98); Human grandma2 = new Human("Ba2", false, 91); Human father = new Human("Pa", true, 41, grandpa1, grandma1); Human mother = new Human("Ma", false, 35, grandpa2, grandma2); Human kid1 = new Human("Kid1", true, 8, father, mother); Human kid2 = new Human("Kid2", false, 13, father, mother); Human kid3 = new Human("Kid3", true, 5, father, mother); humans.add(grandpa1); humans.add(grandma1); humans.add(grandpa2); humans.add(grandma2); humans.add(father); humans.add(mother); humans.add(kid1); humans.add(kid2); humans.add(kid3); 
  • There are ways from the category of bad advice - Stranger in the Q
  • @StrangerintheQ is: lib.ru/ANEKDOTY/osterwred.txt ? - Miron
  • humans.add(new Human("Ded1", true, 99)); - have you tried? - Tsyklop
  • @Tsyklop I think he needs a separate mom, dad separately. Therefore, it is more rational to store them in arrays, and if necessary, call the static method - Miron
  • @Miron arrays are not as functional as collections. For them, you will have to write code to work with them that already exist in the collection. In other words - reinvent the wheel. With the same success you can have different collections. - Tsyklop

2 answers 2

You can write an addition to the constructor, then the object will immediately fly into the sheet, but you do not need to do this.

It is better to create a method in a class like Human

 public Human addToList(ArrayList<Human> humanList) { humanList.add(this); return this; } 

Then you can just write

 ArrayList<Human> humans = new ArrayList(); Human grandpa1 = new Human("Ded1", true, 99).addToList(humans); ... 

Another option is to create a separate class structure like

 class HumanList { private ArrayList humans = new ArrayList<>(); public Human add(Human human){ humans.add(human); return human; } public ArrayList getHumans(){ return humans; } } 

Then the addition will look like

 HumanList humans = new HumanList(); Human grandpa1 = humans.add(new Human("Ded1", true, 99)); … 

And use sheet

 humans.getHumans(); 

It seems to me that the second option is better, because leaves more room for maneuver. For example, you can make it a singleton or cram in there more methods to manage collected human-s ... But here it all depends on the task.

  • one
    I will not argue strongly because This answer is accepted, but ... So no one does. Passing a collection to a class method and saying, "add yourself here" is completely wrong. The second option is generally much better, but too confusing. We in object parameters create object which is returned by the same method. Yes, it reduces the number of lines, but the obviousness of such a code is questionable. I would leave the add () method to return the result of insertion into the sheet, i.e. boolean Or generally void - Oleksiy Morenets
  • Yes, it is so more logical. Just the question was, as I understood, dictated by the desire to shorten the code ... - Adm123

First, create all the necessary objects, and then a sheet with a constructor that accepts another sheet:

 ArrayList<Human> humans = new ArrayList(Arrays.asList(grandpa1, grandma1 /*, и т.д. */)); 
  • Sorry, but what does the argument mean, итд... ? - 0xdb
  • I'm sorry. Do not be clever. - Olexiy Morenets
  • If you are offended by my comment, mark it with anxiety as "unfriendly" or explain why you perceive it that way. Do not respond in a caustic tone. - 0xdb
  • one
    I am not offended by any means. I'm just sure that even in jvichek in Java would understand what the argument means, итд... , not to mention such experienced members of the community as you ... - Oleksiy Morenets
  • one
    I used to answer with a fully working code, after which everyone rushed in with slippers, which everyone provides on a saucer and the topstarter should not even think. Therefore now I leave a field for activity - Oleksiy Mororets