What is Collection?
A collection is a general term that means something like "a bunch of objects stored in a structured manner". or Simply Collection is also called Container.
Collection is a Java Interface.
A collection represents a group of objects, known as its elements. This framework is provided in the java.util package. Objects can be stored, retrieved, and manipulated as elements of collections.
Collections can be used in various scenarios like storing phone number, Employee names database etc. They are basically used to group multiple elements into a single unit. Some collections allow duplicate elements while others do not. Some collections are ordered and others are not.
Why ArrayList? If you don't know how many items are going to be held in your array, you may be better off using something called an ArrayList.
What is ArrayList?
An ArrayList is a dynamic data structure(Collection), meaning items can be added and removed from the list. A normal array in java is a static data structure, because you stuck with the initial size of your array but Java ArrayList is a dynamic array, an array that can grow after it is created.
Which Package do you need?
ArrayList is in the "java.util" package and if you want to use this then you should import this package in your code like this
import java.util.ArrayList;
Hierarchical Structure
A collection is a general term that means something like "a bunch of objects stored in a structured manner". or Simply Collection is also called Container.
Collection is a Java Interface.
A collection represents a group of objects, known as its elements. This framework is provided in the java.util package. Objects can be stored, retrieved, and manipulated as elements of collections.
Collections can be used in various scenarios like storing phone number, Employee names database etc. They are basically used to group multiple elements into a single unit. Some collections allow duplicate elements while others do not. Some collections are ordered and others are not.
Why ArrayList? If you don't know how many items are going to be held in your array, you may be better off using something called an ArrayList.
What is ArrayList?
An ArrayList is a dynamic data structure(Collection), meaning items can be added and removed from the list. A normal array in java is a static data structure, because you stuck with the initial size of your array but Java ArrayList is a dynamic array, an array that can grow after it is created.
Which Package do you need?
ArrayList is in the "java.util" package and if you want to use this then you should import this package in your code like this
import java.util.ArrayList;
Hierarchical Structure
The ArrayList class extends AbstractList and implements the List interface.
Why we use ArrayList instead Array?
ArrayList supports dynamic arrays that can grow as needed.
In Java, standard 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. But, sometimes, you may not know until run time precisely how large of an array you need. To handle this situation, the collections framework defines ArrayList. In essence, an ArrayList is a variable-length array of object references. That is, an ArrayList can dynamically increase or decrease in size. Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.
How to delcare and use ArrayList?
ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
How to show String ArrayList?
for(String temp:list){
System.out.println(temp);
}
OR
for(int i=0;i<list.size;i++){ System.out.println(list.get(i)); }
No comments:
Post a Comment