An element in ArrayList can be replaced using the ListIterator method set(). This method has a single parameter i.e. the element that is to be replaced and the set() method replaces it with the last element returned by the next() or previous() methods.A program that demonstrates this is given as follows.Example Live Demoimport java.util.ArrayList; import java.util.ListIterator; public class Demo { public static void main(String[] args) { ArrayList aList = new ArrayList(); aList.add("Amanda"); aList.add("Taylor"); aList.add("Justin"); aList.add("Emma"); aList.add("Peter"); System.out.println("The ArrayList elements ... Read More
Before getting into an example, we should know what thread is. A thread is a lightweight sub-process, it going to do background operations without interrupt to ui. This example demonstrate about How to get getAllStackTraces of current thread in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken textview. It contains information about current thread stack trace.Step 3 − Add the following code to src/MainActivity.javapackage ... Read More
A ListIterator can be used to traverse the elements in the forward direction as well as the reverse direction in a LinkedList.The method hasNext( ) in ListIterator returns true if there are more elements in the LinkedList while traversing in the forward direction and false otherwise. The method next( ) returns the next element in the LinkedList and advances the cursor position.The method hasPrevious( ) in ListIterator returns true if there are more elements in the LinkedList while traversing in the reverse direction and false otherwise. The method previous( ) returns the previous element in the LinkedList and reduces the ... Read More
Before getting into example, we should know what thread is. A thread is a lightweight sub-process, it going to do back ground operations without interrupt to ui. This example demonstrate about How to get current thread state in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken textview. It contains information about the current thread state.Step 3 − Add the following code to src/MainActivity.javapackage com.example.myapplication; ... Read More
Before getting into example, we should know what thread is. A thread is a lightweight sub-process, it going to do back ground operations without interrupt to ui. This example demonstrate about How to get current thread priority in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken textview. It contains information about current thread priority.Step 3 − Add the following code to src/MainActivity.javapackage com.example.myapplication; ... Read More
A method which does not have body is known as abstract method. It contains only method signature with a semi colon and, an abstract keyword before it.public abstract myMethod();To use an abstract method, you need to inherit it by extending its class and provide implementation to it.Declaring an abstract method privateIf a method of a class is private, you cannot access it outside the current class, not even from the child classes of it.But, incase of an abstract method, you cannot use it from the same class, you need to override it from subclass and use.Therefore, the abstract method cannot ... Read More
An object array can be created from the elements of a LinkedList using the method java.util.LinkedList.toArray(). This method returns the object array with all the LinkedList elements in the correct order.A program that demonstrates this is given as follows.Example Live Demoimport java.util.LinkedList; public class Demo { public static void main(String[] args) { LinkedList l = new LinkedList(); l.add("Amy"); l.add("Sara"); l.add("Joe"); l.add("Betty"); l.add("Nathan"); Object[] objArr = l.toArray(); System.out.println("The object array elements are: "); for ... Read More
Interface in Java is similar to class but, it contains only abstract methods and fields which are final and static.Since all the methods are abstract you cannot instantiate it. To use it, you need to implement this interface using a class and provide body to all the abstract methods int it.Concrete methods in an interfaceAll the methods in an interface must be abstract, you cannot have a concrete method (the one which has body) if you try to do so, it gives you a compile time error saying “interface abstract methods cannot have body”.ExampleIn the following Java program, we are ... Read More
The elements of a Collection can be inserted at the specified index of the ArrayList using the method java.util.ArrayList.addAll(). This method has two parameters i.e. the specific index at which to start inserting the Collection elements in the ArrayList and the Collection itself.If there is an element already present at the index specified by ArrayList.addAll() then that element and all subsequent elements are shifted to the right to make the space for the Collection elements in the ArrayList.A program that demonstrates this is given as follow.Example Live Demoimport java.util.ArrayList; import java.util.Vector; public class Demo { public static void main(String[] args) ... Read More
Before getting into an example, we should know what thread is. A thread is a lightweight sub-process, it going to do background operations without interrupt to ui. This example demonstrate about How to get current thread name in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken textview. It contains information about current thread name.Step 3 − Add the following code to src/MainActivity.javapackage com.example.myapplication; ... Read More