I have an array like this:

array = [ 'charcaters' => [ 'characterName1' => [ 'blockingStance' => [ ['name' => '1.jpg', 'url' => 'http://site.com/sprites/block/1.jpg'], ['name' => '2.jpg', 'url' => 'http://site.com/sprites/block/2.jpg'], ['name' => '2.jpg', 'url' => 'http://site.com/sprites/block/3.jpg'] ], 'walkingStance' => [ ['name' => '1.jpg', 'url' => 'http://site.com/sprites/walk/1.jpg'], ['name' => '2.jpg', 'url' => 'http://site.com/sprites/walk/2.jpg'], ['name' => '2.jpg', 'url' => 'http://site.com/sprites/walk/3.jpg'] ], 'specialMoves' => [ ['name' => '1.jpg', 'url' => 'http://site.com/sprites/special/1.jpg'], ['name' => '2.jpg', 'url' => 'http://site.com/sprites/special/2.jpg'] ], ... ], 'characterName2' => [ ..... ], ... ], 'stages' => [ 'stageName1' => [ 'bridge' => [ ['name' => '1.jpg', 'url' => 'http://site.com/sprites/bridge/1.jpg'], ['name' => '2.jpg', 'url' => 'http://site.com/sprites/bridge/2.jpg'], ['name' => '2.jpg', 'url' => 'http://site.com/sprites/bridge/3.jpg'] ], 'street' => [ ['name' => '1.jpg', 'url' => 'http://site.com/sprites/street/1.jpg'], ['name' => '2.jpg', 'url' => 'http://site.com/sprites/street/2.jpg'], ['name' => '2.jpg', 'url' => 'http://site.com/sprites/street/3.jpg'] ], 'forest' => [ ['name' => '1.jpg', 'url' => 'http://site.com/sprites/forest/1.jpg'], ['name' => '2.jpg', 'url' => 'http://site.com/sprites/forest/2.jpg'] ], ... ], 'characterName2' => [ ..... ], ... ], 'other' => ... ...... ]; 

Many lovers obkakat PHP . However, there it is easy to create such an array and work with it. And it seems to me very convenient.

In Java, based on this data, my final object is a monster like this:

 Map<String, Map<String, Map<String, ArrayList<TreeMap<String, String>>>>> 

Where

 TreeMap<String, String> - это ΠΊΠΎΠ½Π΅Ρ‡Π½Ρ‹Π΅ ΠΊΠ»ΡŽΡ‡-Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ArrayList<TreeMap<String, String>> - список этих самых ΠΊΠ»ΡŽΡ‡Π΅ΠΉ ΠΈ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ Map<String, ArrayList<TreeMap<String, String>>> - ΠΊΠ»ΡŽΡ‡: Π½Π°Π·Π²Π°Π½ΠΈΠ΅ состояния ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°, Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ - список Π²Ρ‹ΡˆΠ΅. Map<String, Map<String, ArrayList<TreeMap<String, String>>>> - ΠΊΠ»ΡŽΡ‡: Π½Π°Π·Π²Π°Π½ΠΈΠ΅ ΠΎΠ±ΡŠΠ΅ΠΊΡ‚Π°, Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ - список состояний Map<String, Map<String, Map<String, ArrayList<TreeMap<String, String>>>>> - ΠΊΠΎΠ½Π΅Ρ‡Π½Ρ‹ΠΉ список. 

Horror.

Generally. Maybe I do something wrong and do it, but ...

Are there no means in Java to quickly build an associative array and preferably unlimited nesting? Is it possible to simplify this design with the help of those means so that there is no such monster? Or is it the only way?

  • 3
    I would be in your place, use JSON and then parsil it - Andrew Bystrov Sept
  • @AndrewBystrov yes, I also thought about it. Just all these mutated constructions for the same parsing a la obj.getJSONObject("someObject").getJSONArray("characters").get(0).getJSONArray("blockingStance").getJSONArray("name"). are frozen out)) - Alexey Shimansky
  • This is much better than the type of object you wrote in the question) - Andrew Bystrov
  • 2
    Map<String, Map<String, Map<String, ArrayList<TreeMap<String, String>>>>> - nobody does this. All that can be allocated to the object is allocated to the object. This makes it easy to write / read code, and test, and debug. - Nofate ♦

1 answer 1

It looks like an attempt to stretch an associative array where it is not needed, because in fact there is a clear structure. And Java loves OOP and classes. Essentially, you have some collections that describe image sequences; there are some characters who have, apparently, animation; There are scenes consisting of objects. This can be represented as:

http://i.imgur.com/W59zhlJ.png

 class ImageInfo { String name; URI url; } class Character { String name; List<ImageInfo> blockingStanceImages; List<ImageInfo> walkingStanceImages; List<ImageInfo> specialMovesImages; } class StageObject { String name; List<ImageInfo> images; } class Stage { String name Map<String, StageObject> objects; } class Context { Map<String, Character> characters; Map<String, Stage> stages; }