-4

I have a function that I want to output a list of all the names in the object. What is the easiest way to do this?

Requirement object has a names variable and a method called getChildren which contains a list of Requirements.

Void updateItems(Requirement r)
{
   System.out.println(r.getChildren.names) //doesnt work

}

I want to output all the names of the children objects like this:

Hello1, Hello2, Hello3

lakeIn231
  • 1,177
  • 3
  • 14
  • 34
  • 5
    How about you actually call the `getChildren` method? Also *"doesn't work"* isn't a proper problem description. *Also* you'll probably need to use a loop if you have a list – UnholySheep Apr 20 '17 at 17:14
  • As stated above, you need to actually call the `getChildren` method. `getChildren()` – Carter Brainerd Apr 20 '17 at 17:15
  • by the way, `void` is written with a lowercase v – UnholySheep Apr 20 '17 at 17:24
  • Possible duplicate of [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value) – lakeIn231 Apr 20 '17 at 18:24

3 Answers3

0

Requirement object has a names variable and a method called getChildren which contains a list of Requirements.

the code below will retrieve the children's list for the current r object and iterate over its list in order to display their names.

void updateItems(Requirement r)
{ 
   r.getChildren().forEach(c -> System.out.print(c.names+" "));
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
0

You mentioned "getChildren contains a list of Requirements". I guess you mean getChildren returns a list of Requirement objects?

If that's right. You could try:

    void updateItems(Requirement r) {
        for (Requirement child: r.getChildren()) {
            System.out.println(child.names);
        }
    }
-1

You said the method getChildren contains a list.

So you must iterate through the elements of the list to get the output:

First: Create a variable for your list.

ArrayList<String> x = new ArrayList<>(); 

Then you can call the getChildren method to save your list:

x = getChildren();

And then you can iterate through it.

for(String names : x){
     System.out.println(names);
}

That means:

void updateItems(Requirement r)
{
     ArrayList<String> x = new ArrayList<>(); 

     x = r.getChildren();

     for(String names : x){
         System.out.println(names);
     }
}