java中数组的合并以及与集合之间的相互转化

快来打我* 2022-06-09 14:42 236阅读 0赞

package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestArrayCopy {

/**
* @param args
*/
public static void main(String[] args) {
String[] str1s = {“haha”, “hehe”, “wahaha”, “哈哈”};
//String[] str2s = {“haha1”, “hehe1”, “wahaha1”};
List lis = new ArrayList();
lis.add(“黄山”);
lis.add(“九华山”);
lis.add(“九寨沟”);
lis.add(“桂林山水”);
String[] str2s = lis.toArray(new String[lis.size()]);

String[] strs = new String[str1s.length + str2s.length];

System.arraycopy(str1s, 0, strs, 0, str1s.length);
System.arraycopy(str2s, 0, strs, str1s.length, str2s.length);

for (String str:strs)
System.out.print(str + “ “);

testArrayToList();

}

private static void testArrayToList()
{
String[] strs = {“haha2”, “hehe2”, “wahaha2”, “哈哈2”};
List lis = Arrays.asList(strs);

for (String str : lis)
System.out.print(str + “ “);

}

}

发表评论

表情:
评论列表 (有 0 条评论,236人围观)

还没有评论,来说两句吧...

相关阅读