0

So I have been searching and searching all day, but I cannot seem to find how to create my dictionary I created in Python and create something similar in Java. I have tried looking at ArrayList and List etc. But to no avail.

PIECESDICT = {1: [[0,0], [0,1], [0,2], [1,1]], 2: [[0,0], [1,0], [0,1]], 3: [[0,0], [0,1], [1,1]], 4: [[0,0], [0,1], [1,0], [1,1]], 5: [[0,0], [1,0], [2,0]]} 

So here is my Dictionary, and essentially what it does is just says from coords (0,0) being the top left point of each piece. It is a piece in and of itself.

For example Piece 1 would look like:

0
00
0

Similarly Piece 2 would look like:

00
0
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
KodyVanRy
  • 1,110
  • 1
  • 14
  • 25
  • 1
    You could try a HashMap. – Christian Tapia Jan 18 '14 at 22:15
  • Could you explain to me how to use that similar to a dictionary? – KodyVanRy Jan 18 '14 at 22:16
  • If you've been searching all day, you should work on your search terms. The first google hit for `Java dictionary` is the (admittedly obsolete) [Dictionary class](http://docs.oracle.com/javase/7/docs/api/java/util/Dictionary.html), and reading the class description would have pointed you towards `Map`. Searching `Java map` yields [this tutorial](http://docs.oracle.com/javase/tutorial/collections/interfaces/map.html) as the second hit, which is an extensive introduction to the topic. But actually, if all your keys are numbers from 1 upwards like in your example, you could just use a list... – l4mpi Jan 18 '14 at 22:23

4 Answers4

4

You could try a HashMap

The way you could use it:

Map<Integer, Integer[][]> map = new HashMap<Integer, Integer[][]>();
Integer[][] v1 = { { 0, 0 }, { 0, 1 }, { 0, 2 }, { 1, 1 } };
map.put(1, v1);
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
3

It looks like you want... a HashMap of ArrayLists of some kind of pair or coordinate object.

If you have a Pair class that accepts the coordinates as arguments (e.g. new Pair(0, 0)), then you can just do something like the following:

HashMap<Integer, ArrayList<Pair>> map = new HashMap<Integer, ArrayList<Pair>>();

ArrayList<Pair> first = new ArrayList<Pair>();
first.add(new Pair(1, 2));
first.add(new Pair(3, 4));

map.put(1, first);

// map == {1: [[1,2], [3,4]]}

HashMap can also accept a String (instead of Integer) as a key for the lists within.

Cat
  • 66,919
  • 24
  • 133
  • 141
  • Thank you that makes perfect sense! I don't know why I didn't look into something like that earlier. I was originally looking more so into an ArrayList of an ArrayList of an ArrayList, so thanks to everyone who helped. – KodyVanRy Jan 18 '14 at 22:21
2

what about HashMap? for example: map.put("name", "value");

ItayB
  • 10,377
  • 9
  • 50
  • 77
  • Yes a Hashmap will work, I do like the idea, but then how do I include the `[[0,0],[0,1],[0,2],[1,1]]` – KodyVanRy Jan 18 '14 at 22:19
  • You can choose what the Object in the value will be.. you can decide that it will array of Point (which you should define) – ItayB Jan 18 '14 at 22:20
0

If you need to move data from Python to Java, check out JSON.

In Python, you could use http://docs.python.org/3/library/json.html

And for Java: http://json.org/java/

dstromberg
  • 6,954
  • 1
  • 26
  • 27
  • 1
    How does JSON help here? The OP wanted to implement a data structure in Java and gave a Python structure as an example of what was needed. Where does it talk about moving data? – Martijn Pieters Jan 19 '14 at 00:03