Glyn
Glyn

Reputation: 1995

How to correctly specify a list in java

I am using Eclipse Juno and Java.

I want to create a list and then store that list in another list so I can pass the list of lists to the server side. I have tried:

ArrayList<T> listAccountAndCubs = new ArrayList<Comparable>();
listAccountAndCubs.add(accountId);
listAccountAndCubs.add(sqlDateArchived);

However, I can not get the values "T" and "Comparable" correct. I tried "String" however that does not work for storing the date.

Once the above is correct how do I set up the list to contain "listAccountAndCubs"?

Thanks for any assistance,

Glyn

Upvotes: 1

Views: 122

Answers (4)

Devolus
Devolus

Reputation: 22094

If I understand you crrectly you want to have a list of Strings, and store this in another list?

List<String> sl = new ArrayList<String>();
List<List<String>>sls = new ArrayList<List<String>>();
sls.add(sl);
sl.add("String 1");

The value "T" is just a placeholder for the type, as the list is a generic interface, which can take any arbitrary object.

If you want to create a list of unspecified types, you would use

List<?>list = new ArrayList<?>();

Then you can add untyped objects to it, but in your case this is not neccessary.

Instead you can of course also create a list of comparables. Like this:

List<Comparable<String>>list = new ArrayList<Comparable<String>>();

Upvotes: 1

Ruchira Gayan Ranaweera
Ruchira Gayan Ranaweera

Reputation: 35557

List<ArrayList<Comparable>> listAccountAndCubs = new ArrayList<>();

or

List<String> l1=new ArrayList<>();
List<List<String>> l2=new ArrayList<>();

l1.add("a");
l2.add(l1); 

Upvotes: 2

RNJ
RNJ

Reputation: 15552

Sounds like you want something like this

List<List<String>> listAccountAndCubs = new ArrayList<List<String>>();

I would recomment using Google Guava library to clean the syntax a bit

List<List<String>> listAccountAndCubs = Lists.newArrayList();

Upvotes: 2

sanbhat
sanbhat

Reputation: 17622

this is how you can create a list

List<String> l = new ArrayList<String>();

this is how you can create list of list

List<List<Comparable>> listOfList = new ArrayList<List<Comparable>>();
listOfList.add(new ArrayList<Comparable>());
...

Upvotes: 4

Related Questions