关于Java:如何组合两个数组

How to Combine two array

本问题已经有最佳答案,请猛点这里访问。

我的应用程序中有两个数组,在本例中,我希望在应用程序中组合两个数组,但我尝试了并失败了。这是我的密码……请组合数组数据和数据2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Data[] data = {
        new Data(-79.400917f,43.661049f,"New New College Res",
               "Residence building (new concrete high-rise)","R.drawable.mr_kun"),
        new Data(-79.394524f,43.655796f,"Baldwin Street",
               "Here be many good restaurants!","R.drawable.mr_kun"),
        new Data(-79.402206f,43.657688f,"College St",
               "Lots of discount computer stores if you forgot a cable or need to buy hardware.","R.drawable.mr_kun"),    
        new Data(-79.390381f,43.659878f,"Queens Park Subway",
               "Quickest way to the north-south (Yonge-University-Spadina) subway/metro line","R.drawable.mr_kun"),

};

Data[] data2 = {
        new Data(-79.403732f,43.666801f,"Spadina Subway",
               "Quickest way to the east-west (Bloor-Danforth) subway/metro line","R.drawable.mr_kun"),
        new Data(-79.399696f,43.667873f,"St George Subway back door",
               "Token-only admittance, else use Spadina or Bedford entrances!","R.drawable.mr_kun"),
        new Data(-79.384163f,43.655083f,"Eaton Centre (megamall)",
               "One of the largest indoor shopping centres in eastern Canada. Runs from Dundas to Queen.","R.drawable.mr_kun"),
};

首先把你的String array转换成List而不是用addAllmetod来添加List

例子

1
2
3
ArrayList<String> first;
ArrayList<String> second;
second.addAll(first);


干得好。

1
2
3
Data[] mergedArray = new Data[data.length + data2.length];
System.arraycopy(data, 0, mergedArray, 0, data.length);
System.arraycopy(data2, 0, mergedArray, data.length, data2.length);


您可以轻松地使用ApacheCommonsLang库。然后您的代码将显示:

1
ArrayUtils.addAll(data, data2);

以下是此方法的说明:http://cAMOS.Apache .org/Outs/Orthon Lang/JavaDoc/API2.6/Org/Apache/Caluns/Lang/ArayUTILLS.html,HTML,AdvALL(Java.Lang.Obj[],java. Lang.Obj[])