问题描述
我试图创建一个数组的数组列表。当我做,我想数组列表看起来是这样的:
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] ]
我想首先定义玛丽作为长度为1的字符串数组,并定义玛莎,张曼玉,莫莉为长度为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,而不是您所创建的字符串数组。
You're adding the individual strings to the ArrayList instead of the String arrays that you created.
尝试只是在做:
list1.add(s1);
list1.add(s3);
这篇关于如何添加字符串数组的数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!