2011-07-18 185 views
1

因此,对于我正在处理的程序,我试图创建一个将通过文本文件解析的方法。使用文本文件中的值,在ArrayList内创建一个ArrayList。这里是我的代码到目前为止:在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,然后列出其相应的image值在嵌套ArrayList。但是,我有点卡住,不知道该怎么做。有人可以帮我吗?我感谢提供的任何帮助。

+3

'Map >'可以更好地满足您的当前需求。按照这个答案:http://stackoverflow.com/questions/1010879/best-way-to-create-a-hashmap-of-arraylist/1011072#1011072 – Marcelo

+0

+1 @Marcelo - 甚至只是一个'地图<字符串,字符串>'如果rIdxx'键只有一个图像'值' – andyb

+0

从示例数据中,一个简单的'Map '就足够了,因为在第一列中没有看到任何重复。 –

回答

5

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

从阅读代码我猜想它读取第一行好,但在该点之后失败,因为你只添加一个嵌套的ArrayList,但尝试使用它们中的许多。

最简单的事情就是返回地图< String,String>,因为我认为第一列是唯一的。

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; 
} 
+0

啊,是的,谢谢。我甚至没有考虑使用hashmap,因为我试图解决我的同事代码,并且他正在使用ArrayList。绝对是一个更好的实现。感谢所有贡献的人。 :) –

1

我认为做这个任务的更好方法是使用任何已有的库从CSV文件中读取。这会让你的代码更简单。

3

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

这就是说,这是否必须是一个数组列表?你不能只有一个包含两个不同的数组列表的常规对象吗?甚至更好,也许你想考虑一个不同的数据结构本身就像一个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); 
    }  
1

我同意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,然后它将该值添加到。

希望这是你想要的!

相关问题