Found 9150 Articles for Object Oriented Programming

How to make elements of array immutable in Java?

Maruthi Krishna
Updated on 15-Oct-2019 06:44:33

3K+ Views

No, you cannot make the elements of an array immutable.But the unmodifiableList() method of the java.util.Collections class accepts an object of the List interface (object of implementing its class) and returns an unmodifiable form of the given object. The user has only read-only access to the obtained list.And the asList() method of the ArrayList class accepts an array and returns a List object.Therefore, to convert an array immutable −Obtain the desired array.Convert it into a list object using the asList() method.Pass the obtained list as a parameter to the unmodifiableList() method.Example Live Demoimport java.util.Arrays; import java.util.Collections; import java.util.List; public class UnmodifiableExample ... Read More

How can we convert a JSONArray to String Array in Java?

Manisha Chand
Updated on 06-Jun-2025 12:43:59

4K+ Views

A JSONArray is a class provided by the org.json package that represents a collection of JSON values. These values can be of any type, such as strings, numbers, booleans, or even nested objects or arrays. If you do not know what JSON is, then you can read the JSON tutorial. Converting JSON Array to String ArrayWe can convert a JSONArray to String Array by using a simple loop as shown in the example below - import org.json.*; import java.util.*; public class JsonArraytoStringArrayTest { public static void main(String[] args) { ... Read More

How to convert a List to JSON array using the Jackson library in Java?

Manisha Chand
Updated on 29-Apr-2025 12:24:45

11K+ Views

JSON is used in Java applications in APIs, file storage, and data communication between systems. Sometimes, we need to convert a list into a JSON array. In this article, we will learn how to convert a list to JSON array using the Jackson library. Jackson Library It is a library that is used in Java to work with JSON data. It provides APIs to serialize Java objects into JSON and deserialize JSON back into Java objects. If you want to read more about the Jackson library, you can refer Jackson library. There are mainly three components of Jackson - ... Read More

Can we override only one method while implementing Java interface?

Maruthi Krishna
Updated on 22-Apr-2025 11:31:34

1K+ Views

An interface in Java is a specification of method prototypes. Whenever you need to guide the programmer or make a contract specifying how the methods and fields of a type should be, you can define an interface. When a class implements an interface, it should provide concrete implementations for all of its abstract methods. But is it possible to override only one method from an interface? Overriding a Method from an Interface In Java, all methods in an interface are abstract (unless declared as default or static). When a class implements an interface, it must override those abstract methods. No, ... Read More

How to Sort a String in Java alphabetically in Java?

Maruthi Krishna
Updated on 22-Apr-2025 11:29:01

90K+ Views

Sorting a string alphabetically means rearranging its characters in order, from A to Z. For example, the string "java" becomes "aajv" after sorting. Different ways to sort a String Alphabetically There are two ways to do this in Java using different approaches - Using toCharArray() and Arrays.sort() Method Sorting the array manually Using toCharArray() and Arrays.sort() Method The toCharArray() method of this ... Read More

In how many ways you can retrieve the elements of a collection in Java?

Maruthi Krishna
Updated on 22-Apr-2025 11:25:46

3K+ Views

In Java, the collections framework provides various classes like ArrayList, HashSet, and LinkedList to store groups of elements. But once data is stored, how do you access or retrieve it? Java provides multiple ways to retrieve elements from collections, depending on whether you're reading values, modifying them during traversal, or iterating forward or backward. You can retrieve the elements of a collection in four ways- Using For-Loop Using For-Each Loop ... Read More

How many ways are there to convert an Array to ArrayList in Java?

Maruthi Krishna
Updated on 22-Apr-2025 11:23:59

357 Views

When working with collections in Java, you might need to convert an array to an ArrayList. This is useful because ArrayList offers more flexibility, such as dynamic sizing and built-in methods for adding, removing, and searching elements. Need for Converting an Array to an ArrayList Arrays in Java have a fixed size and provide limited functionality. ArrayList, on the other hand, is part of the Java Collections Framework and provides methods to modify data dynamically. It is useful when - Need to use dynamic Collections. ... Read More

Remove all non-alphabetical characters of a String in Java?

Maruthi Krishna
Updated on 22-Apr-2025 11:22:42

2K+ Views

When you are working with strings in Java, you may want to keep only the alphabetic characters (A-Z, a-z) and remove everything else, like numbers, punctuation, and symbols. Java gives you multiple ways to do this for different cases or scenarios. Removing non-alphabetical characters from a string There are different methods, and they are - Using split() method Using replaceAll() method ... Read More

How to remove an element from ArrayList or, LinkedList in Java?

Maruthi Krishna
Updated on 16-Jun-2025 19:03:21

848 Views

The ArrayList and LinkedList classes implement the List interface of the java.util package. This interface provided two variants of the remove() method to remove particular elements, as shown below - Using remove(int index) Using remove(Object o) There are two additional removal approaches provided by the Collection and Iterator interfaces. They are - ... Read More

How to make an ArrayList read only in Java?

Maruthi Krishna
Updated on 16-Apr-2025 11:20:17

906 Views

A collection is an object that holds a reference to other objects. A Java array list is one of the collections. it is a resizable array and is represented by the class named ArrayList. This java.util.Collections class provides methods to manipulate the collection objects in Java. We can make an ArrayList read-only using the Collections.unmodifiableList() method. Need to make a ArrayList Read-Only Sometimes we may need to make the ArrayList object read-only - To protect data from unnecessary or unwanted changes. ... Read More

Advertisements