Shaydoth
Shaydoth

Reputation: 112

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?

Upvotes: 0

Views: 163

Answers (3)

Angel Guevara
Angel Guevara

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

Meik Vtune
Meik Vtune

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

while true
while true

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

Related Questions