Programming Articles - Page 2468 of 3363

Print the last occurrence of elements in array in relative order in C Program.

Sunidhi Bansal
Updated on 22-Aug-2019 06:07:00

351 Views

Given an array a[] with elements and the task is to print the last occurrences of the given elements in the list. Here we not only have to remove the duplicate elements but also we have to maintain the order of the occurrences of the elements in an array as per the last time they have occurred.Like we have an array of 6 elements also containing some duplicate values i.e., {1, 3, 2, 3, 1, 2} so the result should be in form of 3 1 2.ExampleInput: a[]={4, 2, 2, 4, 1, 5, 1} Output : 2 4 5 1AlgorithmSTART ... Read More

Print reverse of a Linked List without extra space and modification in C Program.

Sunidhi Bansal
Updated on 22-Aug-2019 05:57:17

506 Views

The task is to print the nodes starting from the end of the linked list without using extra space which means there shouldn’t be any extra variable instead the head pointer pointing the first node will be moved.ExampleInput: 10 21 33 42 89 Output: 89 42 33 21 10There can be many solutions to print the linked list in reverse order, like recursive approach (use extra space), reverse the linked list(requires modification in the given Linked List), pushing the elements on a stack and then pop and display the elements one by one(requires space O(n)), but these solutions seem to ... Read More

How can we call the invokeLater() method in Java?

raja
Updated on 02-Jul-2020 08:12:57

4K+ Views

An invokeLater() method is a static method of the SwingUtilities class and it can be used to perform a task asynchronously in the AWT Event dispatcher thread. The SwingUtilities.invokeLater() method works like SwingUtilities.invokeAndWait() except that it puts the request on the event queue and returns immediately. An invokeLater() method does not wait for the block of code inside the Runnable referred by a target to execute.Syntaxpublic static void invokeLater(Runnable target)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class InvokeLaterTest extends Object {    private static void print(String msg) {       String name = Thread.currentThread().getName();       System.out.println(name + ": " + msg);    }   ... Read More

What is the use of Thread.sleep() method in Java?

raja
Updated on 27-Nov-2023 09:23:59

5K+ Views

The sleep() method is a static method of Thread class and it makes the thread sleep/stop working for a specific amount of time. The sleep() method throws an InterruptedException if a thread is interrupted by other threads, that means Thread.sleep() method must be enclosed within the try and catch blocks or it must be specified with throws clause. Whenever we call the Thread.sleep() method, it can interact with the thread scheduler to put the current thread to a waiting state for a specific period of time. Once the waiting time is over, the thread changes from waiting state to runnable state. Syntax public ... Read More

Importance of join() method in Java?

Vivek Verma
Updated on 08-May-2025 14:06:51

3K+ Views

The join() Method In Java, a join() is a final method of the Thread class, and it can be used to join the start of a thread's execution to the end of another thread's execution so that a thread will not start running until another thread has ended. If the join() method is called on a thread instance, the currently running thread will be blocked until the thread instance has finished its execution. The join() method in Java is important in multithreading because it allows one thread to wait for the completion of another thread before continuing execution. Syntax Following is ... Read More

What is the purpose of overriding a finalize() method in Java?

Vivek Verma
Updated on 27-May-2025 15:07:58

2K+ Views

In Java, overriding is a technique used to provide a specific implementation of a method that is already defined in a superclass (i.e., parent class), allowing a subclass (i.e., child class) to modify or extend the behavior of that method. Purpose of Overriding the finalize() Method The purpose of a finalize() method can be overridden for an object to include the cleanup code or to dispose of the system resources that can be done before the object is garbage collected. If we are overriding this method, then we need to call the finalize() method explicitly. The finalize() method can be ... Read More

How to implement a rollover effect for a JButton in Java?

raja
Updated on 02-Jul-2020 07:04:18

1K+ Views

A JButton is a subclass of AbstractButton and it can be used for adding platform-independent buttons to a GUI application. A JButon can generate an ActionListener interface when the button is pressed or clicked, it can also generate the MouseListener and KeyListener interfaces. We can implement the rollover effect when the mouse moves over a JButton by overriding the mouseEntered() method of the MouseListener interface.Syntaxvoid mouseEntered(MouseEvent e)Exampleimport java.awt.*; import java.awt.event.*; import javax.swing.*; public class RollOverButtonTest extends JFrame {    private JButton button;    public RollOverButtonTest() {       setTitle("RollOverButton Test");       button = new JButton("Rollover Button");       button.addMouseListener(new MouseAdapter() {     ... Read More

How can we avoid a deadlock in Java?

Vivek Verma
Updated on 14-May-2025 14:07:06

5K+ Views

This article explains several strategies to avoid deadlock in Java, including a brief introduction to deadlock, strategies, and respective examples. Deadlock in Java In case of multi-threading, a deadlock is a programming situation where two or more threads are holding resources needed by other threads and are blocked forever, waiting for each other (to release the required resource). A deadlock condition will occur with at least two threads and two or more resources.How To Avoid Deadlock in Java? Following is a list of strategies to avoid the deadlock in Java: ... Read More

Binary Search using pthread in C Program?

Arnab Chakraborty
Updated on 20-Aug-2019 13:18:47

597 Views

We know that the binary search approach is one of the most suitable and effective sorting algorithm. This works on sorted sequence. The algorithm is simple, it simply finds the element from middle, then divide the list by two parts, and moves either towards the left sublist, or right sublist.We know the algorithm of it. Now we will see how to use binary search technique in multithreading environment. The number of threads depends on number of cores are present in the system. Let us see the code to get the idea.Example#include #define MAX 16 #define MAX_THREAD 4 using namespace ... Read More

Binary representation of next greater number with same number of 1’s and 0’s in C Program?

Arnab Chakraborty
Updated on 20-Aug-2019 13:15:25

319 Views

Suppose we have one binary number, that is representation of a number n. We have to find binary representation of a number which is smallest but larger than n, and it also has same number of 0s and 1s. So if the number is 1011 (11 in decimal), then the output will be 1101 (13). This problem can be found using the next-permutation calculation. Let us see the algorithm to get the idea.AlgorithmnextBin(bin) −Begin    len := length of the bin    for i in range len-2, down to 1, do       if bin[i] is 0 and bin[i+1] ... Read More

Advertisements