Showing posts with label util. Show all posts
Showing posts with label util. Show all posts

Wednesday, November 17, 2021

Java ArrayList Add() - How to add values to ArrayList?

How to add values to ArrayList?

In this post, we will learn how to add elements to ArrayList using java inbuilt methods. ArrayList class in java has been implemented based on the growable array which will be resized size automatically. Maintains the order of insertion of values added to it and permits adding all kinds of values including primitives, user-defined classes, wrapper classes and null values.

java arraylist add


Java ArrayList add methods:

Java ArrayList add method is overloaded and the following are the methods defined in it.

1) public boolean add(E e)
2) public void add(int index, E element)

Friday, April 26, 2019

Java ArrayList add method examples

Java ArrayList add method examples:

Introduction:

We will learn Java ArrayList add method with examples. Add method is used to add the specified values at last position of arraylist and at the specified position.

java.util.ArrayList is a class in Java which is implemented based on array concepts. It has many inbuilt methods to simplify the common functionalities.

In this post, we will see examples of how to use add method in ArrayList. Add method is used to add elements to it.

add method 

ArrayList has two add methods.
1. public boolean add(E e) - Adds the value after the last value in the arraylist.
2. public void add(int index, E element) - Adds the values at specified index. 
add() method first ensures that there is sufficient space in the arraylist. If list does not have space, then it grows the list by adding more spaces in underlying array. Then it add the element to specific array index.
Java ArrayList add method examples

Tuesday, April 9, 2019

java.util.Date in Java Example

java.util.Date in Java Example

Date class is inbuilt in Java API and package is java.util.Date.

Date class is used to get the current date, time or specific date time. This has methods to format and parse string dates. Implements Serializable, Cloneable, Comparable<Date>.


java.util.Date in Java Example

Constructor of Date class:


public Date()

Example program to get the current date or today's date:

Wednesday, January 9, 2019

Java 8 BiConsumer | Examples of BiConsumer accept and andThen methods

BiConsumer is a functional interface which is part of java.util.function widely used with collection api.

Package java.util.function is introduced in Java 8.



BiConsumer has two methods in it.


1) void accept(T t, U u)
2) BiConsumer<T,U>     andThen(BiConsumer<? super T,? super U> after)


accept() method is an abstract method and andThen() is a default method.

In this post, we will see how to use BiConsumer interface with examples. Please post your quires in the comment section.


Wednesday, December 6, 2017

Java ArrayList remove Example: How to remove by index, by Value/Object, for a specific range


How to remove a value from ArrayList in java with example programs.

In this post, we will learn how to program to remove elements from a ArrayList in java. Removing value can be done in three ways.

1) By index
2) By value or Object
3) For a given specific range

ArrayList api provides various methods to do remove operations.

java-arraylist-remove