2016-05-05 63 views
2

對不起,在這裏包括這麼多,我只是不知道從哪裏開始,因爲我是新來的Java,我迷失瞭如何顯示一個朋友的整個列表,一旦我輸入他們進入陣列。 我需要使用for循環來做到這一點。顯示列表的朋友陣列

回答

3

添加toString方法您Friend類,如:

@Override 
public String toString() { 
    return "Friend [firstName=" + firstName + ", firstAge=" + firstAge + "]"; 
} 

所以,當你調用System.out.println()你會得到一個人類可讀的格式,而不是哈希碼。

我還建議你將下面的行移出:for循環中的System.out.println("\nYou have added the following friends:");

您可以從列表中刪除喜歡條目:

public void removeFriend() 
{ 
    System.out.println("Please enter Name of person you would like to remove: "); 
    String name = input.nextLine(); 
    Iterator<Friend> it = friendList.iterator(); 
    while(it.hasNext()){ 
     Friend f = it.next(); 
     if(f.getName().equals(name)){ 
      it.remove(); 
     } 
    } 
} 
+0

這工作,我很欣賞移動打印語句圈外的意見。現在我似乎無法從列表中刪除和朋友。這裏是我的代碼,我目前有添加朋友和刪除朋友... @ ManishKothari – gan212

+0

@ gan212我已經添加了代碼,以從friendList中刪除對象。 –