0

I'm a Java beginner currently using Java version 8. While I was coding, I had the following problem, but I really cannot find what the problem is. I tried to create an ArrayList by declaring...

ArrayList al=new ArrayList(100000);  

...which has an initial capacity of 100000. But the program returns an error saying...

The constructor ArrayList(int) is undefined.

To make sure, I even searched for the API Documentation of class java.util.ArrayList, which, of course, said...

ArrayList​(int initialCapacity):
Constructs an empty list with the specified initial capacity.

...in its Constructor summary.

Also, the second problem is:
After I declared ArrayList, I tried to use it as an argument for the add method, but it says...

The method add(List) in the type ArrayList3 is not applicable for the arguments (ArrayList). 

My full code is here:

import java.util.*;
public class ArrayList3 {
        public static void main(String[] args) {
                ArrayList al=new ArrayList(100000);
                LinkedList ll=new LinkedList();
                System.out.println("ArrayList: "+add(al));
                System.out.println("LinkedList: "+add(ll));
        }

        public static long add(List list) {
            long start = System.currentTimeMillis();
            for(int i=0;i<10000;i++) 
                list.add(i+" ");
            long end = System.currentTimeMillis();
            return start - end;
        }
}

What would be the problem, and what could have I missed? Thank you very much in advance!

6
  • Compiles fine. Can't reproduce your error. Commented Jun 6, 2018 at 9:50
  • Clean and rebuild? Commented Jun 6, 2018 at 9:51
  • Working fine in Java 8. Commented Jun 6, 2018 at 9:52
  • 3
    You have a ArrayList3 class. I'm guessing you have (or had as some point) a custom ArrayList class too. Commented Jun 6, 2018 at 9:52
  • You have another class called ArrayList in the same source file or in the same package as ArrayList3 (not surprising as the class you show here is called ArrayList3) Commented Jun 6, 2018 at 9:53

1 Answer 1

1

You have another ArrayList class in your package, and it is conflicting with java.util.ArrayList. Either it is a real class or maybe it is the result of a previous compilation.

Note that this won't compile, due to conflicting names,

package stackOv;    
import java.util.*;
public class ArrayList {
    public static void main(String[] args) {
        ArrayList al=new ArrayList(100000);
    }
}

while this will compile, since using fully qualified names resolves the conflict

import java.util.*;
public class ArrayList {
    public static void main(String[] args) {
        java.util.ArrayList al=new java.util.ArrayList(100000);
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

This will work, however, It is important to understand what is the other ArrayList that is in the classpath that causes this ,
Thanks, there actually was an ArrayList class that I made in the same package and after I deleted it, the first problem was solved; but I still cannot fix the second problem where the method that I made with the argument type of List, says that 'the method add(List) in the type ArrayList3 is not applicable for the arguments (ArrayList)'. Wondering if the program might be colliding with ArrayList.add, I tried to change the method name to 'measure', but still the error doesn't go away.
I really don't get why this problem is happening; doesn't the class ArrayList implement the interface List?
Perhaps try a different Work-space without this Custom ArrayList.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.