Found 2616 Articles for Java

Can we insert null values in a Java list?

Mahesh Parahar
Updated on 09-May-2022 11:23:46

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

How do I insert elements at a specific index in Java list?

Mahesh Parahar
Updated on 09-May-2022 09:03:52

514 Views

SolutionWe can add all elements of one list into another list at a specified index easily using its addAll() method.Syntaxboolean addAll(int index, Collection

How do I insert all elements from one list into another in Java?

Mahesh Parahar
Updated on 09-May-2022 09:00:35

5K+ Views

SolutionWe can add all elements of one list into another list easily using its addAll() method.Syntaxboolean addAll(Collection

How do I empty a list in Java?

Mahesh Parahar
Updated on 09-May-2022 08:57:11

703 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

How do I add an element to an array list in Java?

Mahesh Parahar
Updated on 09-May-2022 08:51:37

672 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

Java Program to Convert the local Time to GMT

AmitDiwan
Updated on 30-Mar-2022 12:12:28

2K+ Views

In this article, we will understand how to convert the local Time to GMT. Java does not have a built-in Date class, but we can import the java.time package to work with the date and time API. The package includes many date and time classes.Below is a demonstration of the same −Suppose our input is −The local time is: Fri Mar 18 00:01:54 IST 2022The desired output would be −The time in Gmt is: 17/03/2022 18:31:54AlgorithmStep 1 - START Step 2 - Declare an object of LocalDateTime namely date. Step 3 - Define the values. Step 4 - Define different ... Read More

Java Program to Display time in different country’s format

AmitDiwan
Updated on 30-Mar-2022 12:10:09

440 Views

In this article, we will understand how to display time in different country’s format. Java does not have a built-in Date class, but we can import the java.time package to work with the date and time API. The package includes many date and time classes.Below is a demonstration of the same −Suppose our input is −Run the programThe desired output would be −The England Format is: Friday, 18 March 2022 The Italian Format is: venerdì, 18 marzo 2022AlgorithmStep 1 - START Step 2 - Declare an object of LocalDateTime namely date. Step 3 - Define the values. Step 4 - ... Read More

Java Program to Display Dates of Calendar Year in Different Format

AmitDiwan
Updated on 30-Mar-2022 12:00:29

275 Views

In this article, we will understand how to display dates of calendar year in different format. Java does not have a built-in Date class, but we can import the java.time package to work with the date and time API. The package includes many date and time classes.Below is a demonstration of the same −Suppose our input is −Run the programThe desired output would be −The first date format is:2022-03-17T23:37:37.623304800 The second date format is:17/03/2022 The third date format is:Thursday, 17 Mar 2022AlgorithmStep 1 - START Step 2 - Declare an object of LocalDateTime namely date. Step 3 - Define the ... Read More

Java Program to Iterate over ArrayList using Lambda Expression

AmitDiwan
Updated on 30-Mar-2022 11:51:45

2K+ Views

In this article, we will understand how to iterate over ArrayList using lambda expression. The ArrayList class is a resizable array, which can be found in the java.util package. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified.Below is a demonstration of the same −Suppose our input is −Run the programThe desired output would be −The list is defined as: Java Python Scala Mysql RedshiftAlgorithmStep 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Create an ArrayList, and iterate over ... Read More

Java Program to Pass ArrayList as the function argument

AmitDiwan
Updated on 30-Mar-2022 11:42:18

4K+ Views

In this article, we will understand how to pass ArrayList as the function argument. The ArrayList class is a resizable array, which can be found in the java.util package. The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified.Below is a demonstration of the same −Suppose our input is −Run the programThe desired output would be −The list is defined as: Java Python Scala Mysql RedshiftAlgorithmStep 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Create an ArrayList, and iterate over ... Read More

Advertisements