简体   繁体   中英

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. of course with a life boat, the stackoverflow.

Im struggling to make a multidimensional array in java where as in php it was simply possible $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

$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

Good practice for code like this in Java is not to use an untyped array for this, but to make actual typed objects:

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.

If the keys could be dynamic, though: the equivalent of PHP's associative arrays is a 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. 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:

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.

[Note: I haven't tested any of the above code yet. I think I got it right.]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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