0

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

CatLoves
  • 1
  • 1
  • 2
    Why are you not using an object for this? – Eric Stein Aug 29 '13 at 17:03
  • possible duplicate of [java multidimensional array](http://stackoverflow.com/questions/1067073/java-multidimensional-array) – Henry Keiter Aug 29 '13 at 17:03
  • Look like json string – Subhrajyoti Majumder Aug 29 '13 at 17:04
  • @Henry Keiter, sorry. this isn't a duplicate of your link. and that doesn't solve my problem. title may match but content differs. – CatLoves Aug 29 '13 at 17:12
  • Yes, I'm seeing that from the answers that are getting upvoted. I took "Im struggling to make a multidimensional array in java" and the fact that you provided no Java code to mean that you were stumped with how to create multidimensional Java arrays, but apparently that isn't the issue. – Henry Keiter Aug 29 '13 at 17:17
  • @ Subhrajyoti Majumder, sounds interesting. I need this array to store in session. is json a good practice? – CatLoves Aug 29 '13 at 17:23

3 Answers3

4

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 {
  ...
}
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • because the poster mentions he is new to java , are keywords 'final' and 'private' needed ? [private](http://www.tutorialspoint.com/java/java_access_modifiers.htm) [final](http://www.java-examples.com/java-final-variable-example) – Srinath Ganesh Aug 29 '13 at 17:14
1

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 
    } 
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
0

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 ArrayLists, 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.]

ajb
  • 31,309
  • 3
  • 58
  • 84