Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList(Collection<? extends E> c)

Tags:

java

api

I know that in order to create an object of ArrayList type I need to:

ArrayList<MyType> l = new ArrayList<MyType>();

I know it because I've seen this in books.

But looking at Java SE API for the ArrayList constructor I see: ArrayList(Collection<? extends E> c)

I don't understand, how would I ever get an idea to specify the type of objects my new ArrayList object would hold? How would I know from this definition that I have to specify <MyType> during instantiation and variable declaration?

like image 657
Eugene Avatar asked Dec 06 '10 19:12

Eugene


People also ask

Does ArrayList extend List?

The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed. Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold.

What is E in collections in Java?

It means that you're dealing with a collection of items with type E .

Is ArrayList a collection?

Java ArrayList is an ordered collection. It maintains the insertion order of the elements. You cannot create an ArrayList of primitive types like int , char etc. You need to use boxed types like Integer , Character , Boolean etc.

What is the capacity of an ArrayList?

Capacity is the number of elements that the ArrayList can store. Count is the number of elements that are actually in the ArrayList. Capacity is always greater than or equal to Count.


1 Answers

That method is meant to copy an existing collection instance to the new ArrayList not to create on from scratch. The elements of the collection it will accept have an upper bound of type E which will be the type of your new ArrayList then.

like image 196
soc Avatar answered Oct 01 '22 03:10

soc