简体   繁体   中英

Accessing array list elements from another class

I have a main class which has an ArrayList . This ArrayList has a set of values stored via iteration. I need to use this ArrayList (fileList & directoryList) value in another class. How can I achieve this? I do not want to move the ArrayList into another class.

package com.filehandler;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class FileFinder {

    public static void main(String[] args) {
        // TODO Auto-generated method stub


        int directoryCount = 0;

        /* Declare Lists for Files & Directories */
        List<String> fileList = new ArrayList<String>();
        List<String> directoryList = new ArrayList<String>();

        // enter code here

        /* Set file location */

        File myFolder = new File("O:\\The Ultimate Book Shelf\\Technical");
        File[] myFileList = myFolder.listFiles();

        /* Iterate the folder to get the details of Files & Folders */

        for (int count = 0; count < myFileList.length; count++) {
            if (myFileList[count].isFile()) {
                // System.out.println("File " + myFileList[count].getName());
                fileList.add(myFileList[count].getName());

            } else if (myFileList[count].isDirectory()) {
                directoryCount++;
                // System.out.println("Directory " +
                // myFileList[count].getName());
                directoryList.add(myFileList[count].getName());
            } else {
                System.out
                        .println("There are no files or directories in the mentioned path. Please verify the folder location.");
            }
        }
        System.out.println("Total Files : "
                + (myFileList.length - directoryCount));
        System.out.println("Total Directories :" + directoryCount);
        System.out.println(fileList);
        System.out.println(directoryList);

    }    
}

Your other class can either accept two ArrayLists in its own constructor or as parameters for a specific method, depending on what you are trying to do. By passing them as parameters you will actually pass a reference to the ArrayLists you created in you main function, therefore the other class will be able to read the values inside.

It depends how would the array be used in the second class. You have the following options.

  1. To use the array in a method in the second class.
  2. To use the array as a member in the second class.
  3. To use the array as a static member in the second class.

For 1, you can simply pass the array to the method. For 2, you can pass the array to a constructor and initialize the member variables in the class. For 3, you can set like SecondClass.fileList = myFileList;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM