Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 LinkedList suitable for this?
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Epic Tissue




PostPosted: Fri Jun 13, 2008 10:48 am   Post subject: LinkedList suitable for this?

If I have objects representing people. And those people have friends (which are people too). Whats the best way for me to store that information? In a arraylist, linkedlist or some other method?

I tried an arraylist.

code:
private ArrayList<Person> friends;

        public void addFriend(Person friend) {
                friends.add(friend);
        }


code:
andrew.addFriend(peter);


where andrew and peter are a "Person" object. I kept getting a null pointer exception there.

I looked up a tutorial on arraylists, and it said it's not the best if you need to delete things from the list regularly (which in my case, that might be needed).

So two questions:

1) Which is the best way to go about this?
2) Why was I getting a NullPointer exception?

Thanks for any suggestions
Sponsor
Sponsor
Sponsor
sponsor
Vermette




PostPosted: Fri Jun 13, 2008 11:07 am   Post subject: RE:LinkedList suitable for this?

An ArrayList would probably be fine, but you could consider any of the other Collection types. Since I don't think the order of the friends is really important, you could consider one of the Collections based on the Set interface, like the HashSet:

http://java.sun.com/javase/6/docs/api/java/util/HashSet.html

Without being able to see your exact error, the most likely cause of your exception is forgetting to instantiate your ArrayList (create a new instance of ArrayList friends, probably in your Person constructor.
Epic Tissue




PostPosted: Fri Jun 13, 2008 11:16 am   Post subject: Re: RE:LinkedList suitable for this?

Vermette @ Fri Jun 13, 2008 5:07 pm wrote:
Without being able to see your exact error, the most likely cause of your exception is forgetting to instantiate your ArrayList (create a new instance of ArrayList friends, probably in your Person constructor.


Thanks Vermette! That's exactly what was wrong Embarassed

I'll look at the other link in a minute.
DemonWasp




PostPosted: Fri Jun 13, 2008 12:49 pm   Post subject: RE:LinkedList suitable for this?

Vermette is right, but I'd just like to add:

It's probably a good idea to use the base collection interfaces for variable types (though of course you need an implementation when instantiating). So you may want:

Java:
Map<String,Person> = new HashMap<String, Person>();


It doesn't seem like a big thing, but if you ever have to change the actual type of the variable, it's only a one-line change. Plus, it lets your API be more descriptive and generic - you can return Map<String, Person> instead of HashMap<String, Person>.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: