如何将 String Array[] 元素转换/添加到 List [] 数组中

huangapple go评论75阅读模式
英文:

How to Convert/add String Array[] elements into List<String> [] Array

问题

String array1[] = {"A", "B", "C"};
String array2[] = {"D", "E"};
String array3[] = {"F", "G", "H", "I"};

List<String>[] allArrays = new ArrayList[3];

allArrays[0] = new ArrayList<>(Arrays.asList(array1));
allArrays[1] = new ArrayList<>(Arrays.asList(array2));
allArrays[2] = new ArrayList<>(Arrays.asList(array3));

System.out.println(allArrays[0]);
System.out.println(allArrays[1]);
System.out.println(allArrays[2]);
英文:

I have three arrays of String

String array1 [] = {&quot;A&quot;, &quot;B&quot;, &quot;C&quot;};
String array2 [] = {&quot;D&quot;, &quot;E&quot;};
String array3 [] = {&quot;F&quot;, &quot;G&quot;, &quot;H&quot;, &quot;I&quot;};

I want to store all this arrays into List of String array.

List&lt;String&gt; allArrays[]

My output List<String> allArrays[] should contain

allArrays[0]= [A,B,C]
allArrays[1]= [D,E]
allArrays[2]= [F,G,H,I]

I have tried below piece of code but getting null pointer exception

String array1 [] = {&quot;A&quot;, &quot;B&quot;, &quot;C&quot;};
String array2 [] = {&quot;D&quot;, &quot;E&quot;};
String array3 [] = {&quot;F&quot;, &quot;G&quot;, &quot;H&quot;, &quot;I&quot;};

List&lt;String&gt; allArrays[] =null;

allArrays[0].addAll(Arrays.asList(array1));
//Null pointer exception here
allArrays[1].addAll(Arrays.asList(array2));
allArrays[2].addAll(Arrays.asList(array3));

System.out.println(b[0]);
System.out.println(b[1]);
System.out.println(b[2]);

答案1

得分: 1

你的代码存在问题,没有正确地创建和添加到列表中。由于你需要一个包含 List<String>array,你可以这样做:

String array1[] = {"A", "B", "C"};
String array2[] = {"D", "E"};
String array3[] = {"F", "G", "H", "I"};
List<String>[] allArrays = new ArrayList[3];

List<String> list = new ArrayList<String>();
Collections.addAll(list, array1);
allArrays[0] = list;
list = new ArrayList<String>();
Collections.addAll(list, array2);
allArrays[1] = list;
list = new ArrayList<String>();
Collections.addAll(list, array3);
allArrays[2] = list;

System.out.println(allArrays[0]);
System.out.println(allArrays[1]);
System.out.println(allArrays[2]);

输出结果:

[A, B, C]
[D, E]
[F, G, H, I]

一个更好的方式是使用 List<List<String>>

String array1[] = {"A", "B", "C"};
String array2[] = {"D", "E"};
String array3[] = {"F", "G", "H", "I"};
List<List<String>> allArrays = new ArrayList<>(); 

allArrays.add(Arrays.asList(array1));
allArrays.add(Arrays.asList(array2));
allArrays.add(Arrays.asList(array3));
		        
System.out.println(allArrays);

输出结果:

[[A, B, C], [D, E], [F, G, H, I]]
英文:

You are not creating and adding to the list correctly. Since you need an array of List&lt;String&gt;, you can do this:

String array1 [] = {&quot;A&quot;, &quot;B&quot;, &quot;C&quot;};
String array2 [] = {&quot;D&quot;, &quot;E&quot;};
String array3 [] = {&quot;F&quot;, &quot;G&quot;, &quot;H&quot;, &quot;I&quot;};
List&lt;String&gt;[] allArrays = new ArrayList[3]; 

List&lt;String&gt; list = new ArrayList&lt;String&gt;();
Collections.addAll(list, array1);
allArrays[0] = list;
list = new ArrayList&lt;String&gt;();
Collections.addAll(list, array2);
allArrays[1] = list;
list = new ArrayList&lt;String&gt;();
Collections.addAll(list, array3);
allArrays[2] = list;
	    
System.out.println(allArrays[0]);
System.out.println(allArrays[1]);
System.out.println(allArrays[2]);

Output:

[A, B, C]
[D, E]
[F, G, H, I]

A better way would be using List&lt;List&lt;String&gt;&gt;:

String array1 [] = {&quot;A&quot;, &quot;B&quot;, &quot;C&quot;};
String array2 [] = {&quot;D&quot;, &quot;E&quot;};
String array3 [] = {&quot;F&quot;, &quot;G&quot;, &quot;H&quot;, &quot;I&quot;};
List&lt;List&lt;String&gt;&gt; allArrays = new ArrayList&lt;&gt;(); 

allArrays.add(Arrays.asList(array1));
allArrays.add(Arrays.asList(array2));
allArrays.add(Arrays.asList(array3));
		        
System.out.println(allArrays);

Output:

[[A, B, C], [D, E], [F, G, H, I]]

答案2

得分: 0

试一下这个:

String[] array1 = {"A", "B", "C"};
String[] array2 = {"D", "E"};
String[] array3 = {"F", "G", "H", "I"};

List<String>[] listArr = new ArrayList[3];
listArr[0] = new ArrayList<>(Arrays.asList(array1));
listArr[1] = new ArrayList<>(Arrays.asList(array2));
listArr[2] = new ArrayList<>(Arrays.asList(array3));

for (int i = 0; i < 3; i++) {
    System.out.println(listArr[i]);
}
英文:

Try this:

String[] array1 = {&quot;A&quot;, &quot;B&quot;, &quot;C&quot;};
String[] array2 = {&quot;D&quot;, &quot;E&quot;};
String[] array3 = {&quot;F&quot;, &quot;G&quot;, &quot;H&quot;, &quot;I&quot;};

List&lt;String&gt;[] listArr = new ArrayList[3];
listArr[0] = new ArrayList&lt;&gt;(Arrays.asList(array1));
listArr[1] = new ArrayList&lt;&gt;(Arrays.asList(array2));
listArr[2] = new ArrayList&lt;&gt;(Arrays.asList(array3));

for (int i = 0; i &lt; 3; i++) {
    System.out.println(listArr[i]);
}

答案3

得分: 0

尝试一下。

String array1[] = {"A", "B", "C"};
String array2[] = {"D", "E"};
String array3[] = {"F", "G", "H", "I"};

List<String>[] allArrays = new List[] {
    Arrays.asList(array1),
    Arrays.asList(array2),
    Arrays.asList(array3)
};

System.out.println(Arrays.toString(allArrays));

输出结果:

[[A, B, C], [D, E], [F, G, H, I]]
英文:

Try this.

String array1 [] = {&quot;A&quot;, &quot;B&quot;, &quot;C&quot;};
String array2 [] = {&quot;D&quot;, &quot;E&quot;};
String array3 [] = {&quot;F&quot;, &quot;G&quot;, &quot;H&quot;, &quot;I&quot;};

List&lt;String&gt;[] allArrays = new List[] {
    Arrays.asList(array1),
    Arrays.asList(array2),
    Arrays.asList(array3)
};

System.out.println(Arrays.toString(allArrays));

output

[[A, B, C], [D, E], [F, G, H, I]]

答案4

得分: -1

当然会得到空指针异常,因为你需要创建一个新的列表。如果你将列表设置为null,你并没有创建一个空列表,你只是拥有一个无法进行任何操作的对象。

String array1[] = {"A", "B", "C"};
String array2[] = {"D", "E"};
String array3[] = {"F", "G", "H", "I"};

List<String[]> allArrays;

allArrays = new LinkedList<>();

allArrays.add(array1);
allArrays.add(array2);
allArrays.add(array3);

String[] array4 = allArrays.get(0);
String[] array5 = allArrays.get(1);
String[] array6 = allArrays.get(2);

System.out.println(array4);
System.out.println(array5);
System.out.println(array6);

如果你使用LinkedList应该能正常工作。顺便问一下,你在使用IDE吗?当我将你的代码放入Eclipse或其他任何IDE时,会有许多警告出现,所以空指针异常并不是你的代码中唯一不起作用的部分。

英文:

Of course you get a null Pointer Exception, because you need to creat a new List. If you set your List to null, you dont create a empty List, you just have a Object, which you can do nothing with.

    String array1 [] = {&quot;A&quot;, &quot;B&quot;, &quot;C&quot;};
	String array2 [] = {&quot;D&quot;, &quot;E&quot;};
	String array3 [] = {&quot;F&quot;, &quot;G&quot;, &quot;H&quot;, &quot;I&quot;};

	List&lt;String[]&gt; allArrays;
	
	allArrays = new LinkedList&lt;&gt;();


	allArrays.add(array1); 
	allArrays.add(array1); 
	allArrays.add(array1); 
	
	String[] array4 = allArrays.get(0);
	String[] array5 = allArrays.get(1);
	String[] array6 = allArrays.get(2);

	System.out.println(array4);
	System.out.println(array5);
	System.out.println(array6);

It should work if you do it with a LinkedList. By the way, are you using a IDE, when I put your code in Eclipse or any other IDE I get many Warnings, so the NullPointerException wasnt the only that doesnt work in your code.

huangapple
  • 本文由 发表于 2020年8月18日 16:32:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63464777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定