简体   繁体   English

Java中的多维数组(或其他结构)

[英]Multi-Dimensional Array(or other structure) in Java

I come from PHP, i'm developping a tool, and i need to use a multidimensional array ( or something other, map,..) My goal is to be able to have an ArrayList like that :我来自 PHP,我正在开发一个工具,我需要使用多维数组(或其他东西,map,..)我的目标是能够拥有这样的 ArrayList:

[["name1", "surname1", "age"], ["name1", "surname1", "age"]]
Or :
[["name" : "name1", "surname" : "surname1", "age" : "age1"], ["name" : "name2", "surname" : "surname2", "age" : "age2"]]

And to be able to add another list into this global list, without size limit.并且能够将另一个列表添加到这个全局列表中,没有大小限制。 Actually, i do not found that in Java, it's crazy xD.实际上,我没有发现在 Java 中,这是疯狂的 xD。

The goal after that it to filter (with something like in PHP ((multi sort) to be able to sort those arrays which areinside the global array.之后的目标是过滤(使用类似于 PHP 中的((多排序))能够对全局数组内的那些数组进行排序。

( I need to sort a key of those list,in order to get the most recent, and then i save the first list which is the most recent ). (我需要对这些列表的键进行排序,以获得最新的,然后我保存最新的第一个列表)。

So actually, i only arrive to get an arraylist.. but not multidimensionnal..所以实际上,我只是为了得到一个数组列表..但不是多维..

Thanks for tips :) (I use spring boot )感谢您的提示:)(我使用弹簧靴)

EDIT 1 :编辑 1:

HashMap<String, String> myhash = new HashMap<String, String>();
        myhash.put("Cat1", "james");
        myhash.put("Cat2", "adams");
        myhash.put("Cat3", "turk");
        System.out.println(myhash);
        // {Cat3=turk, Cat2=adams, Cat1=james}
        // I Would like : [{Cat3=turk, Cat2=adams, Cat1=james}, {Cat3=turkother, Cat2=adamsother, Cat1=jamesother}, {Cat3=fred, Cat2=ded, Cat1=tp}]

You mean HashMap<String, ArrayList<String>> and than use filter on the HashMap.keyset() .你的意思是HashMap<String, ArrayList<String>>而不是在HashMap.keyset()上使用过滤器。

But don't know if this is exactly what your are looking for.但不知道这是否正是您要寻找的。

Two notes:两个注意事项:

  1. This is basic Java.这是基本的Java。 No Spring required.不需要弹簧。
  2. If you make a nice Java object with the properties you are looking for, perhaps filtering will be more easier... But don't know.如果你用你正在寻找的属性制作一个很好的 Java 对象,也许过滤会更容易......但不知道。 I would create an object Person with the required properties, store it in an ArraysList and than use this object further.我将创建一个具有所需属性的对象Person ,将其存储在ArraysList然后进一步使用该对象。

Update:更新:

    HashMap<String, String> myhash = new HashMap<String, String>();
    myhash.put("Cat1", "james");
    myhash.put("Cat2", "adams");
    myhash.put("Cat3", "turk");
    System.out.println(myhash);
    // {Cat3=turk, Cat2=adams, Cat1=james}
    // I Would like : [{Cat3=turk, Cat2=adams, Cat1=james}, {Cat3=turk, Cat2=adams, Cat1=james}]

    ArrayList<HashMap<String, String>> lst = new ArrayList<>();
    lst.add(myhash);
    lst.add(myhash);
    System.out.println(lst); //[{Cat3=turk, Cat2=adams, Cat1=james}, {Cat3=turk, Cat2=adams, Cat1=james}]

Remark:评论:

  1. take care that myhash is added twice ==> if you change on the one field ==> changes on the other field is done as well注意myhash被添加两次 ==> 如果你改变了一个字段 ==> 另一个字段的更改也完成了
  2. I don't see much value in this sample.我看不出这个样本有多大价值。 Especially since the Map is added twice.特别是因为地图被添加了两次。
  3. Take a look at the different samples for streams - for filtering and sorting.查看流的不同样本 - 用于过滤和排序。 This might be useful.这可能有用。
  4. If there are more qs I would guess its better to create a different q.如果有更多的 qs,我想创建一个不同的 q 会更好。 I think that's it ... Feel free to ask ...我想就是这样......随意问......

以下是在 Java 中获得列表列表的方式。

ArrayList<ArrayList<String>> myList = new ArrayList<>();

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

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