d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Groovy Question > I Dont Know It
Add Reply New Topic New Poll
Member
Posts: 8,724
Joined: Oct 20 2007
Gold: 2,794.65
Jul 5 2012 07:10am
So I've been given this question and I don't know it can anyone help?

We have a list of User and User class has name,surname properties. Which is the shortest code to get
a list of all the surnames?

Code
def newList = []

User.each{
newList.add(user.surname)
}

for( i in User{
newList.add(i.surname)
}


How far off am i? It does mean a list of user objects right? so I can just go User.name / User.surname to get at that value?

//i'm not sure on the question it's worded funny that is why i'm posting here

This post was edited by Atonement on Jul 5 2012 07:19am
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Jul 5 2012 10:50am
edit: you're close ish i'd say. but it looks like you're trying to do it twice... the syntax for using each is also off.

are you assuming that the list is called User? that seems very unlikely.


the question isn't worded that funny. it says, assuming you're given a list of User objects - from that, create a list of surnames.

This post was edited by irimi on Jul 5 2012 11:08am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll