I want to store some values (actually a message) using list in Java. I want to store something like:
{user1, message, user2, time}
{user1, message, user4, time}
{user3, message, user1, time}
I have declared an arraylist like this:
public List<List<String>> message = new ArrayList<List<String>>();
How can I add values in this list and how can I get a specific row (let's say the second row) of this list?
I've tried:
message.get(0).add(msg, target,time);
but I get an error:
"The method add(String) in the type List is not applicable for the arguments (String, String, Integer)"
You could do something like:
List<Object[]> list = new ArrayList<Object[]>();
Object[] ob = new Object[4];
ob[0] = "user1";
ob[1] = "messge1";
ob[2] = "user2";
ob[3] = "time1";
list.add(ob);
ob = new Object[4];
ob[0] = "user2";
ob[1] = "messge2";
ob[2] = "user4";
ob[3] = "time2";
list.add(ob);
// Output the values
for(Object[] o : list){
System.out.print(o[0] + "\t");
System.out.print(o[1] + "\t");
System.out.print(o[2] + "\t");
System.out.print(o[3] + "\n");
}
and the output will be:
user1 messge1 user2 time1
user2 messge2 user4 time2
by
message.get(0);
you are retriving the first list in list of list referred by message
List
has add(T object)
method so you can't pass all 3 Objects in one call, one way to do is
message.get(0).add(msg);
message.get(0).add(target);
message.get(0).add(time);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.