简体   繁体   English

java中的多级关联数组,如php

[英]Multilevel Associative array in java like php

For development of web services, i have to make multilevel Associative array as follows 对于Web服务的开发,我必须按如下方式制作多级关联数组

Array
(
    [0] => Array
        (
            [id] => 1100
            [content] => This is content
            [creator_info] => Array
                (
                    [id] => 1
                    [fname] => abc
                    [lname] => xyz
                )

            [tag_info] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [name] => my
                        )

                    [1] => Array
                        (
                            [id] => 4
                            [name] => you
                        )

                    [2] => Array
                        (
                            [id] => 5
                            [name] => your
                        )

                )

            [created_date] => 14 JAN 2012
        )

    [1] => Array
        (
            [id] => 1101
            [content] => This is content1
            [creator_info] => Array
                (
                    [id] => 2
                    [fname] => abc1
                    [lname] => xyz1
                )

            [tag_info] => Array
                (
                    [0] => Array
                        (
                            [id] => 35
                            [name] => wer
                        )

                    [1] => Array
                        (
                            [id] => 47
                            [name] => cvb
                        )

                    [2] => Array
                        (
                            [id] => 51
                            [name] => mnv
                        )

                )

            [created_date] => 15 JAN 2012
        )

)

with referencing this question, i am using ArrayList . 引用这个问题,我正在使用ArrayList

as follows 如下

Java doesn't have associative arrays like PHP does. Java没有像PHP那样的关联数组。

There are various solutions for what you are doing, but it depends on how you want to look up the information. 您正在做的事情有各种解决方案,但这取决于您希望如何查找信息。 You can easily write a class that holds all your information and store instances of them into an ArrayList. 您可以轻松编写一个包含所有信息的类,并将它们的实例存储到ArrayList中。

public class Foo{
    public String name, fname;

    public Foo(String name,String fname){
        this.name=name;
        this.fname=fname;
    }
}

And then... 然后...

List<Foo> foos = new ArrayList<Foo>();
foos.add(new Foo("demo","fdemo");
foos.add(new Foo("test","fname");

So you can access them like... 所以你可以像...一样访问它们

foos.get(0).name;
=> "demo"

This keeps me happy for single level. 这让我对单级感到高兴。 If i keeps continue with it, this looks like as a complicated procedure. 如果我继续使用它,这看起来像一个复杂的过程。

I wants something simple procedure with high speed. 我想要一些高速的简单程序。

Last 3 days i am working in it and i got the success for following 最近3天我在工作,我获得了成功跟随

Map<String,Object> myMap1 = new HashMap<String, Object>();
List<Map<String , Object>> myMap  = new ArrayList<Map<String,Object>>();
Map<String,String> user_info = new HashMap<String, String>();
user_info.put("fname", "xyz");
user_info.put("lname", "pqr");
myMap1.put("id", 20);
myMap1.put("content", "This is content");
myMap1.put("creator_info", user_info);
myMap1.put("timestamp", "11 Jan 2012");
myMap.add(0,myMap1);

still not getting that how to do it for tags_info 仍然没有得到如何为tags_info做到这tags_info

is there any library for this thing....? 这个东西有没有图书馆....?

Java does have kind of associative arrays. Java确实有一些关联数组。 They are called Maps (see implementations of java.util.Map). 它们被称为Maps(请参阅java.util.Map的实现)。 With map you can associate value to key, eg 使用map,您可以将值与键相关联,例如

Map<String, String> mymap = new HashMap<String, String>();
mymap.put("one", "1");
mymap.put("two", "2"); 
// etc.

So to solve your problem you can create complex data structure that is a combination of arrays, lists (see java.util.List) and maps. 因此,要解决您的问题,您可以创建复杂的数据结构,它是数组,列表(请参阅java.util.List)和映射的组合。

But your structure looks too complicated and will not be easily manageable. 但是您的结构看起来太复杂,并且不易管理。 So, your suggestion is good. 所以,你的建议很好。 Create classes like Foo, probably create yet another that holds several Foo instances etc. 创建像Foo这样的类,可能会创建另一个包含多个Foo实例等的类。

This is old I know but, I felt I just have to reply for anyone else reading this. 我知道这已经过时了,但我觉得我只需回复其他读这篇文章的人。 Java is an Object Oriented language so think of your data structure in terms of 'objects'. Java是面向对象的语言,因此请根据“对象”来考虑您的数据结构。 Forget deep array constructs that PHP leads developers to create. 忘记PHP引导开发人员创建的深层数组结构。

When moving from a language like PHP to Java you have to leave the 'PHP' way of doing things and embrace objects. 当从PHP这样的语言转移到Java时,你必须保留“PHP”的做事方式并拥抱对象。

public class Content {
    String content;
    int id;
    List<Tag> getTags() {...}
    List<Creator> getCreators() {...}

}
public class Tag {
    int id;
    String name;
}
public class Creator {
    String fname;
    String lname;
}

When using the objects for example do the following... 例如,在使用对象时,请执行以下操作...

List<Creator> creators = content.getCreators();
List<Tag> tags = content.getTags();

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

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