I understand that what I have currently is maybe better for readability, but I am just interested in the different ways I could achieve the same result with less code.
ArrayList<MaterialButton> buttons = new ArrayList<>();
// grab button states and write to database
String[] buttonStates = new String[buttons.size()];
for (int i = 0; i < buttons.size(); i++) {
buttonStates[i] = String.valueOf(buttons.get(i).isChecked());
}
Appreciate any input I can get on this!
You can use the java stream api for that:
The
String[]::new
is a Function that takes the number of elements as an argument, meaning it creates anew String[buttons.size()]
.