Can't add values in an arraylist?

ratRiot01 :

Why is arrayList printing 2 times in this javafx controller code?

Example 1:

List<Compute> list = new ArrayList<>(); // declared as global

@FXML
public void clickSubmit() {

    int hr = Integer.parseInt(txtHours.getText());
    int min = Integer.parseInt(txtminutes.getText());
    Compute compute = new Compute(hr, min);
    list.add(compute);
    for(int x = 0; x < list.size(); x++) {
        showVBox.getChildren().add(new Label(list.get(x).toString()));
        list = new ArrayList<>(copyList);
    }

I have 2 values in this list. On 2nd submit, the first value was also shown

So I created another ArrayList and took the copy of the origList. By doing this, this will not print 2 times on 2nd submit.

List<Compute> copyList = new ArrayList<>(); // declared as global

@FXML
public void clickSubmit() {
    List<Compute> origList = new ArrayList<>();
    int hr = Integer.parseInt(txtHours.getText());
    int min = Integer.parseInt(txtminutes.getText());
    Compute compute = new Compute(hr, min);
    origList.add(compute);

    for (int x = 0; x < origList.size(); x++) {
        showVBox.getChildren().add(new Label(origList.get(x).toString()));
        copyList = new ArrayList<>(origList); // I took it's copy
    }
}

enter image description here

However, I still don't understand why is it printing 2 times on my Example 1. Also, the time will not add properly if I used Example 2. Please please help!

@FXML
public void clickAdd() {
Duration sum = Duration.ZERO;
for(Compute duration : list) {
sum = sum.plus(duration.getDuration());
}
System.out.println(sum);
System.out.format("%d:%02d", sum.toHours(), sum.toMinutesPart());
}
Sweeper :

Note that you have a for loop in the clickSubmit method, so each time the submit button is clicked, the whole list will be looped through and each item will be added to the VBox.

The first time clickSubmit is clicked, the list will have only one element, so one label will be added to the VBox. The second time it's clicked, the list will have two elements, so two more labels will be added to the VBox, the first one being the one you already added the first time.

See the problem? The contents of the VBox doesn't get cleared automatically at the start of clickSubmit, which you might have assumed.

One solution is to only add one label each time, for the new item only:

public void clickSubmit() {

    int hr = Integer.parseInt(txtHours.getText());
    int min = Integer.parseInt(txtminutes.getText());
    Compute compute = new Compute(hr, min);
    list.add(compute);
    showVBox.getChildren().add(new Label(compute.toString()));
}

Alternatively, clear the VBox before adding all the labels:

public void clickSubmit() {
    // note this line:
    showVBox.getChildren().clear();
    int hr = Integer.parseInt(txtHours.getText());
    int min = Integer.parseInt(txtminutes.getText());
    Compute compute = new Compute(hr, min);
    list.add(compute);
    for(int x = 0; x < list.size(); x++) {
        showVBox.getChildren().add(new Label(list.get(x).toString()));
        list = new ArrayList<>(copyList);
    }
}

Your Example 2 "works" because you are clearing the list (yes, that's what origList= new ArrayList<>(copyList); does) every time, so the list is almost always empty.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=27062&siteId=1