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

6
  • 2
    Why are you not using an object for this? Commented Aug 29, 2013 at 17:03
  • possible duplicate of java multidimensional array Commented Aug 29, 2013 at 17:03
  • Look like json string Commented Aug 29, 2013 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. Commented Aug 29, 2013 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. Commented Aug 29, 2013 at 17:17

3 Answers 3

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 {
  ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

because the poster mentions he is new to java , are keywords 'final' and 'private' needed ? private final
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 
    } 

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.