Found 7442 Articles for Java

Java Program to Implement switch statement on strings

AmitDiwan
Updated on 30-Mar-2022 06:08:59

279 Views

In this article, we will understand how to implement switch statement on strings. The switch expression is evaluated once. The value of the expression is compared with the values of each case. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”).Below is a demonstration of the same −Suppose our input is −Input string: JavaThe desired output would be −Using siwtch cases: We shall use Java for our codingAlgorithmStep 1 - START Step 2 - Declare a string namely input_string. Step 3 - Define the values. Step 4 - Define a stwtch statement ... Read More

Java Program to Replace the Spaces of a String with a Specific Character

AmitDiwan
Updated on 30-Mar-2022 05:49:41

1K+ Views

In this article, we will understand how to replace the spaces of a string with a specific character. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”).Below is a demonstration of the same −Suppose our input is −Input string: Java Program is fun to learn Input character: $The desired output would be −The string after replacing spaces with given character is: Java$Program$is$fun$to$learnAlgorithmStep 1 - START Step 2 - Declare a string namely input_string, a char namely input_character. Step 3 - Define the values. Step 4 - Using the function replace(), replace the ... Read More

Java Program to Join Two Lists

AmitDiwan
Updated on 29-Mar-2022 13:50:24

343 Views

In this article, we will understand how to join two lists. The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.Below is a demonstration of the same −Suppose our input is −First list is defined as: [Java, Python] Second list: [Mysql, Redshift]The desired output would be −The list after joining two strings: [Java, Python, Mysql, Redshift]AlgorithmStep 1 - START Step 2 - Declare two lists ... Read More

Java Program to Access elements from a LinkedList

Aishwarya Naglot
Updated on 20-Aug-2025 12:26:31

453 Views

The Linked List in a Java is a data structure that is used for storing data in linear order. It has a series of node where each node contains two parts: data and a reference to the next node in the list. The first node is called the head and the last node is called the tail. In this article, we will learn how to access elements from a LinkedList in Java. Following are the ways to access elements from a LinkedList in Java: Using get() Method Using getFirst() Methods Using getLast() Method Accessing Elements from a LinkedList ... Read More

Java Program to Add elements to a LinkedList

AmitDiwan
Updated on 10-Aug-2023 12:14:07

364 Views

LinkedList is a generic class of Java Collection Framework that implements three interfaces namely List, Deque, and Queue. It provides the features of a LinkedList data structure which is a linear data structure where each element are linked with each other. There are several operations we can perform on LinkedList including appending, removing and traversing of elements. To add elements to a LinkedList Collection we can use various built-in methods such as add(), addFirst() and addLast(). We are going to explore how we can use these methods to add elements to a LinkedList. Adding elements to a LinkedList in Java ... Read More

Java Program to Find Common Elements in Two ArrayList

AmitDiwan
Updated on 04-Jul-2024 16:59:09

3K+ Views

In this article, we will learn how to find common elements in two array-list. The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arraysthat can grow as needed. Problem Statement In the given array, we aim to find the common elements in two ArrayList objects in Java, array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk. Below is a demonstration of the same − Input − First list: [Java, Scala, Shell, JavaScript] Second list: [Java, Python, Shell] Output ... Read More

Java program to convert a list of string to comma separated string

Aishwarya Naglot
Updated on 26-Aug-2025 16:44:22

806 Views

A list is a collection in Java which is used for storing group of elements. It is an ordered collection which also allows to store duplicate elements. A String is a sequence of characters which is used for representing text in Java. Here, in this article, we are given a list of strings, we have to convert it into a comma-separated string in Java using different approaches. To understand this problem, consider the following input-output scenarios: Scenario 1 Input: ["Apple", "Banana", "Mango", "Orange"] Output: Apple, Banana, Mango, Orange Scenario 2 Input: ["Red", "Green", "Blue"] Output: ... Read More

Java Program to Differentiate String == operator and equals() method

Revathi Satya Kondra
Updated on 06-Jan-2025 22:46:56

298 Views

In this article, we will understand how to differentiate == operator and equals() method in Java. The == (equal to) operator checks if the values of two operands are equal or not, if yes then the condition becomes true. The equals() method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object. Below is a demonstration of the same − Suppose our input is − The first string: abcde The second string: 12345 The ... Read More

Java Program to Remove a Sublist from a List

AmitDiwan
Updated on 29-Mar-2022 12:28:04

420 Views

In this article, we will understand how to remove a sub-list from a list. A list is an ordered collection that allows us to store and access elements sequentially. It contains the index-based methods to insert, update, delete and search the elements. It can also have the duplicate elements.Below is a demonstration of the same −Suppose our input is −Input list: [Java, Programming, Is, Fun]The desired output would be −The list after removing a sublist is: [Java, Programming]AlgorithmStep 1 - START Step 2 - Declare an AbstractList namely input_list. Step 3 - Add the values to the list. Step 4 ... Read More

Java program to iterate through each character of the string.

AmitDiwan
Updated on 29-Sep-2024 02:48:31

2K+ Views

In this article, we will understand how to iterate through each character of the string using Java. The string is a datatype that contains one or more characters and is enclosed in double quotes (“ ”). Char is a datatype that contains an alphabet an integer or a special character. Problem Statement Write a program to Iterate through each character of the string. Below is a demonstration of the same − Input The string is defined as: Java Program Output The characters in the string are: J, a, v, a, , P, r, o, g, r, a, m, Different approaches ... Read More

Advertisements