0

可能重复:
Java 关联数组

我在 PHP 中有一个数组,我需要一种在 Java 中描述相同类型数组的方法。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")
        )
    )
);
?>
4

1 回答 1

3

使用更新的示例,我将使用

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

之后,我将创建所需的索引,甚至使用 MySQL 之类的数据库或从普通 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

除非有充分的理由嵌套数据,否则我会使用复合键

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 作为一种面向对象的语言,最好尽可能使用对象来存储您的数据结构。

于 2012-08-14T12:04:04.073 回答