Forums Register Login

Need help with using arraylist

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
I have to use Arraylist... I'm a newbie to Java...
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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

+Pie Number of slices to send: Send
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).
Did you ever grow anything in the garden of your mind? - Fred Rogers. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1379 times.
Similar Threads
Need Help with Java...Just 1 line...
Need help in Java Class
watchdog
The correct location of Jar files
Spring validation
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 22:42:27.