![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9326 Articles for Object Oriented Programming
![Mahesh Parahar](https://www.tutorialspoint.com/assets/profiles/127878/profile/60_2277600-1655382864.jpg)
518 Views
List provides a method size() to check the current size of the list.Syntaxint size()Returns the number of elements in this list. If this list contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.ReturnsThe number of elements in this list.ExampleThe following example shows how to get size of a list using size() method.package com.tutorialspoint; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { List list = new ArrayList(Arrays.asList(1, 2, 3, 4)); System.out.println("List: " + list); System.out.println("List Size: " + list.size()); list.add(5); ... Read More
![Mahesh Parahar](https://www.tutorialspoint.com/assets/profiles/127878/profile/60_2277600-1655382864.jpg)
4K+ Views
List provides a method contains() to check if list contains that element or not. It utilizes equals() method so we need to override the equals() method in the element type.Syntaxboolean contains(Object o)Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).Parameterso − Element whose presence in this list is to be tested.ReturnsTrue if this list contains the specified element.ThrowsClassCastException − If the type of the specified element is incompatible with this list (optional).NullPointerException − If the specified element ... Read More
![Mahesh Parahar](https://www.tutorialspoint.com/assets/profiles/127878/profile/60_2277600-1655382864.jpg)
2K+ Views
We can utilize Arrays.asList() method to get a List of specified elements in a single statement.Syntaxpublic static List asList(T... a)Returns a fixed-size list backed by the specified array. (Changes to the returned list "write through" to the array.)Type ParameterT − The runtime type of the array.Parametersa − The array by which the list will be backed.ReturnsA list view of the specified arrayIn case we use Arrays.asList() then we cannot add/remove elements from the list. So we use this list as an input to the ArrayList constructor to make sure that list is modifiable.ExampleThe following example shows how to create ... Read More
![Mahesh Parahar](https://www.tutorialspoint.com/assets/profiles/127878/profile/60_2277600-1655382864.jpg)
1K+ Views
We can insert element between two items of an array list easily using its add() method.Syntaxvoid add(int index, E element)Inserts the specified element at the specified position in this list (optional operation). Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).Type ParameterE − The runtime type of the element.Parametersindex − Index at which the specified element is to be inserted.e − Element to be inserted to this list.ThrowsUnsupportedOperationException − If the add operation is not supported by this listClassCastException − If the class of the specified element prevents it ... Read More
![Mahesh Parahar](https://www.tutorialspoint.com/assets/profiles/127878/profile/60_2277600-1655382864.jpg)
13K+ Views
SolutionYes, We can insert null values to a list easily using its add() method. In case of List implementation does not support null then it will throw NullPointerException.Syntaxboolean add(E e)Appends the specified element to the end of this list.Type ParameterE − The runtime type of the element.Parameterse − Element to be appended to this listReturnsIt returns true.ThrowsUnsupportedOperationException − If the add operation is not supported by this listClassCastException − If the class of the specified element prevents it from being added to this listNullPointerException − If the specified element is null and this list does not permit null elementsIllegalArgumentException − If some ... Read More
![Mahesh Parahar](https://www.tutorialspoint.com/assets/profiles/127878/profile/60_2277600-1655382864.jpg)
695 Views
SolutionWe can clear a list easily using its clear() method.Syntaxvoid clear()Removes all the elements of the list.ThrowsUnsupportedOperationException − If the clear operation is not supported by this listExampleThe following example shows how to clear elements from the list using the clear() method.package com.tutorialspoint; import java.util.ArrayList; import java.util.List; public class CollectionsDemo { public static void main(String[] args) { // Create a list object List list = new ArrayList(); // add elements to the list list.add(1); list.add(2); list.add(3); ... Read More
![Mahesh Parahar](https://www.tutorialspoint.com/assets/profiles/127878/profile/60_2277600-1655382864.jpg)
667 Views
SolutionWe can add element to an array list easily using its add() method.Syntaxboolean add(E e)Appends the specified element to the end of this list.Type ParameterE − The runtime type of the element to be added.Parameterse − Element to be appended to this listReturnsIt returns true.ThrowsUnsupportedOperationException − If the add operation is not supported by this listClassCastException − If the class of the specified element prevents it from being added to this listNullPointerException − If the specified element is null and this list does not permit null elementsIllegalArgumentException − If some property of this element prevents it from being added to ... Read More
![AmitDiwan](https://www.tutorialspoint.com/assets/profiles/123055/profile/60_187394-1565938756.jpg)
327 Views
In this article, we will understand how to convert array into collection. The Collection is a framework that provides architecture to store and manipulate the group of objects. Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.Below is a demonstration of the same −Suppose our input is −Input array: [Java, Python, Scala, Shell]The desired output would be −After elements after converting the array to a list are: [Java, Python, Scala, Shell]AlgorithmStep 1 - START Step 2 - Declare a string array namely input_array and a list namely result_list. ... Read More