Found 33676 Articles for Programming

How to get Sublist of an ArrayList using Java?

Vivek Verma
Updated on 21-May-2025 16:49:38

2K+ Views

In Java, an ArrayList is a class of the List interface, which stores elements of similar data types and can be null while creating. A sub-list is the view of a portion (or part) of an ArrayList. For example, if the given array list is {1, 2, 3, 4, 5}, then the possible sub-lists can be {1, 2}, {1, 2, 3}, {2, 3, 4}, {4, 5}, etc.The List interface in Java provides a built-in method named subList(), which directly returns a sub-list from the given ArrayList. Sublist from an ArrayList using the subList() Method The subList() method of the List interface returns a portion ... Read More

How to find an element in a List with Java?

Vivek Verma
Updated on 16-May-2025 19:20:46

24K+ Views

In Java, a List is an interface that extends the Collection interface and represents a sequence of elements. Since the List is an interface. To create a List object, we need to instantiate a class that implements the List interface, such as ArrayList.  The List provides various methods that help to check or find the element in it. We will discuss those methods in the coming section with suitable examples. Below is a list of various ways to find an element in the Java List: Using the get() Method Using ... Read More

How to convert between List and Array in Java?

Vivek Verma
Updated on 27-May-2025 14:31:46

1K+ Views

In Java, a List is an interface that represents an ordered collection of elements of the same type. You cannot directly create an object of the List interface, instead, you need to instantiate an ArrayList class or another class that implements the List interface.An array is a container that holds a fixed size of similar types of data. As the size is fixed, it can not be changed once created.The conversion between List and array is important because array provides faster access to the elements than List interface (faster in case of searching elements), and provides a fixed size of ... Read More

C++ Program to remove Characters from a Numeric String Such That String Becomes Divisible by 8

Prateek Jangid
Updated on 18-May-2022 11:54:33

249 Views

Given a number in the form of a string, we need to find where to make it divisible by eight after deleting zero or more elements. In other words, we need to find whether there is a subsequence of the string, which is divisible by 8. Return the modified string or -1 if it is not possible.Any number whose last three digits are divisible by 8 is also divisible by 8. For example, 56992992 and 476360 are divisible by 8, but 2587788 is not. If the result is a whole number, then the original number is divisible by 8.We can ... Read More

Remove All Nodes Which Don't Lie in Any Path With Sum>=k using C++

Prateek Jangid
Updated on 18-May-2022 11:48:55

228 Views

In this problem, we have a binary tree with a path from the root node to the leaf node that is completely defined. The sum of all nodes from a root node to a leaf node must be greater than or equal to k. So we need to remove all nodes in the paths whose sum is less than k. An important thing to remember here is that a node may be a part of many paths, so only remove such nodes if all the paths have sum < k.From the root node to the leaf node, we can calculate ... Read More

How to convert an Array to a List in Java program?

Vivek Verma
Updated on 27-May-2025 14:23:13

664 Views

In Java, an array is a container object that holds a similar type of data (elements), but has a fixed size. You can declare it by assigning values in curly braces or creating it using the new keyword by specifying the size. A List in Java is an interface that represents a sequence of elements of the same type (duplicates are allowed) and has a dynamic size. To create an object of the List interface, you can use one of its classes, such as ArrayList, Stack, Vector, etc.Converting an Array to a List in JavaSometimes we need to insert more data ... Read More

How to check if ArrayList contains an item in Java?

Vivek Verma
Updated on 19-May-2025 15:27:41

5K+ Views

To check if an ArrayList contains a specific item, we can either compare each element with the given item and print the result if they are equal, or use a predefined method that directly checks for the containing element. ArrayList in Java In Java, an ArrayList is a class similar to an Array, but unlike an array, it has a dynamic size that automatically increases or decreases, depending on the number of elements added or removed from it. Following is the syntax for creating an ArrayList in Java - ArrayList list_name = new ArrayList(); Here, ... Read More

How does a list work in Java?

Mahesh Parahar
Updated on 10-May-2022 08:42:04

209 Views

public interface List extends CollectionThe List interface extends Collection and declares the behavior of a collection that stores a sequence of elements.Elements can be inserted or accessed by their position in the list, using a zero-based index.A list may contain duplicate elements.In addition to the methods defined by Collection, List defines some of its own, which are summarized in the following table.Several of the list methods will throw an UnsupportedOperationException if the collection cannot be modified, and a ClassCastException is generated when one object is incompatible with another.ExampleThe above interface has been implemented in various classes like ArrayList or LinkedList, ... Read More

How do you turn an ArrayList into a Set in Java?

Aishwarya Naglot
Updated on 30-May-2025 18:10:34

11K+ Views

In this article, let's learn how to convert an ArrayList into a Set in Java. ArrayList is a collection that allows us to store duplicates, whereas Set is a collection that does not allow duplicates. So, when we convert an ArrayList to a Set, all the duplicate elements will be removed. If someone tries to add duplicate elements to a Set, it will not throw an error but will simply ignore the duplicate elements. Let's take an example: Input: [1, 2, 3, 4, 5, 5, 6] Output: [1, 2, 3, 4, 5, 6] Ways to Convert an ArrayList to ... Read More

How do you search for an element in an ArrayList in Java?

Aishwarya Naglot
Updated on 30-May-2025 18:29:25

7K+ Views

In this article, we will learn how to search for an element in an ArrayList in Java. ArrayList is an interface that extends another interface called the List interface. It provides a way to store elements and also provides resizable functionality. Let's take an example: Input: [1, 2, 3, 4, 5] Element to search: 3 Output: Element 3 found at index 2 Ways to Search for an Element in an ArrayList Below are the different approaches to searching for an element in an ArrayList: Using indexOf() method Using contains() method Using for loop Using Stream API ... Read More

Advertisements