Java Articles

Page 32 of 450

How to remove the redundant elements from an ArrayList object in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 549 Views

The interface set does not allow duplicate elements. The add() method of this interface accepts elements and adds to the Set object, if the addition is successful it returns true if you try to add an existing element using this method, the addition operations fails to return false.Therefore, to remove redundant elements of an ArrayList object −Get/create the required ArrayList.Create an empty set object.Try to add all the elements of the ArrayList object to set objectives.Clear the contents of the ArrayList using the clear() method.Now, using the addAll() method add the contents of the set object to the ArrayList again.Exampleimport ...

Read More

How to insert an object in an ArrayList at a specific position in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 1K+ Views

The add() method of the ArrayList class helps you to add elements to an array list. It has two variants −add(E e) − This method accepts an object/elements as a parameter and adds the given element at the end of the list.public void add(int index, E element) − This method accepts an element and an integer value representing the position at which we need to insert it and inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).Therefore ...

Read More

Difference between peek(), poll() and remove() method of Queue interface in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 16K+ Views

This represents a collection that is indented to hold data before processing. It is an arrangement of the type First-In-First-Out (FIFO). The first element put in the queue is the first element taken out from it.The peek() methodThe peek() method returns the object at the top of the current queue, without removing it. If the queue is empty this method returns null.Exampleimport java.util.Iterator; import java.util.LinkedList; import java.util.Queue; public class QueueExample {    public static void main(String args[]) {       Queue queue = new LinkedList();       queue.add("Java");       queue.add("JavaFX");       queue.add("OpenCV");     ...

Read More

Is parent child hierarchy important on throws while overriding in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 393 Views

When you try to handle an exception (checked) thrown by a particular method, you need to catch it using the Exception class or super class of the Exception occurred.In the same way while overriding the method of a super class, if it throws an exception −The method in the sub-class should throw the same exception or its sub type.The method in the sub-class should not throw its super type.You can override it without throwing any exception.When you have three classes named Demo, SuperTest and, Super in (hierarchical) inheritance, if Demo and SuperTest have a method named sample().Exampleclass Demo {   ...

Read More

What is meant by re-throwing exceptions in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 5K+ Views

When an exception is cached in a catch block, you can re-throw it using the throw keyword (which is used to throw the exception objects).While re-throwing exceptions you can throw the same exception as it is without adjusting it as −try {    int result = (arr[a])/(arr[b]);    System.out.println("Result of "+arr[a]+"/"+arr[b]+": "+result); } catch(ArithmeticException e) {    throw e; }Or, wrap it within a new exception and throw it. When you wrap a cached exception within another exception and throw it, it is known as exception chaining or, exception wrapping, by doing this you can adjust your exception, throwing a ...

Read More

How to avoid ConcurrentModificationException while iterating a collection in java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 422 Views

When you are working with collection objects, while one thread is iterating over a particular collection object, if you try to add or remove elements from it, a ConcurrentModificationException will be thrown.Not only that, If you are iterating a collection object, add or remove elements to it and try to iterate its contents again it is considered that you are trying to access the collection object using multiple threads and ConcurrentModificationException is thrown.Exampleimport java.util.ArrayList; import java.util.Iterator; public class OccurenceOfElements {    public static void main(String args[]) {       ArrayList list = new ArrayList();       //Instantiating ...

Read More

What is the difference between getter/setter methods and constructor in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 13K+ Views

ConstructorsA constructor in Java is similar to method and it is invoked at the time creating an object of the class, it is generally used to initialize the instance variables of a class. The constructors have the same name as their class and, have no return type.If you do not provide a constructor the compiler defines one on your behalf, which initializes the instance variables with default values.You can also accept parameters through constructors and initialize the instance variables of a class using the given values, these are known as parameterized constructors.ExampleThe following Java program has a class named student ...

Read More

Can we initialize static variables in a default constructor in Java?

Maruthi Krishna
Maruthi Krishna
Updated on 11-Mar-2026 8K+ Views

Class/static variables belong to a class, just like instance variables they are declared within a class, outside any method, but, with the static keyword.They are available to access at the compile time, you can access them before/without instantiating the class, there is only one copy of the static field available throughout the class i.e. the value of the static field will be same in all objects. You can define a static field using the static keyword.If you declare a static variable in a class, if you haven’t initialized it, just like with instance variables compiler initializes these with default values ...

Read More

What is deep copy? Explain with an example in Java.

Venkata Sai
Venkata Sai
Updated on 11-Mar-2026 7K+ Views

Creating an exact copy of an existing object in the memory is known as cloning.The clone() method of the class java.lang.Object accepts an object as a parameter, creates and returns a copy of it (clones).In order to use this method, you need to make sure that your class implements the Cloneable interface.Exampleimport java.util.Scanner; public class CloneExample implements Cloneable {    private String name;    private int age;    public CloneExample(String name, int age){       this.name = name;       this.age = age;    }    public void displayData(){       System.out.println("Name : "+this.name);       ...

Read More

Copy all elements of ArrayList to an Object Array in Java

Samual Sam
Samual Sam
Updated on 11-Mar-2026 3K+ Views

All the elements of an ArrayList can be copied into an Object Array using the method java.util.ArrayList.toArray(). This method does not have any parameters and it returns an Object Array that contains all the elements of the ArrayList copied in the correct order.A program that demonstrates this is given as follows −Exampleimport java.util.ArrayList; import java.util.List; public class Demo {    public static void main(String[] args) {       List aList = new ArrayList();       aList.add("Nathan");       aList.add("John");       aList.add("Susan");       aList.add("Betty");       aList.add("Peter");       Object[] objArr = ...

Read More
Showing 311–320 of 4,498 articles
« Prev 1 30 31 32 33 34 450 Next »
Advertisements