英文:
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 [] = {"A", "B", "C"};
String array2 [] = {"D", "E"};
String array3 [] = {"F", "G", "H", "I"};
I want to store all this arrays into List of String array.
List<String> 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 [] = {"A", "B", "C"};
String array2 [] = {"D", "E"};
String array3 [] = {"F", "G", "H", "I"};
List<String> 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<String>
, you can do this:
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]);
Output:
[A, B, C]
[D, E]
[F, G, H, I]
A better way would be using 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);
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 = {"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]);
}
答案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 [] = {"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));
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 [] = {"A", "B", "C"};
String array2 [] = {"D", "E"};
String array3 [] = {"F", "G", "H", "I"};
List<String[]> allArrays;
allArrays = new LinkedList<>();
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论