简体   繁体   中英

How can i return a ArrayList and a File Array from one method?

I want to return a ArrayList and a File Array with one method. For more Background(isnt necessary to help me) you can read the rest.

I am coding my version for the Sokoban game. For that i have to get a List(in my case a ArrayList) with all the paths of the levels so i can get the information and I also need the File Array because i used in a previous version and it would take hours to change that. The input for the method is "level" at the moment. Thats the directory with the .xsd files.r

I cant rly change the things i want to return.

public static ArrayList<SokobanLevel> loadLevels(String dirPath) throws SokobanException {
    Path dir = 
        FileSystems.getDefault().getPath(dirPath).toAbsolutePath();
    File[] fileFolder = dir.toFile().listFiles();
    ArrayList<SokobanLevel> allLevel = new ArrayList<SokobanLevel>();
    for(int i = 0; i < fileFolder.length; i++) {
        SokobanLevel sl = new 
                SokobanLevel(fileFolder[i].getAbsolutePath());
        sl.setId(i);
        allLevel.add(sl);
    }
    //would like to also return File[] fileFolder
    return allLevel;
}

method works so far so good.

Just make a tuple like this:

Class Result {
   public static final ArrayList list;
   public static final File[] files;

   public Result(ArrayList list,File[] files){
       this.list =list;
       this.files = files;
   }

}

And use it like this: return new Result(list,files)

When you need to use the list:result.list.

When you need to use the files:result.files.


It's read-only because it's qualifies wiht final keyword.

You cannot return multiple objects from one method. You can put the different objects in an new class and then return that.

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