1

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?

2
  • 1
    What exactly do you mean by "implement List"? And don't omit type information (inheritance) when that's what you're asking about. It sounds like you are unclear on what the concept of a Java interface is. Commented Jan 6, 2019 at 3:36
  • java.util.ArrayList is an implementation of the java.util.List interface and a reference to 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 own List then java.util.ArrayList will not be a sub-type of your custom List thus the statement List<SomeObject> myList = new java.util.ArrayList<>() will not be valid. Commented Jan 6, 2019 at 3:42

2 Answers 2

1

You don't need to do any of this. Java takes care of this type resolution for you through generics.

Because List<E> is defined in a generic way, so too must its implementors be, and thus ArrayList<E> uses the same generic type declared by the interface.

To be explicit:
When you declare List<Homework3> hw = new ArrayList<Homework3>();, everywhere that E is used in the Javadoc is replaced by Homework3. You don't have to implement any of this because the language already has for you.

Sign up to request clarification or add additional context in comments.

1 Comment

List<Homework3> hw = new ArrayList<Homework3>(); and I removed the list class
0

The class java.util.ArrayList implements java.util.List interface. Hence your code after defining your own List<Homework3> interface would have incompatible types.

 List<Object> list = new ArrayList<Object>(); // ^^ ^^ // your.package.List java.util.ArrayList 

Similar code in your question

 List<Homework3> hw = new ArrayList<Homework3>(); // ^^ ^^ // java.util.List java.util.ArrayList 

would have worked earlier since then you didn't introduce the List interface as mentioned in the question and you were referring to java.util.List itself.

5 Comments

I removed the java.util.ArrayList and now it says cannot find symbol over the arraylist
@user9986762 If you're planning to define your own List interface and ArrayList class, then you would have to create one. Also note, you should rename such instances to refrain from class name conflicts and incorrect imports, something like CustomList and CustomArrayList.
List<Homework3> hw = new ArrayList<Homework3>(); and I removed the list class. I also removed the import statement at the top of the code
Now it cannot find the "List" and the "ArrayList" symbol so what can I do to get it working just like an arraylist again while keeping the "List"
I removed the list class. I also removed the import statement at the top of the code ... you should now use the imports for JDK classes .. import java.uti.List; import java.util.ArrayList;

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.