• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

List with a sublist of Integers

 
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If there will be no duplicate keys, you could use a Map implementation, Hashtable or something similar
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic