Harout Tatarian
Harout Tatarian

Reputation: 431

Changing from List to ArrayList removes the error "List is an abstract and can not be instantiated?

Well, I'm new to List I've always worked with Arrays, now I'm writing a program where I cannot tell the size of the array before it's creation so I'm using List. The thing is that I've a method that returns a List of none repeating digits.

Here is my method:

public static List<Integer> arrayOfNoneRepeatingDigits(int limit) {

List<Integer> list = new ArrayList<Integer>();

for(int i = 0; i < limit; i++){
        boolean ignore = false;
        for(int j = i; j > 0; j/=10){
            if(ignore == true) break;
            for(int k = j/10; k > 0; k/=10){
                if(j%10 == k%10){
                    ignore = true;
                    break;                        
                    }                    
                }               
            }
        if(ignore == false)list.add(i);                
        }    
return list;    
} 

First my method datatype was an Array but after I changed it to List this line of code gave an error:

List<Integer> list = new List<Integer>();

I've searched online and It turned out that I should replace List with ArrayList. I did it and the error is gone now but I don't know why.

Upvotes: 2

Views: 288

Answers (3)

jungyh0218
jungyh0218

Reputation: 568

List is an interface. It means there is no attribute in List, and methods are declared but not defined. So, there is not enough information to instantiate it and an error occurs. This happens same when you try to instantiate an object of an abstract class.

You can declare an object of interface/abstract class, but you cannot instantiate it. So you have two choices.

List<Integer> list = new ArrayList<Integer();
((ArrayList)list).method(); 
//cast it into ArrayList. 
//If you try to cast a wrong class, then the error will be printed out.

This way need extra casting. Else, you can declare with ArrayList also.

ArrayList<Integer>list = new ArrayList<Integer>();

Upvotes: 1

Sweeper
Sweeper

Reputation: 272845

Interfaces cannot be instantiated, so no new List<Integer> stuff. Interfaces's methods don't have implementations. The implementations of those methods are in the subclasses. ArrayList is one of the subclasses of List so that's why you can use your first code snippet.

If you write new List<Integer>, it doesn't make sense. Because List's methods don't have implementations so when you call the add method, how would the compiler know what implementation is in the method?

Upvotes: 2

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

This:

List<Integer> list = new List<Integer>();

doesn't make sense since you're trying to directly instantiate an interface, something that is not concrete and thus something that you can't do unless you create an anonymous inner class with the instantiation, something that you really don't want to do. The best solution: stick with the ArrayList for the concrete implementation (or LinkedList depending on your requirements). e.g.,

List<Integer> list = new ArrayList<>();

Out of curiosity, what motivated you to make this change when you already had working code?

Upvotes: 2

Related Questions