如何将字符串数组添加到数组列表 [英] How to add string arrays to array list
问题描述
我正在尝试创建数组的数组列表.完成后,我希望数组列表如下所示:
I'm trying to create an array list of arrays. When I'm done, I want the array list to look like this:
[ [Mary] , [Martha,Maggie,Molly] ]
我试图通过首先将Mary"定义为长度为 1 的字符串数组,并将Martha、Maggie、Molly"定义为长度为 3 的字符串数组来实现这一点.然后我尝试将这些数组添加到数组列表中.但遗憾的是,数组列表将不接受数组.看一看:
I'm trying to accomplish this by first defining "Mary" as a string array of length 1, and defining "Martha, Maggie, Molly" as a string array of length three. Then I attempt to add these arrays to an array list. But alas, the array list will not accept the arrays. Take a look:
String[] s1 = new String[1];
String[] s3 = new String[3];
ArrayList<String[]> list1 = new ArrayList<String[]>();
s1[0]="Mary";
s3[0]="Martha";
s3[1]="Maggie";
s3[2]="Molly";
list1.add(s1[0]);
list1.add(s3[0]);
list1.add(s3[1]);
list1.add(s3[2]);
关于如何将这些数组添加到 list1 以形成数组的数组列表的任何想法?
Any ideas on how I can add these arrays to list1, in order to form an array list of arrays?
推荐答案
您将单个字符串添加到 ArrayList 而不是您创建的 String 数组.
You're adding the individual strings to the ArrayList instead of the String arrays that you created.
试着做:
list1.add(s1);
list1.add(s3);
这篇关于如何将字符串数组添加到数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!