How to add elements in ArrayList

In this post we will see various ways to add elements in ArrayList in java. ArrayList provides following methods to add elements. 1. [crayon-5f971021169d0246948548-i/] Adds an element at the end of the arrayList. Example [crayon-5f971021169e0747776194/] Output [crayon-5f971021169e7435529819/] 2. [crayon-5f971021169ec207261038-i/] Adds an element

Read More

How to Iterate ArrayList in java

In this post we will see how to iterate ArrayList in java. We can iterate ArrayList either using 1. For loop 2. Iterator. 3. While loop We will see examples for all these methods. 1. Iterate ArrayList in Java using For Loop

Read More

How to initialize ArrayList in java

We have many ways to initialize ArrayList in java. ArrayList can be initialized in one line, Using constructors, Using Arrays. 1. Initialize ArrayList using Constructors [crayon-5f97102117ee3053230815/] 2. Initialize ArrayList in One line using Arrays.asList() [crayon-5f97102117eec746430868/] 3. Initialize ArrayList using Collections (Another List) [crayon-5f97102117ef2526903202/] 4. Initialize ArrayList using InnerClass

Read More

String trim example

String trim method is used to remove the leading and trailing white spaces in a String. [crayon-5f971021183a8036487158/] String trim method example [crayon-5f971021183b0362126947/] Output [crayon-5f971021183b7041588094/] Reference 1. String trim() API

Read More

String concat example

Concat method of String in java is used to add a new String at the end of the original String. [crayon-5f971021187f0909468582/] String concat example [crayon-5f971021187f9519150434/] Output [crayon-5f971021187fe916158393/] Reference 1. String API

Read More

String endsWith example

String endsWith is used to check if a String ends with the given suffix. [crayon-5f97102118bfc095514723/] endsWith() method returns a boolean value based on the suffix passed String endsWith example [crayon-5f97102118c05192327441/] Output [crayon-5f97102118c0b908034292/] Reference 1. String API

Read More

String startsWith example

String startsWith method is used to check if a String starts with a specified substring. Startswith method has 2 variations and we will see both these. [crayon-5f9710211904c509410763/] [crayon-5f97102119055288654891-i/] checks if the String starts with the given prefix. [crayon-5f9710211905a781976247-i/] checks if the sub

Read More

String replace methods example

String replace methods are used to replace a part of string with a new string. We have 4 variations for replace in String [crayon-5f97102119617557712671/] [crayon-5f97102119620060730852-i/] returns a new String with the oldChar replaced with a newChar that was specified. [crayon-5f97102119625042326835-i/] returns a

Read More

String lastIndexOf example

String lastIndexOf returns the index of the last occurrence of the given String for specified conditions searching backward. String lastIndexOf method can be used with multiple types of parameters as shown below [crayon-5f97102119dbb329765861/] 1. [crayon-5f97102119dc5288442224-i/] returns the index of the last occurrence

Read More

String indexOf example

String indexOf returns the index of the given String for specified conditions. String indexOf method can be used with multiple types of parameters as shown below [crayon-5f9710211a3d5217646225/] 1. [crayon-5f9710211a3de214616357-i/] returns the index of the first occurrence of the specified character. 2. [crayon-5f9710211a3e3775534396-i/]

Read More