| Author |
List with a sublist of Integers
|
nimo frey
Ranch Hand
Joined: Jun 28, 2008
Posts: 580
|
|
I need a data structure, that can hold such datas:
[[0, 92], [12, 85]]
You see, I have a list and in this list I have a list of Integers. But the sublist needs a size of 2 elements.
How can I declare such a datastructure?
Is this the best way?
One minor problem is, that I cannot fix the size of the sublist to 2.
|
 |
Seetharaman Venkatasamy
Ranch Hand
Joined: Jan 28, 2008
Posts: 5575
|
|
I really dont understand your question. can you please ellaborate?
do you mean you want to maintain key/value pair?
|
 |
Martin Vanyavchich
Ranch Hand
Joined: Sep 16, 2008
Posts: 241
|
|
|
If there will be no duplicate keys, you could use a Map implementation, Hashtable or something similar
|
SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3796
|
|
If you want to fix the size of the sublist, would an array be a better choice? The outer list wouldn't be constrainted to contain only arrays with two elements, but once added the arrays will always be the same size.
Alternatively, you could create a new class containing two ints that you add to a list. The best approach is going to depend on what the data structure actually represents.
|
 |
nimo frey
Ranch Hand
Joined: Jun 28, 2008
Posts: 580
|
|
Hello Martin,
thanks. This is what I need.
I tried to use this:
data.add(Arrays.asList(1,2));
But it does not work.
Your solution works and the size is limited to 2:
data.add(new int[]{12, 85});
thanks.
|
 |
 |
|
|
subject: List with a sublist of Integers
|
|
|