2016-08-12 45 views
0

我正在做一个简单的sch程序来添加朋友,即将对象添加到arraylist中。我遵循了一切,但我的方法befriend()似乎并不奏效。 当我手动测试使用.add()在主,它的作品。我在哪里做错了?Arraylist not adding java

import java.util.*; 
public class NetworkFriends { 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 



    Person me = new Person("Aloysius", 1); 

    ArrayList<Person> myList = new ArrayList<Person>(Arrays.asList(me.getFriendList())); 

    Person p1 = new Person("Gorgon", 2); 
    Person p2 = new Person("Eddy", 3); 

    me.befriend(p1); 

    for(Person i : myList) { 
     System.out.println("Name: " + i.getName()); 
    } 
    } 
} 

class Person 
{ 
    private int id; 
    private String name; 
    private ArrayList<Person> friendList; 
    private static int runningNum = 0; 
    private static int friendsLimit = 5; 
     private String degree; 
     private int degreeNum; 

    /* Constructor - 1 param */ 
    public Person(String name, int degreeNum) 
    { 
     //Implement your code here.. 
     //Initialize all necessary variable(s) 
       this.name = name; 
       friendList = new ArrayList<Person>(5); 
       this.degree = degree; 
       this.degreeNum = degreeNum; 
} 

public void befriend(Person p){ 
     //Implement your code here.. 

       ArrayList<Person> anotherList = new ArrayList<Person>(Arrays.asList(p.getFriendList())); 

       for(Person i : friendList) { 
        if(!isFriend(this) && friendList.size() < 5) { 
          friendList.add(p); 
          anotherList.add(this); 
        } 
        else if(!isFriend(this) && friendList.size() == 5) { 
         System.out.println("Friends limit reached"); 
        } 

        else { 
         System.out.println("Already in friend list"); 
        } 
       } 

    } 
} 

public boolean isFriend(Person p){ 
     //Implement your code here.. 

       boolean isItAFriend = true; 
       for(Person i : friendList) { 
        if(friendList.contains(p)) { 
         isItAFriend = true; 
        } 
        else { 
         isItAFriend = false; 
        } 
       } 
       return isItAFriend; 

     } 
+5

我可以告诉你,你isFriend方法是不是做你认为它是。但是由于这看起来像是家庭作业,我会强烈建议您花几分钟时间熟悉使用调试器并使用它,因为它可以快速回答您的问题。 https://www.youtube.com/watch?v=9gAjIQc4bPU –

+2

这是什么:'new ArrayList (Arrays.asList(p.getFriendList()));'?你为什么要再次将一个列表变成一个列表成为一个列表?并且(虽然您没有显示代码)会创建副本,因此原件上的操作不会影响副本。 –

+0

两件事:1)'ArrayList anotherList = new ArrayList (Arrays.asList(p.getFriendList()));'是可怕的。不要写这种代码。 2)关键点是你想互相去重复朋友。使用Set而不是List将使其变得非常容易。 :-) – MageXellos

回答

2

问题在于你的befriend方法中的foreach循环。您正在使用构造函数创建一个新的Person,该构造函数创建初始大小为5的空友列表,但仍为空。

在您的befriend方法中,您将为此空列表中的每个朋友循环。所以循环内的代码将不会被执行,并且朋友也不会被添加到列表中。

我怀疑你想要做这样的事情:(和这看起来像功课我只会给你的伪代码)

  1. 已经是人的朋友
    • 是 - 什么需要做或给予反馈,并返回
    • 否 - 继续
  2. 他们已经达到了他们的朋友极限
    • 是 - 显示反馈和返回
    • 否 - 继续
  3. 添加好友
+0

你指的是哪一个循环? – Aloysius

+0

谢谢。我得到它的工作使用你的伪代码 – Aloysius

+0

非常清晰和详细的答案。 @Aloysius:调试类似这样的问题的一种方法是在每个代码分支(if,for,while ...)中放置日志(在你的情况下是System.out.println)。您将知道代码经过的路径,并更好地查看问题。 –