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

This is giving me an error

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

Maroun
  • 94,125
  • 30
  • 188
  • 241

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
  • 387,369
  • 54
  • 702
  • 768