Why can not I create an ArrayList without parameters?

2

It tells me that the constructor does not have parameters when in the API 7 java an ArrayList can be built without paramentros ie empty.

import java.util.Iterator;
import java.util.List;

public class ArrayList {




public static void main(String[] args) {

        // create list
        List<String> crunchifyList = new ArrayList<String>();

        // add 4 different values to list
        crunchifyList.add("eBay");
        crunchifyList.add("Paypal");
        crunchifyList.add("Google");
        crunchifyList.add("Yahoo");

        // iterate via "for loop"
        System.out.println("==> For Loop Example.");
        for (int i = 0; i < crunchifyList.size(); i++) {
            System.out.println(crunchifyList.get(i));
        }

        // iterate via "New way to loop"
        System.out.println("\n==> Advance For Loop Example..");
        for (String temp : crunchifyList) {
            System.out.println(temp);
        }

        // iterate via "iterator loop"
        System.out.println("\n==> Iterator Example...");
        Iterator<String> crunchifyIterator = crunchifyList.iterator();
        while (crunchifyIterator.hasNext()) {
            System.out.println(crunchifyIterator.next());
        }

        // iterate via "while loop"
        System.out.println("\n==> While Loop Example....");
        int i = 0;
        while (i < crunchifyList.size()) {
            System.out.println(crunchifyList.get(i));
            i++;
        }

        // collection stream() util: Returns a sequential Stream with this collection as its source
        System.out.println("\n==> collection stream() util....");
        crunchifyList.forEach((temp) -> {
            System.out.println(temp);
        });
    }
}
    
asked by user7407723 20.06.2017 в 20:58
source

3 answers

2

The problem you mention:

  

type Arraylist does not take parameters

It does not have to do with defining a generic or untyped ArrayList, it's because your class has the name ArrayList :

public class ArrayList {

Change the name and you will not have any problem, your code is correct, avoid using reserved words.

    
answered by 20.06.2017 / 21:10
source
2

The problem seems to be that you are creating your class with the name ArrayList, which is an existing class but takes precedence by being the class you are creating.

You try to instantiate it but your class does not have a constructor that receives parameters and marks you that error

You should do this initialization so that it works using the Java class (not your class)

List<String> crunchifyList = new java.util.ArrayList<String>();

    
answered by 20.06.2017 в 21:09
1
public class ArrayList {

You have defined a class called ArrayList . That is the class ArrayList that the compiler uses.

Your class is not parameterizable, so the error comes out. Another clue is that you do not import of java.util.ArrayList

Leaving aside that the name is very confusing (maybe I would rename it to ArrayListTest ?), if you want to use the class ArrayList of the API you will have to use the full name (with the package):

List<String> crunchifyList = new java.util.ArrayList<String>();
    
answered by 20.06.2017 в 21:07