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")
        )
    )
);
?>
7
  • 1
    What have you tried? - and what's wrong with zero-indexed arrays, why set the keys to numeric strings? Commented Aug 14, 2012 at 11:59
  • Actually nothing, because I have no clue at all. Commented Aug 14, 2012 at 12:00
  • 3
    Already discussed on this thread stackoverflow.com/questions/5122913/java-associative-array Commented Aug 14, 2012 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 Commented Aug 14, 2012 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. Commented Aug 14, 2012 at 12:10

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

That's a Freudian slip I guess. :) "Useless"
@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. :)
@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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.