I'm a java beginner and I know that you should use list over arraylist but i'm not exactly sure how to change from arraylist to list. Before I had
ArrayList<Homework3> hw = new ArrayList<Homework3>(); Which worked and then I tried:
List<Homework3> hw = new ArrayList<Homework3>(); Then I tried to implement the List interface with this:
public interface List<Homework3> // inheritance not shown { boolean add( Homework3 x ); void add( int index, Homework3 x ); Homework3 get( int index ); Homework3 remove( int index ); Homework3 set( int index, Homework3 x ); int size(); } But now it's saying incompatible types. I looked at other questions and discussions and they had the code just like this:
List<Object> list = new ArrayList<Object>(); And i'm following the same basic principle, can someone help explain why it isn't working and how I can fix this?
java.util.ArrayListis an implementation of thejava.util.Listinterfaceand areferenceto a parent type can refer to a child object. therefore,java.util.List<SomeObject> list = new java.util.ArrayList<>()is a valid statement. If you implement your ownListthenjava.util.ArrayListwill not be a sub-type of your customListthus the statementList<SomeObject> myList = new java.util.ArrayList<>()will not be valid.