有没有办法遍历 Control 数组并只获取一种类型的 UI 元素,例如 TextField
?这是一个包含 TextField
、DatePicker
和 ChoiceBox
es 的数组:
Control[] allControls = new Control[] {
name, surname, age, address, city,
telephoneNum, email, deptBox, idNum, startDate,
contractType, endDate, payFreq, accountNum, taxCoeficient,
netSalary
};
有没有办法将if
条件放入for
循环中,只获取一种类型的UI元素?
最佳答案
您可以申请 Stream API使用 instanceof
过滤所需类的所有实例,最后将它们收集到一个新列表中:
List<TextField> textFieldList = Arrays.asList(allControls).stream()
.filter(c -> c instanceof TextField)
.map (c -> (TextField) c)
.collect(Collectors.toList());
关于java - 从 Control 数组按类型获取 UI 元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45661097/