0

Possible Duplicate:
Java associative-array

I have an array in PHP and I need a way to describe the same kind of array in Java. What's the equivalence for Java?

<?php
$data = array(
    "Artist 1" => array(
        "Album 1" => array(
            array("id", "title", "genre", "length", "year"),
            array("id", "title", "genre", "length", "year"),
            array("id", "title", "genre", "length", "year")
        ),
        "Album 2" => array(
            array("id", "title", "genre", "length", "year"),
            array("id", "title", "genre", "length", "year"),
            array("id", "title", "genre", "length", "year")
        )
    ),
    "Artist 2" => array(
        "Album 1" => array(
            array("id", "title", "genre", "length", "year"),
            array("id", "title", "genre", "length", "year")
        )
    )
);
?>
Community
  • 1
  • 1
MikkoP
  • 4,864
  • 16
  • 58
  • 106
  • 1
    What have you tried? - and what's wrong with zero-indexed arrays, why set the keys to numeric _strings_? – Elias Van Ootegem Aug 14 '12 at 11:59
  • Actually nothing, because I have no clue at all. – MikkoP Aug 14 '12 at 12:00
  • 3
    Already discussed on this thread http://stackoverflow.com/questions/5122913/java-associative-array – orak Aug 14 '12 at 12:02
  • 1
    Read the FAQ section of this site, then: a question has to show you've done your homework: IE research, reading manuals, tried to compile some code and show your attempts, regardless of how clunky they are... that gives us a place to start. If you haven't got a clue, I'd say google some basic Java tuts on the subject of arrays/structs/enums first – Elias Van Ootegem Aug 14 '12 at 12:02
  • @EliasVanOotegem It seems you edited your post. Well, the values aren't what there's going to be. The array is about to contain data about songs. I'm not sure what benefits you think you would achieve if I told you I've tried something like 1+1=11 and it haven't worked. – MikkoP Aug 14 '12 at 12:10
  • Not sure I quite understand what you mean by `1+1=11`, but if you want to use the numeric string indexes for concatenation, you should know that the operator for that is `.`, not `+`. The `+` will coerce (silently cast) the string to int (or float). `'1'+'1' === (int)'1' + (int) '1' === 2 !== ('1'.'1')` if you're don't need the indexes at all, just leave the array zero indexed, for performance reasons – Elias Van Ootegem Aug 14 '12 at 12:16
  • 1+1=11 was just an example of a problem. If I tried to calculate 1+1 and I told you that I've tried and I got 11 as an answer, how that would help you to solve my problem? I edited the original post if that makes more sense. – MikkoP Aug 14 '12 at 12:21

1 Answers1

3

With the updated example I would use

List<Record> records = ...
records.add(new Record("Artist 1", "Album 1", "id", "title", "genre", "length", "year"));
// etc

After that I would create an indexes required or even use a database like MySQL or load the data from a plain CSV

Artist 1,Album 1,id,title,genre,length,year
Artist 1,Album 1,id,title,genre,length,year
Artist 1,Album 2,id,title,genre,length,year
Artist 1,Album 2,id,title,genre,length,year
Artist 2,Album 1,id,title,genre,length,year

Unless there is a good reason to nest the data I would use a composite key

Map<String, List<String>> map = new LinkedHashMap<>();
map.put("1,1", Arrays.asList("abcdefgh", "abcdefgh", "abcdefgh"));
map.put("1,2", Arrays.asList("abcdefgh", "abcdefgh", "abcdefgh"));
map.put("2,1", Arrays.asList("abcdefgh", "abcdefgh"));

Java, being an Object Orientated language, it is preferable to use Objects to store your data structures where possible.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 2
    That's a Freudian slip I guess. :) "Useless" – Adam Arold Aug 14 '12 at 12:22
  • @MikkoP As long as you have a willingness to learn, I am willing to help. I get frustrated with people who ask for help but are not willing to listen. :) – Peter Lawrey Aug 14 '12 at 12:52
  • @PeterLawrey Of course I'm willing to learn. I have the project done with PHP but I want to learn more Java and that's why I'm doing it again. I get frustrated with people who don't know the answer but they have to write something just to get thumbs up. Keep up the good work, Peter! – MikkoP Aug 14 '12 at 12:56