I am trying to understand how to create array lists in Java, for some reason I am getting an error saying to change the type of myList to ArrayList even though I saw this example from a different post on stack overflow and it seemed to work.
What I am trying to do is simply create a List, add three strings to it and iterate through it and print it out.
On a side note, is it possible to add integers, strings, and objects all to the same list?
I’d appreciate any guidance in this regard.
List myList = new ArrayList();
myList.add("hello");
myList.add("World");
myList.add("!");
//print contents
System.out.println(myList);
My guess is that you’ve imported
java.util.ArrayList, but notjava.util.List. The code you’ve posted works if you import both:Having said that, I’d strongly urge you to use generics: