0

I have a very long structure in C that looks like..

something c = 
{
    {123, {1,2,3,4,5,5}},
    {333, {1,2,4}},
    {13}, {6,3,1,2,3,4,5,6,7,7,8}}
    // continue for 100 lines
};

I need this in Java, I don't know any C but this looks like a hashmap where the key is an integer and the value is an array of integers. I tried something like

HashMap<Integer, Integer[]> something =
{
    123:{1,2,3,4,5,5},
    333:{1,2,4},
    //continue for 100 lines
}

and this did not work.

EDIT: so the first number is an int called startX, and the array is full of short called startY. The code did something like

int tab = c[num];
int a = tab>startX;
short b = tab>startY;

so in Java I believe this is like

int a = something.get(startX);
int b = a[0];

I need to be able to traverse the data structure and was hoping I wouldn't have to manually type in all those lines :/

gallly
  • 1,201
  • 4
  • 27
  • 45
  • 6
    C does not have anything called a "hashmap", nor any data structure that acts like one. What you are looking at is probably an array of structures, but it's impossible to tell unless you say what `something` is. – Greg Hewgill Oct 21 '14 at 21:41
  • so it turns out the first number is just an int, and the other array are short – gallly Oct 21 '14 at 22:02
  • All three of the current answers still apply, just change the Integer[] to Short[] or short[] if you will. In fact, you don't even have to use Integer[], had they been ints, just int[] would do. Autoboxing takes care of the rest. – A.Grandt Oct 21 '14 at 22:09
  • int a = something.get(startX); must be int[] a = ... as you are retrieving an array. – A.Grandt Oct 21 '14 at 22:10
  • 1
    In Java, it's often preferable to use a `List` instead of an `Integer[]`, and you can use either as the value for a `Map`. – chrylis -cautiouslyoptimistic- Oct 21 '14 at 22:19

3 Answers3

2

You will have to put them in the HashMap manually.

Map<Integer, Integer[]> something = new Map<Integer, Integer[]>();
something.put(123, new Integer[]{1,2,3,4,5,5});
something.put(333, new Integer[]{1,2,4});
//etc
// Get each array by using something.get() with one of the Integer keys
something.get(123);
SamTebbs33
  • 5,507
  • 3
  • 22
  • 44
2

Try

HashMap<Integer, Integer[]> something = new HashMap<Integer, Integer[]>();
something.put(123, new Integer[]{1,2,3,4,5,5});
something.put(333, new Integer[]{1,2,4});
//continue for 100 lines

and you get the integer arrays with

Integer[] array = something.get(123);

If you are using Java 1.7 or above, you can omit the types in the HashMap instantiation.

HashMap<Integer, Integer[]> something = new HashMap<>();
A.Grandt
  • 2,242
  • 2
  • 20
  • 21
2
HashMap<Integer, Integer[]> something = new HashMap<Integer, Integer[]>() {
    {put(123, new Integer[]{1,2,3,4,5,5});}
    {put(333, new Integer[]{1,2,4});}
};
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70