Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用Java存储某些数据的最佳方式是什么?(数组与ArrayList)_Java_Arrays_Arraylist_Store - Fatal编程技术网

用Java存储某些数据的最佳方式是什么?(数组与ArrayList)

用Java存储某些数据的最佳方式是什么?(数组与ArrayList),java,arrays,arraylist,store,Java,Arrays,Arraylist,Store,因此,目前,我正在用java从一个XML文件中提取两个不同的属性(对于我的项目而言),它们彼此相关,并将它们打印到控制台。但是,我希望能够以引用一个值将检索其对应值的方式存储这些值。例如: Id: rId11 & Target: image3 Id: rId10 & Target: image2 Id: rId9 & Target: image1 有了这3个值,我想要一种存储每一行的方法,但是当我引用“rId”时,我可以得到它对应的“Target”值。我曾考虑使用数组或

因此,目前,我正在用java从一个XML文件中提取两个不同的属性(对于我的项目而言),它们彼此相关,并将它们打印到控制台。但是,我希望能够以引用一个值将检索其对应值的方式存储这些值。例如:

Id: rId11 & Target: image3
Id: rId10 & Target: image2
Id: rId9 & Target: image1

有了这3个值,我想要一种存储每一行的方法,但是当我引用“rId”时,我可以得到它对应的“Target”值。我曾考虑使用数组或arrayList,但我不确定哪一个更适合我的目的,也不确定我将如何准确地只引用一个值并获取另一个值。谁能给我一些建议吗?提前感谢。

使用a,Id为键,目标为值。注意,Map是一个接口,因此只定义行为。您需要选择一个特定的实现,例如HashMap。

如果您的键是唯一的,请使用一个

参考资料:


    • 你对自己想要的东西有点模棱两可。如果要基于给定键查找值,请将对存储在(更快)或(较慢但线程安全)中

      基本数组(以及更高级的基于and或的集合)不能使用现成的名称-值对。他们只是,嗯。。。名单。原语数组可以提供更高的性能,因为您可以避免创建对象,但更高级的列表类型集合可以更安全、更灵活

      尽管如此,听起来(?)您想要的是类型集合,而不是类型集合

      更新:顺便说一句,如果您使用地图,那么您仍然可以使用所有“rId”值的列表。它实际上是一个数据类型,但这只是列表的一个特殊表亲,不允许重复:

      Map<String, String> myMap = new HashMap<String, String>();
      myMap.put("rId11","image3");
      // ... additional put's for the other values
      
      Set<String> myRids = myMap.keySet();
      for(String rId : myRids) {
         // do whatever you want with each rId one-by-one, etc
         // You could also use "myRids.iterator()" to work with an Iterator instead
      }
      
      Map myMap=newhashmap();
      myMap.put(“rId11”、“image3”);
      // ... 其他值的附加put
      Set myRids=myMap.keySet();
      用于(字符串rId:myRids){
      //一个接一个地做你想做的事,等等
      //您也可以使用“myRids.iterator()”来处理迭代器
      }
      
      我认为java.util.HashMap更适合这个需求,尤其是在不需要排序的情况下

      // not sure what types these are but this would work better
      Map<String, String> m = new HashMap<String, String>();
      m.put("rId11", "image3");
      String other = m.get("rId11");
      
      //不确定这些类型是什么,但这样会更好
      Map m=新的HashMap();
      m、 put(“rId11”、“image3”);
      字符串other=m.get(“rId11”);
      
      如果目标值的“键”是唯一的,并且只有一个目标映射到它们,那么我建议改用java.util.HashMap。您可以通过传入键来检索任何目标值。此外,您还可以像遍历ArrayList一样遍历HashMap。

      公共类项{
      
      public class Item {
          private String id;
          private String target;
      
          public Item(String id, String target) {
              this.id = id;
              this.target = target;
          }
      
          public String getId() {
              return this.id;
          }
      
          public String getTarget() {
              return this.target;
          }
      }
      
       List<Item> items = new ArrayList<Item>();
       // or
       Map<String, Item> itemsIndexedById = new HashMap<String, Item>();
       // depending on your use-case
      
      私有字符串id; 私有字符串目标; 公共项(字符串id、字符串目标){ this.id=id; this.target=目标; } 公共字符串getId(){ 返回此.id; } 公共字符串getTarget(){ 返回此.target; } } 列表项=新建ArrayList(); //或 Map itemsIndexedById=new HashMap(); //取决于您的用例

      阅读。

      如果我理解正确,您希望能够查找类似“rId10”的内容,并获得值“image2”(仅此而已)


      如果是这样的话,我认为最好(就速度而言)和最简单的解决方案是哈希表(java.util.Hashtable)——也要小心使用java泛型(在Java1.5之后)。还可以查看。

      如果需要动态向其中添加元素,ArrayList非常有用。

      请不要推荐Vector和Hashtable。他们几年前就应该被弃用了。请阅读或阅读其中一篇文章,了解更多信息。哦,让我休息一下。是的,我99.999%肯定这家伙不是在写多线程应用程序。是的,即使在我自己的多线程开发中,我从2002年左右开始就没有使用过向量或哈希表。然而,对于新手来说,这是一个常见的困惑来源,他们永远不会真正将其从标准库中删除。。。所以我认为至少向新手解释一下那些看起来多余的类型是为了什么是有用的。。。最好使用HashMap而不是Hashtable,使用ArrayList而不是Vector。后一种类型提供了一些神秘级别的保护,但它对大多数开发都没有用处,并且会带来严重的性能损失。请不要推荐哈希表。几年前它就应该被弃用了。更多信息,请阅读或阅读其中一篇。非常感谢!我决定使用您的HashMap答案,但对我的具体操作做了一些轻微的修改。对于java来说是相当新的,所以我甚至在你提到它之前都没有考虑过映射。还要感谢为您提供意见的所有其他人。:)@语法:不客气。如果您是Java新手,您应该学习Java教程并阅读Josh Bloch的《高效Java》(第二版)。
      // not sure what types these are but this would work better
      Map<String, String> m = new HashMap<String, String>();
      m.put("rId11", "image3");
      String other = m.get("rId11");
      
      public class Item {
          private String id;
          private String target;
      
          public Item(String id, String target) {
              this.id = id;
              this.target = target;
          }
      
          public String getId() {
              return this.id;
          }
      
          public String getTarget() {
              return this.target;
          }
      }
      
       List<Item> items = new ArrayList<Item>();
       // or
       Map<String, Item> itemsIndexedById = new HashMap<String, Item>();
       // depending on your use-case