gpt4 book ai didi

java - 将列表的列表转换为字符串数组

转载 作者:太空宇宙 更新时间:2023-11-04 06:38:06 24 4
gpt4 key购买 nike

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

我想将 taglist 转换为字符串数组 TagArray[] 并将 tokenlist 转换为名为 TokenArray 的数组。

我尝试了很多方法,但是有很多方法可以将将 ArrayList 转换为二维数组,但我找不到任何方法将列表列表转换为字符串数组。谁能帮我做到这一点。

标签列表

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

token 列表

[[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 个值

或者

我必须将其转换为具有以下格式的 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" } },

};

最佳答案

试试这个

    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]]]

关于java - 将列表的列表转换为字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25073015/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com