如何附加一个或多个ArrayList?

时间:2015-01-13 06:34:35

标签: java android arraylist append

我试图将一个或多个ArrayList值的数据附加到另一个ArrayList中是否可能,我可能知道如何?

示例代码如下:

private static final Pattern DIR_SEPORATOR = Pattern.compile("/");
    private ArrayList<File> fileList = new ArrayList<File>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] rootFolders = this.getStorageDirectories();

          if (rootFolders == null) {
           return;
          }
          int rl = rootFolders.length;

          File[] filelists = null;
        for (int i = 0; i < rl; i++) {
           String path = rootFolders[i];
           File target = new File(path + "/DCIM/");
           if (target.exists()) {
            filelists = target.listFiles();
               File[] filelist1  =  filelists;

           }
           File target2 = new File(path + "/DCIM/100ANDRO/");
           if (target2.exists()) {
               File[] filelist = target.listFiles();
            // Existing image to DB
            //IntialLoad(target2, Json_SupportedExtensionFromServer,CF,DP);
           }
          }

提前致谢。

3 个答案:

答案 0 :(得分:0)

您正在尝试“连接”两个数组(不是ArrayList s)。您需要使用ArrayUtils.addAll(),如下所述:

How can I concatenate two arrays in Java?

答案 1 :(得分:0)

Arraylistname.addAll(new ArraList())试试这段代码,我也用它来结合两个Arraylist

答案 2 :(得分:0)

在添加到ArrayList之前,需要将File []数组转换为ArrayList,如下所示:

fileList.addAll(Arrays.asList(filelist1));