我一直在尝试使用以下代码在Java中显示文件夹列表:
class Folder {
boolean isFile;
String folderName;
List<Folder> subFolders = new ArrayList<Folder>;
}
class ListF {
public static void main(String args[]) {
File fname = new File("C:/hello");
Folder obj = new Folder();
if (fname.isDirectory()) {
File[] fileNames;
fileNames = fname.listFiles();
for (i = 0; i < fileNames.length; i++) {
obj.subFolders.add(fileNames[i]);
}
System.out.println("The list is " + obj.subFolders);
}
}
}
但我的代码中出现错误:
ListF.java:5: '(' or '[' expected List<Folder> subFolders=new ArrayList<Folder>;
有人可以指出我的错误吗?感谢。
答案 0 :(得分:3)
将new ArrayList<Folder>;
替换为new ArrayList<Folder>();
答案 1 :(得分:1)
List<Folder> subFolders = new ArrayList<Folder>;
将上述声明设为:
List<Folder> subFolders = new ArrayList<Folder>();
或强>
如果您使用的是Java 7及更高版本,也可以这样做....
List<Folder> subFolders = new ArrayList<>();