i'm trying create person class has getname() method, addfriend(person r) method, arraylist<person> getfriendlist()
string name; private arraylist person friend = new arraylist person (); public person(string name) { this.name=name; } public arraylist<person> getfriendlist(){ return friend; } public void addfriend(person r){ friend.add(r); } friendsoffriends(){ //list of friends of friends of person. } public static int (p,q){ //return number of friends shared both r , q. }
public static int numberofmutualfriends(person p, person q){ //return number of friends shared both r , q. return intersection(p.friend,q.friend).size(); } public list<person> intersection(list<person> list1, list<person> list2) { list<person> list = new arraylist<person>(); (person t : list1) { if(list2.contains(t)) { list.add(t); } } return list; } you'll need redefine hashcode , equals method in order make work.
Comments
Post a Comment