作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有课
public class SimpleData() {
String continent;
String country;
String city;
public SimpleData(String continent, String country, String city) {
this.continent = continent;
this.country = country;
this.city = city;
}
}
另一个类从文件中获取数据并返回 2d 对象数组
private Object[][] getDataFromFile(String fileName) {
return dataLoader.getTableArray(fileLocation, dataSheetName);
}
//will return something like
europe, uk, london
europe, france, paris
在循环遍历二维数组并将对象添加到列表中时,如何创建 SimpleData 对象,以便 SimpleData 的每个对象代表一行数据?
private List<SimpleData> getDataList() {
Object[][] array = readDataFromFile("myfile");
List<SimpleData> dataList = new ArrayList<SimpleData>();
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
//what's the code to generate object with the correct row of data?
}
}
return dataList;
}
最佳答案
而不是
for (int j = 0; j < arr[i].length; j++) {
//what's the code to generate object with the correct row of data?
}
您将需要这个(忽略异常处理):
dataList.add(new SimpleData(array[i][0].toString(), array[i][1].toString(),
array[i][2].toString()));
关于java - 如何在 Java 中的二维数组上的循环内创建对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10922187/
我是一名优秀的程序员,十分优秀!