Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with using arraylist

 
Leeee Ming
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to use Arraylist... I'm a newbie to Java...
 
Preetham Chandrasekhar
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Leeee Ming
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic