Logo 
Search:

Java Articles

Submit Article
Home » Articles » Java » Networking TechnologyRSS Feeds

Do the following for the Array Insert the element in the array,delete an element from the array,search for a particular element Create multiple thread

Posted By: Mena Schmidt     Category: Java     Views: 6964

Write a program which creates a one dimensional array. Do the following for the Array.
1. Insert the element in the array
2. delete an element from the array
3. search for a particular element
Create multiple threads which do the above things. Implement the program so that consistency is maintained for the array.

Code for Do the following for the Array Insert the element in the array,delete an element from the array,search for a particular element Create multiple thread in Java

import java.io.*;

class ArrayImpl
{
    int arr[] = newint[15];

    ArrayImpl()
    {
        
        for(int i=0;i<10;i++)
        {
            arr[i]=i+1;
        }
    }
    synchronized void insert(int ele,int pos)
    {
        System.out.println("Insert element 15 at position 2");
        pos = pos - 1;
        for(int i=11;i>pos;i--)
            arr[i] = arr[i-1];

        arr[pos] = ele;
    }
    synchronized void delet(int pos)
    {
        System.out.println("Delete element at position 5");
        pos = pos - 1;
        for(int i=pos ; i< 10 ;i++)
            arr[i] = arr[i+1];

    }
    synchronized void Search(int pos)
    {
        System.out.println("search element at position 6");
        pos = pos - 1;
        int v=arr[pos];
        System.out.println("position " + (pos+1) + " value is ::" + v);
    }
    synchronized void display(int len)
    {
        for(int i=0;i<len;i++)
        {
            System.out.print(arr[i] + "  ");
        }
        System.out.println();
    }
}


class ThreadInsert implements Runnable 
{
    Thread t;
    ArrayImpl a;
    int e,p;
    ThreadInsert(ArrayImpl aa,int ele,int pos)
    {
        a=aa;
        e=ele;
        p=pos;

        t = new Thread(this);
        t.start();
        
    }
    publicvoid run()
    {
        try
        {
            a.insert(e,p);
            a.display(11);
        }catch (Exception e)
        {
            System.out.print("InterruptedException");
        }
    }
}
class ThreadDelete implements Runnable 
{
    Thread t;
    ArrayImpl a;
    int p;
    ThreadDelete(ArrayImpl aa,int pos)
    {
        a=aa;
        p=pos;

        t = new Thread(this);
        t.start();
        
    }
    publicvoid run()
    {
        try
        {
            a.delet(p);
            a.display(10);
        }catch (Exception e)
        {
            System.out.print("InterruptedException");
        }
    }
}
class ThreadSearch implements Runnable 
{
    Thread t;
    ArrayImpl a;
    int p;
    ThreadSearch(ArrayImpl aa,int pos)
    {
        a=aa;
        p=pos;

        t = new Thread(this);
        t.start();
        
    }
    publicvoid run()
    {
        try
        {
            a.Search(p);
        }catch (Exception e)
        {
            System.out.print("InterruptedException");
        }
    }
}
class Array
{
    publicstaticvoid main(String args[])
    {
        
        ArrayImpl a = new ArrayImpl();
        a.display(10);

        new ThreadInsert(a,15,2);
        new ThreadDelete(a,5);
        new ThreadSearch(a,6);

    }
}
/*
Output

1 2 3 4 5 6 7 8 9 10
Insert element 15 at position 2
1 15 2 3 4 5 6 7 8 9 10
Delete element at position 5
1 15 2 3 5 6 7 8 9 10
search element at position 6
position 6 value is ::6

*/
  
Share: 



Mena Schmidt
Mena Schmidt author of Do the following for the Array Insert the element in the array,delete an element from the array,search for a particular element Create multiple thread is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!