将列表列表转换为字符串数组

时间:2014-08-01 04:47:34

标签: java arraylist arrays

我想将列表列表转换为两个单独的字符串数组。

我想将 taglist 转换为字符串数组 TagArray [] ,将 tokenlist 转换为名为 TokenArray 的数组。< / p>

我尝试了很多方法,但有很多方法可以将转换为ArrayList转换为2D数组,但我找不到任何方法将列表列表转换为字符串数组。任何人都可以帮我这样做。

标记表

 [[NON, NON], [NON, NON ], [NON, NON, NON], [NON, NON] ,[ENTITY, ENTITY]]

tokenlist

[[John, ran], [John, jumped], [The, dog, jumped], [Mary, sat], [Finland, India]]

我尝试了以下方式

 for(int i=0;i<tokenlist.size();i++)
    {
        String[] words = tokenlist.get(i);


    }

当我使用上述方式时,我正在获取输出。但问题是我必须同时从tokenlist和taglist中获取第i个值

OR

我必须将其转换为3D阵列,其格式如下

  static final String[][][] WORDS_TAGS = new String[][][]

 {

    { { "John", "ran" },                { "NON", "NON" } },

    { { "John", "jumped"},              { "NON", "NON "} },
    { { "The", "dog", "jumped"},        { "NON", "NON", "NON" } },

    { { "Mary", "sat"},                 { "NON", "NON"} },
    { { "Finland","India" },           { "ENTITY","ENTITY" } },

};

3 个答案:

答案 0 :(得分:1)

试试这个

    List<List<String>> l1 = Arrays.asList(Arrays.asList("NON", "NON"),
            Arrays.asList("NON", "NON"),
            Arrays.asList("NON", "NON", "NON"),
            Arrays.asList("NON", "NON"), Arrays.asList("ENTITY", "ENTITY"));
    List<List<String>> l2 = Arrays
            .asList(Arrays.asList("John", "ran"),
                    Arrays.asList("John", "jumped"),
                    Arrays.asList("The", "dog", "jumped"),
                    Arrays.asList("Mary", "sat"),
                    Arrays.asList("Finland", "India"));
    String[][][] a = new String[l1.size()][][];
    for (int i = 0; i < l1.size(); i++) {
        a[i] = new String[][] {
                l2.get(i).toArray(new String[l2.get(i).size()]),
                l1.get(i).toArray(new String[l1.get(i).size()]) };
    }
    System.out.println(Arrays.deepToString(a));

输出

[[[John, ran], [NON, NON]], [[John, jumped], [NON, NON]], [[The, dog, jumped], [NON, NON, NON]], [[Mary, sat], [NON, NON]], [[Finland, India], [ENTITY, ENTITY]]]

答案 1 :(得分:0)

   String[] words = new String[20];
   int index = 0;
   for(int i=0;i<L2.size();i++)
   {
      List l5 = (List)L2.get(i);
      for(int j=0;j<l5.size();j++){
      words[index] = (String)l5.get(j);
      index++;
      }
   }

你需要两个for循环。一个用于外部,另一个用于内部

答案 2 :(得分:0)

试试这个

    String[][][] newArray = new String[taglist.size()][2][];
    for(int i=0;i<taglist.size();i++){
       String[] arr=tokenlist.get(i).toArray(new String[tokenlist.get(i).size()]);
       newArray[i][0]= arr;
       arr= taglist.get(i).toArray(new String[taglist.get(i).size()]);
       newArray[i][1]= arr;
    }