Java中要数组的文件列表
我想知道,为什么将文件的Arraylist更改为数组会出现问题Java中要数组的文件列表,java,arrays,list,Java,Arrays,List,我想知道,为什么将文件的Arraylist更改为数组会出现问题 public static void main(String[] args) { List<File> pl = new ArrayList<File>(); pl.add(new File ("C:\\folder")); String[] k; k = pl.toArray(new String[pl.size()]); System.out.println(k);
public static void main(String[] args) {
List<File> pl = new ArrayList<File>();
pl.add(new File ("C:\\folder"));
String[] k;
k = pl.toArray(new String[pl.size()]);
System.out.println(k);
}
第二个问题:我必须通过Arraylist的大小吗?因为这两个版本都适用于字符串的arraylist:
k = pl.toArray(new String[pl.size()]);
k = pl.toArray(new String[] {});
你有一个文件对象列表,你不能仅仅把它们放在一个字符串数组中,你需要以某种方式显式地转换它们,例如通过调用FilegetName。最方便的方法可能是使用流:
你有一个文件对象列表,你不能仅仅把它们放在一个字符串数组中,你需要以某种方式显式地转换它们,例如通过调用FilegetName。最方便的方法可能是使用流:
当您想要将列表映射到数组中时,不需要将路径转换为字符串 在这里您可以写些什么,我添加一些关于更改的评论:
public static void main(String[] args) {
List<File> pl = new ArrayList<>(); // Diamond operator
pl.add(new File ("C:\\folder"));
File[] k = pl.toArray(new File[pl.size()]);
k = pl.stream().toArray(File[]::new); // equivalent to the previous line
System.out.println(Arrays.toString(k)); // Array.toString is not overriden. So this is needed
}
当您想要将列表映射到数组中时,不需要将路径转换为字符串 在这里您可以写些什么,我添加一些关于更改的评论:
public static void main(String[] args) {
List<File> pl = new ArrayList<>(); // Diamond operator
pl.add(new File ("C:\\folder"));
File[] k = pl.toArray(new File[pl.size()]);
k = pl.stream().toArray(File[]::new); // equivalent to the previous line
System.out.println(Arrays.toString(k)); // Array.toString is not overriden. So this is needed
}
你想在这里干什么?您正在尝试将文件列表转换为字符串内容列表吗?您在这里尝试执行什么操作?您正在尝试将文件列表转换为字符串内容列表吗?
public static void main(String[] args) {
List<File> pl = new ArrayList<>(); // Diamond operator
pl.add(new File ("C:\\folder"));
File[] k = pl.toArray(new File[pl.size()]);
k = pl.stream().toArray(File[]::new); // equivalent to the previous line
System.out.println(Arrays.toString(k)); // Array.toString is not overriden. So this is needed
}