2016-08-12 44 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)。您將知道代碼經過的路徑,並更好地查看問題。 –