File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes List with a sublist of Integers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "List with a sublist of Integers" Watch "List with a sublist of Integers" New topic

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

I no good English.
Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

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:


But it does not work.

Your solution works and the size is limited to 2:

data.add(new int[]{12, 85});

I agree. Here's the link:
subject: List with a sublist of Integers
jQuery in Action, 3rd edition