-5
List<Object> list = new ArrayList<String>();

This is giving me an error

ArrayList<String> cannot be converted to List<Object>

Maroun
  • 91,013
  • 29
  • 181
  • 233

1 Answers1

1

You can add any object to a List<Object>, but you should only be able to add String objects to a List<String>. Therefore you cannot assign a List<String> to a List<Object> variable.

If your List should only hold String objects, change its type :

List<String> list = new ArrayList<String>();
Eran
  • 374,785
  • 51
  • 663
  • 734