Search code examples
javaarraysgenericsinsertion

How Can I input values into a generic defined array list?


I am working on a calculator like assignment for my data structures class, and I have to input symbols into the array list defined as

ArrayList<ScalarSymbol> scalar;

and I have been trying to input values into it by doing this:

scalars.add(0,')');

and it tells me that the char type is not the correct syntax for the <ScalarSymbol> array. I don't know how to find out what types the array will take, is there bind I have to use in order to make it accept chars?


Solution

  • Just create an instance of the object you want to add:

    ScalarSymbol s = new ScalarSymbol();
    

    Then add it to the list:

    scalar.add(s);
    

    The array list only takes the type written in these:

    <type>