Reputation: 112
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?
Upvotes: 0
Views: 163
Reputation: 426
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.
Upvotes: 1
Reputation: 490
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.
Upvotes: 0
Reputation: 856
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>();
Upvotes: 3