我试图将不同数组中的所有值添加到一个2d数组中,并将其显示为JavaFX中的表。
我尝试这样做。
private StackPane table(Stage stage) {
StackPane root = new StackPane();
String[][] staffArray = new String[365][365];
staffArray[0][0] = "No. ";
staffArray[0][1] = "Check in";
staffArray[0][2] = "Check out";
staffArray[0][3] = "Name";
staffArray[0][4] = "Surname";
System.out.println(String.valueOf(arrRoom1[0][0]));
for (int i = 0; i < arrRoom1.length; i++) {
for (int y = 1; y < arrRoom1.length; y++) {
if (arrRoom1[i] == null) {
break;
}
staffArray[y][0] = String.valueOf(y);
staffArray[y][1] = String.valueOf(arrRoom1[i][0]);
staffArray[y][2] = String.valueOf(arrRoom1[i][1]);
staffArray[y][3] = String.valueOf(names1[i]);
staffArray[y][4] = String.valueOf(surnames1[i]);
}
}
System.out.println(staffArray[1][1]);
ObservableList<String[]> data = FXCollections.observableArrayList();
data.addAll(Arrays.asList(staffArray));
data.remove(0);//remove titles from data
TableView<String[]> table = new TableView<>();
for (int i = 1; i < staffArray.length; i++) {
for (int y = 0; y < 5; y++) {
if ( staffArray[i][y] == null) {
break;
}
TableColumn tc = new TableColumn(staffArray[i][y]);
final int colNo = i;
tc.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<String[], String>, ObservableValue<String>>() {
@Override
public ObservableValue<String> call(TableColumn.CellDataFeatures<String[], String> p) {
return new SimpleStringProperty((p.getValue()[colNo]));
}
});
tc.setPrefWidth(90);
table.getColumns().add(tc);
}
}
table.setItems(data);
root.getChildren().add(table);
return root;
}
但是,每次循环后我在staffArray中得到null值。嵌套循环中的错误可能在哪里?