aspose file tools*
The moose likes Beginning Java and the fly likes Need help with using arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with using arraylist" Watch "Need help with using arraylist" New topic
Author

Need help with using arraylist

Leeee Ming
Greenhorn

Joined: May 24, 2004
Posts: 12
I have to develop this small program where my program can store sms messages with the senders name and date....

I have done my part on creating the sms with sendername and text :

Here are my codes :





So this is the what i need to do in my program...

1.Show all messages received from a particular sender??

I am confused how can i show the messages from a particular sender,so its like when i type this person name it shows all the messages send by him?? since i know i can only retrieve messages using the index of the arraylist.... I am stuck here...
Please Help me.
Thanks
Wai Hung
Greenhorn

Joined: Apr 15, 2004
Posts: 23
Hi,

DO you have to use ArrayList like what you have done here?

How about using key-value pair collection class?
e.g. Use HashMap.

Store the name as key and a ArrayList as the value element.
Here the ArrayList will store objects that contain all information,
include message, date, etc.
Leeee Ming
Greenhorn

Joined: May 24, 2004
Posts: 12
I have to use Arraylist... I'm a newbie to Java...
Preetham Chandrasekhar
Ranch Hand

Joined: Nov 05, 2003
Posts: 98
well.....wat has to be done is not really complicated...i guess....

since arraylist is indexed...
1. for a particular name say 'name0' in the senderarraylist, search all the names that correspond to name0 and take that particular index and retrieve the message from the messagearraylist...

for(int i=0;i<senderlist.length;i++){
if(senderlist[i].equals(name0)
System.out.println(messagelist[i];//or whatever u wanna do here
}

hope it helps and i answered your question right

Preetham


"In theory, there is no difference between theory and practice. But, in practice, there is."<br /> - Jan L.A. van de Snepscheut
Leeee Ming
Greenhorn

Joined: May 24, 2004
Posts: 12
Lets say I use this SMS class to save my message...

I have another class called...Message..

So how do i retrieve my message from my SMS class using the method given....

for(int i=0;i<senderlist.length;i++){
if(senderlist[i].equals(name0)
System.out.println(messagelist[i];//or whatever u wanna do here
}

Thanks
shreehari Gopalakrishnan
Ranch Hand

Joined: Jun 01, 2004
Posts: 30
Hi leee

I appreciate u'r eagerness to stick on the concept and thinking from it

Actually in java we dont have to use arrayLists to store Objects
If u want to do so u have to do with collections

Still if u want to access the arraylists from other class
then u can pass the arraylists thru a constructor to another class

Instaitiate Message

like

Message msg = new Message(senderList,textList,dateList);

and do as suggested by Preetham Chandrasekhar

Best of luck

Thanks

Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
of course if your teacher tells you to use ArrayList that's what you have to use

To store different things in an ArrayList is simple. Getting them out again is easy too, just don't forget to cast them to the correct type and get them out in the correct order (so you don't try to get a String and treat it as something else for example).


42
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Need help with using arraylist