Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Set Multiple Objects
    primarykey
    data
    text
    <p>I know that this is a general question and I have researched about this all day long, but I would like to know if it's possible to set multiple objects using a 'for' statement and avoiding multiple if-elses. So I have this:</p> <ol> <li>I have a String[] array with 50 column names.</li> <li>I have to iterate this array and create a custom object for each name. This custom object has some common data but there are 2 properties that might differ by column (for example I have a columnType property which has to be set in particular for each object I create). Then I have to add these objects to an ArrayList. </li> </ol> <p>So how could I set my objects (an object represents a column) for each column name I have without creating them one by one and then adding them to an ArrayList 'manually' or if I use a 'for' how to avoid those if-else to set the type?</p> <pre><code> ArrayList&lt;MyColumnObj&gt; arrayList = new ArrayList&lt;MyColumnObj&gt;; for (int i = 0; i &lt; columns.length; i++) { Button btn = new HeaderButton(this); btn.setPosition(i); btn.setColumnName(columns[i]); int columnWidth = 150; MyColumnObj myColumnObject = new MyColumnObj(btn, columnWidth); myColumnObject.setTag(i); if (columns[i].equals("Column 1") { myColumnObject.setType("Column Number"); } else if (columns[i].equals("Column 2") { myColumnObject.setType("Column Colored"); } ... else if (columns[i].equals("Column 50") { myColumnObject.setType("Column Text"); } arrayList.add(myColumnObject); } </code></pre> <p>I would appreciate any answer. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload