Double dimension array sorting in java
How to sort a two-dimensional array in Java
We can sort two-dimension arrays in Java easily. Here we have to convert the 2D array into a single dimension array. And do the sorting. In the following, we have applied the bubble sort technique.
Linear Search in Java with Multiple Values
linear search in Java with multiple values |
Linear search in Java
Linear search in Java: Java program to search name and print name and its corresponding other value by Linear Search
stack in python
How to Implement a Python Stack
Introduction to
stacks and Queue data structures, which organized computer storage for
efficient access and manipulation of data.
The stacks and queue
are very simple to learn and execute with ease, and their usage is very common.
Stacks follows the
LIFO principle that is last in first out. The last element will be removed or
pop first.
Two operations are
therein, one is pushed, which adds an element to the top of the stack, and the second one is pop, which removes the element at the top of the stack.
So let us begin with implementing stacks, using lists in python. Let us first create an empty list name[].
We are going to add elements to the
list, name [] using the append () method.
# create an empty list name
name=[]
# add elements into the list with
append method in the list as Stack
name.append('Siraz')
name.append('Dhoni')
name.append('Shami')
name.append('Kholi')
name.append('Rahul')
name.append('Bumrah')
print(name)
Adding elements to the list using the append() method ensures the push operation of the stack.
Now let us try removing elements
from the list.
name.pop()
It will delete the Bumrah as it is
entered last
['Siraz', 'Dhoni', 'Shami', 'Kholi',
'Rahul']
Let us try to delete the item again
from the list for which we have to write the same syntax
name.pop()
The above code deletes Rahul from
the list, and we are left with only
['Siraz', 'Dhoni', 'Shami', 'Kholi']
Stack using deque in python
Implementing push with the help of
append() method of the deque, We have to import deque from python collection
library as follows
# We can initialize a deque with a
list
name = deque()
# Use append like before to add
elements
name.append ('Siraz')
name.append ('Dhoni')
name.append ('Shami')
name.append ('Kholi')
name.append ('Rahul')
name.append ('Bumrah')
so when we print the print(name), it will
print as follows
deque(['Siraz', 'Dhoni', 'Shami',
'Kholi', 'Rahul', 'Bumrah'])
Now using pop() of deque, we can
implement stack pop operation as follows
name.pop()
print (name)
deque(['Siraz', 'Dhoni', 'Shami',
'Kholi', 'Rahul'])
The last element is Bumrah is deleted
first
name.pop()
print (name)
deque(['Siraz', 'Dhoni', 'Shami',
'Kholi'])
The second last element Rahul is
removed second and so on.
Implementation using python queue module
In the Python queue module there is a LIFO
Queue, which is basically nothing but Stack. Element is inserted into the queue
using put() method that act as PUSH and get () remove data out from the queue
that act as POP operation of stack.
from queue import LifoQueue
# Initializing the stack with
maxsize is 10
stack = LifoQueue(maxsize = 10)
# qsize() show the number of
elements
# in the stack
print(“Initial size of ”stack.qsize())
# put() function to push
# element in the stack
stack.put ('Siraz')
stack.put ('Dhoni')
stack.put ('Shami')
stack.put ('Kholi')
stack.put ('Rahul')
stack.put ('Bumrah')
print("Full is over or not :
", stack.full())
print("Total size of the stack:
", stack.qsize())
# This get() method to act as pop
operation of stack in
# LIFO order that is (Last in First
Out)
print ('\nPoped operation from the
stack')
print (stack.get())
print (stack.get())
print (stack.get())
print("\nStack Empty or not :
", stack.empty())
Output
Initial size of 0
Full: False
Size: 6
Elements poped from the stack
Bumrah
Rahul
Kholi
Empty: False
Python Articles
Multithreading in Java with example
Multithreading in Java
When we want to
run multiple programs or tasks simultaneously is known as multitasking. For
example running VLC, Word, and Browser at a given time is an example of
multitasking. Every program in memory is known as a process and each process
has taken a single unit of time.
- Multiprocessing which is based on process and associated with operating system level.
- Multitasking is based on thread and it is associated with programmer.
What is Thread in java?
A thread is a subpart of a
process which lightweight and it is the smallest sequence of a program or a
process. Each thread has some priority and the higher the priority higher the chances
of executing preference.
The life cycle of thread
The JVM controlled the life cycle of a thread and it has five
states 1. New, 2. Runnable, 3. Running, 4. Non-Runnable (Blocked) and last 5.
Terminated.
How to create a thread in java?
We can thread in java by two method
First, With Thread class extending
and second with implementing Runnable interface.
Java provides predefine API
to implement thread, a few of commonly used API as follows
- thread () Create a new thread
- Thread (Runnable target)
- Thread (String name)
- Thread (Runnable target, String name)
- Thread (ThreadGroup g, Runnable target)
To
create a thread we used two way first by extending Thread class and second by implementing
Runnable interface and this done by overriding the run() method. After then
start() is called.
By implementing Runnable interface
public
class RunnableInteface implements Runnable
{
public void run()
{
System.out.println("thread is
running.. by Runnable interface");
}
public static void main(String args[])
{
RunnableInteface ob=new
RunnableInteface();
Thread t1 =new Thread(ob);
t1.start();
}
}
Output
thread
is running.. by Runnable interface
In the above program, we implement a Runnable interface. And override the run()
method and then create an object of the class and that object passed in the
Thread class constructor as Thread(ob).
By extending class Thread
public
class ExtendingThread extends Thread
{
public void run()
{
System.out.println("thread is
running..by extending class Thread.");
}
public static void main(String args[])
{
ExtendingThread t1=new
ExtendingThread();
t1.start();
}
}
Output
thread
is running..by extending class Thread.
public class DemoThread
extends Thread
{
public void run()
{
for(int i=0; i<5; i++)
{
System.out.println(getName() +
"i=" + i);
}
}
DemoThread() // constructor
{
start();
}
public static void main(String args[])
{
DemoThread ob1 = new DemoThread();
DemoThread ob2 = new DemoThread();
}
}
In the above program
run() method override and invoked by its constructor method, a loop is run from
0 to 4 and print the thread name with the help of getThread() method. In main()
method two objects are created. It will print the following output
Thread-1 i=0
Thread-1 i=1
Thread-1 i=2
Thread-1 i=3
Thread-1 i=4
Thread-0 i=0
Thread-0 i=1
Thread-0 i=2
Thread-0 i=3
Thread-0 i=4
Difference between Daemon and User Threads
The main difference between Daemon and User threads is, a Daemon thread is low
priority thread and User thread is a high priority thread.
Daemon
thread is used in the background supporting task and User thread is used in the foreground.
Daemon
thread life depends on the User thread whereas User Thread are independent.
Creating of Daemon Thread
public
class Daemon extends Thread {
@Override
public void run()
{
System.out.println("User Thread or
Non-Daemon Thread");
}
}
class
DaemonThread
{
public static void main(String[] args)
{
Daemon daemon = new Daemon();
daemon.start();
System.out.println("Main
Thread");
System.out.println("Is " +
daemon.getName() + " a Daemon Thread: " + daemon.isDaemon());
System.out.println("Is " +
Thread.currentThread().getName() + " a Daemon Thread: "
+ Thread.currentThread().isDaemon());
}
}
Inter-Thread communication
When a process communicates with synchronized threads is known as
inter-thread communication. It is used to check thread pooling in java. This can
be achieved by notify(), notifyAll() and wait() method.
Few points about the thread
- A thread is the smallest unit of execution
- Multiple tasks execution is multitasking from the user perspective and from multithreading from an operating system perspective
- Thread is lightweight
- Thread is a part of the process and it is an instance of a program
- Java thread can be created either by class Thread or by interface Runnable
- in a thread a run() method override
- The start() method automatically invoke the run() method
- The yield method of thread provides a chance to wait for another thread
- join() method is used to wait till the thread finishes its execution
- sleep() method is used to delay in execution
- Suspending a thread can be resume again later on by resuming it
- stop() method stop the thread and make it dead, it can not be resume again
- synchronized can be used to synchronize the thread
More Java programs
Java Program for BinarySearch
Java program for Selection Sort
Loops in Java - Java loop explained with examples
Java for loop
Array in Java
Program to find largest number among three numbers
Java program to display the big number where the user enters three numbers
import java.util.*;
public class BigNumberPrint
{
public static void main(String[] args)
{
int num1; // First Number
int num2; // Second Number
int
num3; // Third
Number
int big; // Store the largest number.
// Scanner Object sc.
Scanner sc = new Scanner(System.in);
// Input the First number from the user.
System.out.print("Enter first
number: ");
num1 = sc.nextInt();
// Input the Second number from the user.
System.out.print("Enter second
number: ");
num2 = sc.nextInt();
// Input the Third number from the user.
System.out.print("Enter third
number: ");
num3 = sc.nextInt();
// Find the largest number among three
number.
if (num1 > num2 && num1 >
num3)
{
big = num1;
}
else if (num2 > num3)
{
big = num2;
}
else
{
big = num3;
}
// Display a big number.
System.out.println("Big number:
" + big);
}
}
Output
Enter first number: 5
Enter second number: 8
Enter third number: 2
Largest number: 8
Program to find the largest number among three numbers
import
java.util.*;
public class
LargeNumberPrint
{
public static void main(String[] args)
{
int num1; // Input First Number
int num2; // Input Second Number
int num3; // Input Third Number
int large; // Store the largest number.
// Scanner Object sc.
Scanner sc = new Scanner(System.in);
// Input the First number from the user.
System.out.print("Enter first
number: ");
num1 = sc.nextInt();
// Input Second number from the user.
System.out.print("Enter second
number: ");
num2 = sc.nextInt();
// Input Third number from the user.
System.out.print("Enter third
number: ");
num3 = sc.nextInt();
// Find the largest number among three
numbers.
if(num1 >= num2)
{
if(num1 >= num3)
large = num1;
else
large = num3;
}
else
{
if(num2 >= num3)
large = num2;
else
large = num3;
}
// Print the large number.
System.out.println("Largest
number: " + large);
}
}
Output
Enter first number: 6
Enter second number: 9
Enter third number: 2
Largest number: 9
More Java Program
Celsius to Fahrenheit In Java – Fahrenheit To Celsius In Java