简体   繁体   English

Java中的多维数组。 这么复杂吗?

[英]Multi-dimensional array in Java. Is it that complex?

Im basically a php programmer now just wondering( or wandering!) on the shore of java sea. 我基本上是一个php程序员,现在只是想在Java的海岸上徘徊(或徘徊!)。 of course with a life boat, the stackoverflow. 当然是救生艇,stackoverflow。

Im struggling to make a multidimensional array in java where as in php it was simply possible $array["bla"]["blabla"]["blablabla"]... 我正在努力在Java中制作多维数组,就像在php中那样,完全有可能$array["bla"]["blabla"]["blablabla"]...

This what I want to achieve 这是我想要实现的

Array
(
    [user] => UserName
    [groups] => Array
        (
            [0] => group1
            [1] => group2
            [2] => group3
        )

    [categories] => Array
        (
            [0] => category1
            [1] => category2
            [2] => category3
        )

    [notification] => user notification string
    [departments] => Array
        (
            [0] => department1
            [1] => department2
            [2] => department3
            [3] => department4
        )

    [sub-deptmnt] => Array
        (
            [department1] => Array
                (
                    [0] => subdep1
                    [1] => subdep2
                )

            [department2] => Array
                (
                    [0] => another-subdep1
                    [1] => another-subdep2
                    [2] => another-subdep3
                )

        )

)

in php it is 在PHP中

$array["user"] = "UserName";
$array["groups"] = array("group1","group2","group3");
$array["categories"] =array("category1","category2","category3");
$array["notification"] = "user notification string";
$array["departments"] = array("department1","department2","department3","department4");
$array["sub-deptmnt"] = array("department1" => array("subdep1","subdep2"),"department2"=> array("another-subdep1","another-subdep2", "another-subdep3"));

Somebody please help me to move on.. 有人请帮助我继续前进..

Edit: To clarify desired array using php example 编辑:使用php示例来阐明所需的数组

Good practice for code like this in Java is not to use an untyped array for this, but to make actual typed objects: Java中这样的代码的优良作法是不要为此使用无类型的数组,而要制作实际的有类型的对象:

class Whatever {
  private final String username;
  private final List<Group> groups;
  ...
}
class Group {
  ...
}

Define your object like this 像这样定义你的对象

    class SampleModel{

        String userName;
        List<String> groups  ;
        List<String> categories;
        String notification;
        List<String> departments;
        Map<String,List<String>> sub_deptmnt;

        //getter and setter 
    } 

If you know ahead of time what the keys are going to be, then it's best to set up a class with fields and getter/setter methods the way Prabhakaran showed you. 如果您提前知道键将是什么,那么最好以Prabhakaran向您显示的方式设置带有字段和getter / setter方法的类。

If the keys could be dynamic, though: the equivalent of PHP's associative arrays is a Map<String,Object> . 但是,如果键可以是动态的:与PHP关联数组等效的是Map<String,Object>

Map<String,Object> arr = new HashMap<String,Object> ();

Then to create an array element: 然后创建一个数组元素:

arr.put ("user", userName);

and to retrieve it: 并获取它:

String userName = (String)(arr.get ("user"));

You can set array elements to arrays, or ArrayList s, or other Map<String,Object> maps, getting your multidimensional array. 您可以将数组元素设置为数组, ArrayList或其他Map<String,Object>映射,以获取多维数组。 The thing is, though, Java is strongly typed, which means the get method will return an Object, and you have to cast it to the type you expect it to be: 事实是,Java是强类型的,这意味着get方法将返回一个Object,并且您必须将其强制转换为期望的类型:

String[] categories = (String[])(arr.get ("categories"));

or 要么

ArrayList<String> categories = (ArrayList<String>)(arr.get ("categories"));

which will throw an exception if the object earlier stored for "categories" has the wrong type. 如果先前为“类别”存储的对象的类型错误,则将引发异常。

If nothing else, you should at least look into the collections tutorial , since it's stuff you'll definitely need to know about if you'll be working with Java. 如果没有其他问题,您至少应该研究一下collections教程 ,因为您绝对需要了解它是否可以使用Java。

[Note: I haven't tested any of the above code yet. [注意:我尚未测试以上任何代码。 I think I got it right.] 我想我做对了。]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM