简体   繁体   中英

Using something other than an ArrayList

I have a problem which requires me to create an array that will store the name of a file, and then its contents. I tried to use this, to create an Array ArrayList<String, String> fileContent = new ArrayList<String, String>(); but it calls an error, that there are an incorrect number of arguments. Whats the best way to get around this problem?

Would it be better to make two Arrays, one that stores the names, and one that stores the data in the file. Or is there another inbuilt thing inside of java that would be better to use?

create a custom class

class MyClass{
  String filename;
  String content;

}
// use methods as you want

then use array list for MyClass

ArrayList<MyClass> fileContent = new ArrayList<MyClass>();

If you are going to increase the properties you want to store, the answer by @while true is the correct one. If you want to store the name and the file only, you can create a HashMap like this.

HashMap<String, File> myMap = new HashMap<String, File>();

And insert elements like this:

myMap.put("filename",myFile);

Hope it helps.

Create a class that stores what you want, like this:

class Contents {
    String fileName;
    ArrayList<String> fileContent;

public Contents(String fileName){
    this.fileName = fileName;
    fileContent = new ArrayList<String>;
}
//Any Methods you need
}

and create an ArrayList like this:

ArrayList<Contents> = new ArrayList<Contents>;

This way you can store the fileName and it's contents.

The error you get is because an ArrayList can only contain one class, like String or in this example Contents.

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