【发布时间】:2025-12-31 10:00:18
【问题描述】:
我有课
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;
}
}
另一个类从文件中获取数据并返回一个二维对象数组
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;
}
【问题讨论】:
标签: java arrays multidimensional-array 2d