【问题标题】:Creating an ArrayList within an ArrayList在 ArrayList 中创建 ArrayList
【发布时间】:2011-10-08 00:12:52
【问题描述】:

因此,对于我正在开发的程序,我正在尝试创建一个可以解析文本文件的方法。使用文本文件中的值,在ArrayList 中创建一个ArrayList。到目前为止,这是我的代码:

public ArrayList<ArrayList<String>> getRels(String relsFile)
{
   String[] currentId;
   int i = -1;

   ArrayList<ArrayList<String>> relsList = new ArrayList<ArrayList<String>>();
   relsList.add(new ArrayList<String>());

   try
   {
      // Open the file that is the first 
      // command line parameter
      FileInputStream fstream = new FileInputStream(relsFile);
      BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
      String strLine;

      while ((strLine = br.readLine()) != null)
      {
         // Add values to array list
         currentId = strLine.split(",");
         relsList.get(++i).add(currentId[0]);
         //???????????          
      }   
      //Close the input stream
      in.close();
   }        
   catch (Exception e)
   {
      System.err.println("Error: " + e.getMessage());
   }
   return relsList; 
}

这是我正在查看的文本文件的示例。

rId13,image3
rId18,image8
rId26,image16
rId39,image29
rId21,image11
rId34,image24
rId7,image1
rId12,image2
rId17,image7
rId25,image15
rId33,image23
rId38,image28
rId16,image6
rId20,image10
rId29,image19
rId24,image14
rId32,image22
rId37,image27
rId15,image5
rId23,image13
rId28,image18
rId36,image26
rId19,image9
rId31,image21
rId14,image4
rId22,image12
rId27,image17
rId30,image20
rId35,image25

本质上,我希望能够拆分值,将所有rId 值存储在第一个ArrayList 中,然后在嵌套的ArrayList 中列出它们对应的image 值。但是,我有点卡住了,不知道此时该怎么做。有人可以帮我吗?感谢您提供的任何帮助。

【问题讨论】:

  • Map&lt;String, List&lt;String&gt;&gt; 将帮助您更多地满足您当前的需求。按照这个答案:stackoverflow.com/questions/1010879/…
  • +1 @Marcelo - 如果 rIdxx key 只有一张图片 value,甚至只是 Map&lt;String, String&gt;
  • 从示例数据来看,一个简单的Map&lt;String, String&gt; 就足够了,因为我在第一列中看不到任何重复项。

标签: java arraylist nested text-files nested-class


【解决方案1】:

我建议您使用调试器来查看到底发生了什么。

通过阅读代码,我猜它可以读取第一行,但在那之后会失败,因为您只添加了一个嵌套的 ArrayList,但尝试使用其中的许多。

最简单的做法是返回 Map,因为我假设第一列是唯一的。

public static Map<String, String> getRels(String relsFile) {
    Map<String, String> map = new LinkedHashMap<String, String>();
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader(relsFile));
        String line;
        while ((line = br.readLine()) != null) {
            String[] parts = line.split(",", 2);
            if (parts.length > 1)
                map.put(parts[0], parts[1]);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null) br.close();
        } catch (IOException ignored) {
        }
    }
    return map;
}

【讨论】:

  • 啊,是的,谢谢。我什至没有考虑使用 hashmap,因为我正在尝试解决我同事的代码,而他正在使用 ArrayList。绝对是一个更好的实现。感谢所有做出贡献的人。 :)
【解决方案2】:

创建两个数组列表。填充它们..然后将它们添加到您的其他数组列表中。

话虽如此,那一定是一个数组列表吗?你不能只拥有一个包含两个不同数组列表的常规对象吗?更好的是,也许您想考虑一个不同的数据结构本身,比如 Map ?

    try
    {
        // Open the file that is the first 
        // command line parameter
        FileInputStream fstream = new FileInputStream(relsFile);
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
        String strLine;

        ArrayList<String> list1 = new ArrayList<String>();
        ArrayList<String> list2 = new ArrayList<String>();

        while ((strLine = br.readLine()) != null)
        {
            // Add values to array list
            currentId = strLine.split(",");
            list1.add(currentId[0]);
            list2.add(currentId[1]);

        }     
        //Close the input stream
        in.close();

       relsList.add(list1);
       relsList.add(list2);
    }       

【讨论】:

    【解决方案3】:

    我认为执行此任务的更好方法是使用任何现有的库来读取 CSV 文件。这将使您的代码更简单。

    【讨论】:

      【解决方案4】:

      我同意 Peter L - 绝对使用 Map 实现。我使用 TreeMap 进行测试,因为它是一个排序的 Map,但您可以使用任何您想要的 Map。我还对此进行了测试以确保它有效。

      根据您的描述,我相信这就是您想要的:

      public TreeMap<String, ArrayList<String>> getRels(String relsFile)
      {
          String[] currentId;
          int i = -1;
      
          TreeMap<String, ArrayList<String>> relsList = new TreeMap<String, ArrayList<String>>();
      
          try
          {
              // Open the file that is the first 
              // command line parameter
              FileInputStream fstream = new FileInputStream(relsFile);
              BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
              String strLine;
      
              while ((strLine = br.readLine()) != null)
              {
                  // Add values to array list
                  currentId = strLine.split(",");
                  if(relsList.containsKey(currentId[0])) {
                      relsList.get(currentId[0]).add(currentId[1]);
                  } else {
                      relsList.put(currentId[0], new ArrayList<String>());
                      relsList.get(currentId[0]).add(currentId[1]);
                  }
      
              }     
              //Close the input stream
              in.close();
          }       
          catch (Exception e)
          {
              System.err.println("Error: " + e.getMessage());
          }
          return relsList;    
      }
      
      public static void main(String[] args) {
          ListReader lr = new ListReader();
          TreeMap<String, ArrayList<String>> l = lr.getRels("test.txt");
          for(String s : l.keySet()) {
              for(String k : l.get(s)) {
                  System.out.println("Key: " + s + " Value: " + k);
              }
          }
      }
      

      我在解析部分所做的是使用简单的 IF...ELSE 语句解析文件。它检查键是否已经存在(rId 部分),如果存在,它只是将右侧的值添加到现有的 ArrayList。如果没有,它会添加一个新键,然后创建一个 ArrayList,然后将值添加到其中。

      希望这是你想要的!

      【讨论】:

        猜你喜欢
        • 2012-02-25
        • 1970-01-01
        • 2020-03-21
        • 1970-01-01
        • 2017-04-08
        • 2015-04-03
        • 2013-07-05
        • 1970-01-01
        • 2021-11-20
        相关资源
        最近更新 更多