L
L
Lord Drous2017-03-20 00:50:52
Java
Lord Drous, 2017-03-20 00:50:52

How to perform actions on elements with ArrayList?

Hello, I read the array using list.add(tf_name.getText()), I want to perform some actions in the loop then, let's find min, max, etc., in the console it was done like this arr[i] < 0 etc. And I have a sheet, and, as I understand it, I don’t have such an array. Thanks in advance for the help
that I wrote myself:

public class Controller {
    ArrayList<String> list = new ArrayList<>();

    @FXML
    TextField tf_name;

    @FXML
    Label lb_one;
    @FXML
    private void onClick(ActionEvent event){

        list.add(tf_name.getText());
        String s = "";
        for (int i = 0; i < list.size(); i++){
            s = list.get(i) + s;
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.println(s + " ");

        }//String s = tf_name.getText();
        //lb_one.setText(s);
    }
}

screen
3f6f53633474413bb99a5bf4f7ca2e7d.pngd4bf03245cea479da4f0fdb27e10f1fd.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav Rymar, 2017-03-20
@Rymar4ik

If you need a way to loop through a List, you can also use a simple for. You can also use forEach.

for (String num : list){
    s += num + " ";
}

This loop takes each element of the list collection and assigns it to the String num you are already looping with. There is a slightly different loop through the Stream API that appeared in Java 8.
This line will display all the elements of the list separated by a space.
PS
In theory, the code that you indicated takes the string "1 2 3 4 5" and puts it in the List (the size of the sheet after that is 1), after which you work with only one entry in the List. If you want each value of the string in the example to be separate, do it as an option like this
List<String> list = Arrays.asList(tf_name.getText().split(" "));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question