我的调查问卷上有一个开放式问题,例如"请列出十个动物",这给了我以下数据框(每个字母代表一只动物):
nrow <- 1000
list <- vector("list", nrow)
for(i in 1:nrow){
na <- rep(NA, sample(1:10, 1))
list[[i]] <- sample(c(letters, na), 10, replace=FALSE)
}
df <- data.frame()
df <- rbind(df, do.call(rbind, list))
head(df)
# V1 V2 V3 V4 V5 V6 V7 V8 V9 V10
# 1 r <NA> a j w e i h u z
# 2 t o e x d v <NA> z n c
# 3 f y e s n c z i u k
# …Run Code Online (Sandbox Code Playgroud) 我正在尝试理解Java 8的Lambda表达式.在示例中,我想解析许多文件.对于每个文件,我需要创建特定模板的新实例(对于一次传递的所有文件,它是相同的).
如果我理解正确,这就是Lambda表达式的优点.
任何人都可以用简单的术语向我解释如何将调用传递给模板的构造函数作为参数?(这样就可以了new Template1(),new Template2()等等).
import java.io.File;
public class Parser {
public static void main(String[] args) {
new Parser(new File[]{});
}
Parser(File[] files) {
for (File f : files) {
// How can I pass this as a parameter?
Template t = new Template1();
}
}
public class Template {
// Code...
}
public class Template1 extends Template {
// Code...
}
public class Template2 extends Template {
// Code...
}
}
Run Code Online (Sandbox Code Playgroud) 显然,无法从List <File>转换为List <Path>.我试图这样做,因为JFileChooser返回一个File对象数组.现在,我只是好奇Java中是否有比使用循环更优雅(功能)的方法.
import java.io.File;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) {
File[] array = new File[] { new File("a.txt"), new File("b.txt"),
new File("c.txt"), new File("d.txt") };
// Type mismatch: cannot convert from List<File> to List<Path>
// List<Path> list = Arrays.asList(array);
// This will work but is not particularly pretty
List<Path> list = new ArrayList<Path>();
for (int i = 0; i < array.length; i++) {
list.add(array[i].toPath());
}
System.out.println(list);
} …Run Code Online (Sandbox Code Playgroud)