0% found this document useful (0 votes)
5 views

Class1 Esteem ArrayList

Arrays in Java have a fixed size and only allow homogeneous elements, while Collections are dynamically resizable and can hold heterogeneous elements. The List interface, introduced in JDK 1.2, allows for indexed access and duplicates, with ArrayList being a key implementation that supports dynamic resizing and various methods for element manipulation. ArrayLists are not synchronized, allowing multiple threads to access data simultaneously, but they do not guarantee data consistency.

Uploaded by

aoibenio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Class1 Esteem ArrayList

Arrays in Java have a fixed size and only allow homogeneous elements, while Collections are dynamically resizable and can hold heterogeneous elements. The List interface, introduced in JDK 1.2, allows for indexed access and duplicates, with ArrayList being a key implementation that supports dynamic resizing and various methods for element manipulation. ArrayLists are not synchronized, allowing multiple threads to access data simultaneously, but they do not guarantee data consistency.

Uploaded by

aoibenio
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Q) In Java Applications, to represent a group of other elements we have already Arrays then what is

the requirement to use Collections?


OR
Q) What are the differences between Array And Collection?
=======================================
Arrays are having fixed size in nature. In case of arrays, we are able to add the elements up to the
specified size only, we are unable to add the elements over its size. If we are trying to add elements
over its size then JVM will rise an exception like "java.lang.ArrayIndexOutOfBoundsException"

Collections are having dynamically grow-able nature, even if we add the elements over its size then
JVM will not rise any exception.
=======================================
In Java, by default, Arrays are able to allow homogeneous elements, if we are trying to add the
elements which are not same Array data type then Compiler will
rise an error like "Incompatible Types".

In Java, by default, Collections are able to allow heterogeneous elements, even we add different types
of elements Compiler will not rise any error.

=======================================

Eg: If we are given a number n and we want to find all the Fibonacci numbers until that number, then
we cannot use arrays as we have no knowledge about the number of elements before the given n. In
these type of scenarios ArrayList comes handy.

List:

• List is a direct child interface to Collection interface


• List was provided by JAVA along with its JDK1.2 version
• List is index based, it able to arrange all the elements as per indexing.
• List is able to allow duplicate elements.
• List is following insertion order.
• List is not following Sorting order.
• List is able to allow any number of null values.
• List is able to allow heterogeneous elements.

ArrayList:

• It was provided by JAVA along with JDK 1.2 version.


• It is a direct implementation class to List interface.
• It is index based.
• It allows duplicate elements.
• It follows insertion order.
• It will not follow sorting order.
• It allows heterogeneous elements.
• It allows any number of null values.
• Its internal data structure is "Resizable Array".
• Its initial capacity is 10 elements.
• Its incremental capacity ration is new_Capacity = (Current_Capacity*3/2)+1
• It is best option for frequent retrieval operations.

• It is not synchronized.
• No method is synchronized method in ArrayList.
• It allows more than one thread to access data.
• It follows parallel execution.
• It will reduce execution time.
• It will improve application performance.
• It will not give guarantee for data consistency.
• It is not thread safe.
• It is not Legacy Collection.

ArrayList sample code depicting the most commonly used methods: (copy in VSCode or OnlibeGDB
for better understanding)

import java.util.ArrayList;
import java.util.Iterator;

public class ArrayListExample {

public static void main(String[] args) {


// Create an ArrayList to hold String elements
ArrayList<String> list = new ArrayList<>();

// Add elements to the ArrayList


list.add("Apple"); // Adds "Apple" to the end of the list
list.add("Banana"); // Adds "Banana" to the end of the list
list.add("Cherry"); // Adds "Cherry" to the end of the list

// Add an element at a specific index


list.add(1, "Blueberry"); // Adds "Blueberry" at index 1

// Print the ArrayList


System.out.println("ArrayList: " + list);

// Access elements by index


String firstElement = list.get(0); // Gets the element at index 0
System.out.println("First element: " + firstElement);

// Remove an element by index


list.remove(2); // Removes the element at index 2
System.out.println("After removal by index: " + list);

// Remove an element by object


list.remove("Banana"); // Removes the first occurrence of "Banana"
System.out.println("After removal by object: " + list);

// Get the size of the ArrayList


int size = list.size(); // Returns the number of elements in the list
System.out.println("Size of the list: " + size);

// Check if the ArrayList contains a specific element


boolean containsApple = list.contains("Apple"); // Checks if "Apple" is in the list
System.out.println("Contains Apple: " + containsApple);

// Iterate over the ArrayList using a for-each loop


System.out.println("Iterating using for-each loop:");
for (String fruit : list) {
System.out.println(fruit);
}

// Iterate over the ArrayList using an iterator


System.out.println("Iterating using iterator:");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}

// Check if the ArrayList is empty


boolean isEmpty = list.isEmpty(); // Returns true if the list is empty
System.out.println("Is the list empty: " + isEmpty);

// Clear all elements from the ArrayList


list.clear(); // Removes all elements from the list
System.out.println("After clearing the list: " + list);
}
}
add(Element e): Adds the element e to the end of the list.
add(int index, E element): Inserts the element element at the specified position index.
get(int index): Returns the element at the specified position index.
remove(int index): Removes the element at the specified position index.
remove(Object o): Removes the first occurrence of the specified element o.
size(): Returns the number of elements in the list.
contains(Object o): Checks if the list contains the specified element o.
isEmpty(): Checks if the list is empty.
clear(): Removes all elements from the list.
iterator(): Returns an iterator over the elements in the list.

Special points:

1. If we store integer values in ArrayList and then we perform list.remove(x) then the integer will be
treated as index and not element.,i.e., the number at index x will be removed and not element x.
2. No error is thrown if we apply list.clear() in empty list.

Some methods that are common to all collections framework:

.add()
.addAll()
.remove()
.removeAll()
.contains()
.containsAll();
.size()
.clear()
.isEmpty()
.toArray()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy