I have created a BarData class that holds bar chart data. For use with ''. There are two or more bar graphs. In one graph, only name, value 1, and value 2 are used, but in some graphs, name, value 1, value 2, value 3, value 4 may be used.

Specifically, the BarData class is as follows, but in the second argument of the constructor of this class, List values I, in the order in which it is there, I would like to set value1, value2, or set value1, value2, value3, value4 dynamically.

I understand that it is sufficient to prepare the constructor for that amount, but how can I set it as a member variable by the amount specified by List values?

    public class BarData {
    public String name { get; set; }
    public Integer value1 { get; set; }
    public Integer value2 { get; set; }
    public Integer value3 { get; set; }
    public Integer value4 { get; set; }
    public Integer value5 { get; set; }

    public BarData(
            String name,
            List<Integer> values
    ) {
        this.name = name;

        for(Integer i: values) {
            this.value = i;

1 Answer 1


You just have to do it manually. For a handful of values this approach is feasible:

this.name = name;
value1 = values[0];
value2 = values[1];
value3 = values[2];
value4 = values[3];
value5 = values[4];

If you want you can add index checking:

this.name = name;
if (values.size() > 0) value1 = values[0];
if (values.size() > 1) value2 = values[1];
if (values.size() > 2) value3 = values[2];
if (values.size() > 3) value4 = values[3];
if (values.size() > 4) value5 = values[4];

You must log in to answer this question.