import java.util.ArrayList;
public class FriendList {
private ArrayList<String> friendList;
public FriendList() {
ArrayList<String> friendList = new ArrayList<String>();
}
public void addFriend(String friend) {
friendList.add(friend);
}
public String getFriends() {
return friendList.toString();
}
}
我已經嘗試了一些東西,但似乎無法設法將字符串添加到數組列表中。任何想法,爲什麼這可能是?當我嘗試向我的ArrayList添加字符串時,爲什麼我的類會導致'NullPointerException'?
甚至更好的改變:好友列表=新的ArrayList < >()如果你使用Java 1.7 – 2014-10-30 14:57:23
@DavidSoroko爲什麼你認爲這更好?僅僅是因爲你不必再輸入6個字母? – spoko 2014-10-30 15:02:48
其中之一是資本;-)。看看這個[鏈接] http://stackoverflow.com/questions/4166966/what-is-the-point-of-the-diamond-operator-in-java-7 可以改變'private ArrayList friendList '只能在一個地方使用私有ArrayList friendList',所有東西都會編譯。 –
2014-10-30 15:19:27