package com.javarush.task.task07.task0724;
import java.io.IOException;
import java.util.ArrayList;
public class Solution {
public final static ArrayList<Human> humans = new ArrayList<>();
public static void main(String[] args) throws IOException {
Human human0 = new Human("Tom", true, 90);
humans.add(human0);
Human human1 = new Human("Bill", true, 95);
humans.add(human1);
Human human2 = new Human("Mary", false, 80);
humans.add(human2);
Human human3 = new Human("Jenny", false, 85);
humans.add(human3);
Human human4 = new Human("Jack", true, 45, human0, human2);
humans.add(human4);
Human human5 = new Human("Nelly", false, 37, human1, human3);
humans.add(human5);
Human human6 = new Human("Kris", true, 18, human4, human5);
humans.add(human6);
Human human7 = new Human("Greg", true, 14, human4, human5);
humans.add(human7);
Human human8 = new Human("Maya", false, 8, human4, human5);
humans.add(human8);
printList();}
public static void printList() {
for (int x = 0; x < humans.size(); x++) {System.out.println(humans.get(x));}}
public static class Human {
public String name;
public boolean sex;
public int age;
public Human father;
public Human mother;
Human(String name, boolean sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;}
Human(String name, boolean sex, int age, Human father, Human mother) {
this.name = name;
this.sex = sex;
this.age = age;
this.father = father;
this.mother = mother;}
public String toString() {
String text = "";
text += "Имя: " + this.name;
text += ", пол: " + (this.sex ? "мужской" : "женский");
text += ", возраст: " + this.age;
if (this.father != null)
text += ", отец: " + this.father.name;
if (this.mother != null)
text += ", мать: " + this.mother.name;
return text;}}}
Подскажите, как добавить хуманов в массив циклом, а не по одному?Anonymous #2053422
16 уровень
Не получилось создать массив объектов через цикл
Решен
Комментарии (3)
- популярные
- новые
- старые
Для того, чтобы оставить комментарий Вы должны авторизоваться
Алексей
7 мая 2019, 21:05
Как Вы себе представляете цикл? Хотя бы на каком-нибудь псевдокоде Вы можете себе это представить, чтобы было проще чем есть сейчас?
0
Justinian Judge в Mega City One Master
6 мая 2019, 02:12решение
Цикл предназначен для однотипной обработки данных, но в этой задаче все объекты разные да еще и с дополнительными условиями каждый, смысла нет никакого.
Если бы это была большая программа, которая обрабатывала бы тысячи объектов, была бы структура данных, которая бы получала извне параметры, из запросов или БД и вызывала бы метод void saveHuman(), то есть даже вэтом случае цикла не было.
Для этой задачи, через цикл думаю где-то так бы решение выглядело:
Но эта задачка не на циклы
+3
Стас Пасинков Software Developer в Zipy Master
6 мая 2019, 02:11
как-то так.
но проблема в том, что ладно там еще с именами, даже ладно там пол менять туда-сюда, тоже можно сделать.
но как вы в определенных случаях будете туда родителей передавать? вот это вопрос)
так что думаю, цикл тут только все усложнит
+1