小编Bra*_*ram的帖子

获取向量的最后一个元素并将其推送到同一向量

我想做什么:

enum Test {
    Value1,
    Value2,
    Value3
}

fn main() {
    let mut test_vec: Vec<Test> = Vec::new();
    test_vec.push(Test::Value2);

    if let Some(last) = test_vec.last() {
        test_vec.push(*last);
    }
    //Wanted output: vector with [Test::Value2, Test::Value2]
}
Run Code Online (Sandbox Code Playgroud)

我明白,当我打电话时last(),它会返回Option<&Test> 所以它会借用test_vec直到if-let块结束.

我尝试了以下但没有成功:

if let Some(last) = test_vec.last().map(|v| v.clone()) {
    test_vec.push(*last);
}

//and

let last = test_vec.last().unwrap().clone();
test_vec.push(*last);
Run Code Online (Sandbox Code Playgroud)

vector rust

4
推荐指数
2
解决办法
2381
查看次数

当并非所有属性都在同一类中时,填充JavaFX tableview

我有一个关于在JavaFX中填充TableView的问题。首先,我在javaFX中是个新手,对我的无知深表歉意。

我做了一个简单的示例,尝试了tableview的工作原理:

public class Person{

    private StringProperty firstName, lastName;
    private IntegerProperty age;

    //Left the constructor and getters out

}
Run Code Online (Sandbox Code Playgroud)

对于控制器:

public class Controller implements Initializable {

    @FXML private TableView tableView;
    @FXML private TableColumn firstNameColumn, lastNameColumn, ageColumn;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        ObservableList<Person> data = FXCollections.observableArrayList();        
        tableView.setItems(data);

        firstNameColumn.setCellFactory(new PropertyValueFactory<Person, String>("firstName"));
        lastNameColumn.setCellValueFactory(new PropertyValueFactory<Person, String>("lastName"));
        ageColumn.setCellValueFactory(new PropertyValueFactory<Person, Integer>("age"));

        data.add(new Person("Piet", "Jansen", 22));
    }
}
Run Code Online (Sandbox Code Playgroud)

这可以正常工作,但是如果每个人都有一个包含名字和姓氏的Name对象,该怎么办?

例如:

public class Person{

    private Name name;
    private IntegerProperty age;

    //Left the constructor and getters …
Run Code Online (Sandbox Code Playgroud)

java javafx properties tableview fxml

2
推荐指数
1
解决办法
6308
查看次数

标签 统计

fxml ×1

java ×1

javafx ×1

properties ×1

rust ×1

tableview ×1

vector ×1