Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

List with a sublist of Integers

 
nimo frey
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I really dont understand your question. can you please ellaborate?

do you mean you want to maintain key/value pair?
 
Martin Vanyavchich
Ranch Hand
Posts: 241
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there will be no duplicate keys, you could use a Map implementation, Hashtable or something similar
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic