Question

In: Computer Science

Write a Java Program that can:​ Remove a particular element from an array.​ Add a new...

Write a Java Program that can:​

  • Remove a particular element from an array.​
  • Add a new element to an array.​
  • Change an element with the new one.​
  • Search for a particular element in the array.​

​The code must have four separate methods for each task stated above.​

Do not use any pre-defined Java functions.​
You are free to use int or String data-type for the array.​

Solutions

Expert Solution

import java.io.*;
import java.lang.*;
import java.util.*;

public class Allmethods {
   public static int[] addX(int n, int arr[], int x)
   {
       int i;
       int newarr[] = new int[n + 1];
       for (i = 0; i < n; i++)
           newarr[i] = arr[i];

       newarr[n] = x;

       return newarr;
   }
   public static int[] delX(int n,int a[],int x)
   {
   int flag =1,loc=0;
for (int i = 0; i < n; i++)
{
if(a[i] == x)
{
flag =1;
loc = i;
break;
}
else
{
flag = 0;
}
}
if(flag == 1)
{
for(int i = loc+1; i < n; i++)
{
a[i-1] = a[i];
}
return a;
}
return a;
}
public static int search(int arr[],int n,int toSearch)
{
int found = 0,i;
  
for(i=0; i<n; i++)
{

if(arr[i] == toSearch)
{
found = 1;
break;
}
}
System.out.println("\n\nSearch Method for Above Array is:\n");
if(found == 1)
{
System.out.println(toSearch + " is found "+"at position "+(i + 1));
}
else
{
System.out.println(toSearch + " is not found ");
}
return 0;
}
public static int[] replace(int arr[],int n,int elem,int replace)
{
for(int i=0;i<n;i++)
{
if(arr[i]==replace)
{
arr[i] = elem;
}
}
return arr;
}
   public static void main(String[] args)
   {

       int n = 10;
       int i;
       int arr[]
           = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

       System.out.println("Initial Array:\n");
       for(i=0;i<n;i++)
       {
       System.out.print(arr[i]+" ");
       }
       int x = 50;
       arr = addX(n, arr, x);
       System.out.println("\n");
       System.out.println("Array with " + x + " added:\n");
       for(i=0;i<n+1;i++)
       {
       System.out.print(arr[i]+" ");
       }
      
       int elem = 10;
       int xarr[] = new int[arr.length-1];
       xarr = delX(arr.length,arr,elem);
       System.out.println("\n\nArray after deleting element "+elem + " is\n");
       for(i=0;i<xarr.length-1;i++)
       {
       System.out.print(xarr[i]+" ");
       }
      
       int parr[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,50 };
       int toSearch = 50;
       System.out.println("\n\nElements in an array:\n");
       for(i=0;i<parr.length;i++)
       {
       System.out.print(parr[i]+" ");
       }
       search(parr,parr.length,toSearch);
      
      
       int arr1[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
       int replace = 3;
       int elem1 = 45;
       System.out.println("\n\nElements in an array:\n");
       for(i=0;i<arr1.length;i++)
       {
       System.out.print(arr1[i]+" ");
       }
       arr1 = replace(arr1,arr1.length,elem1,replace);
       System.out.println("\n\nArray after replacing element "+replace +" with element "+elem1+" is\n");
       for(i=0;i<arr1.length;i++)
       {
       System.out.print(arr1[i]+" ");
       }
   }
}


Related Solutions

Write a program to remove an element from an array at the given position k and...
Write a program to remove an element from an array at the given position k and push the rest of the array elements one position back. Then insert the removed element at the beginning. Position k is entered through keyboard. For example, if the original array x is {'r', 'c', 'm', '7', 'w', '3', 'q'} and k = 3, the array will be changed to {'7', 'r', 'c', 'm', 'w', '3', 'q'}. Hint: Sequence of moving the element is important....
Write a Java program that creates a three-dimensional array. Populate each element with a string that...
Write a Java program that creates a three-dimensional array. Populate each element with a string that states each coordinate position in the array.
Q1. Write a Java program to do sequential search to find element 55 in array 10,20,35,45,55,65,75,85....
Q1. Write a Java program to do sequential search to find element 55 in array 10,20,35,45,55,65,75,85.                                                                                                                                                                    Q2.Write a Java program to find element 54 in array 45,41,65,53,76,90 using Binary Search. (Hint: Binary search is applied to sorted array elements) Q4.   Write a java program to create array list subject        - add English, Maths, Science to the list        - add Computers at index 2        - display first occurrence index of Maths        - traverse the list using...
Write the methods that insert and remove an element at the kth position in Java using...
Write the methods that insert and remove an element at the kth position in Java using recursion (NOT iteration) (Hint for the remove method: we have to recursive position -1 times, and each time we do the recursion, we have to create a method to move the head to the right) public void insertRecursive(int position, int data) { //enter code here } public int removeAtRecursive(int position) { //enter code here } Here is the given class for the Node: public...
Write a program in MIPS to find the largest element of an array, the array size...
Write a program in MIPS to find the largest element of an array, the array size should be less than or equal to 10. Has to be extremely basic, cannot use stuff like move. Very basic. Here is what I already have and I am stuck. .data myarray: .word 0,0,0,0,0,0,0,0,0,0 invalid: .asciiz "Number is invalid, store a number in the array that is from 0-10.\n" large: .asciiz "The largest element is " colon: .asciiz " :\t" enter: .asciiz "Store a...
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
Write a Java program to initialize an array with the even integers from 2 to 20...
Write a Java program to initialize an array with the even integers from 2 to 20 and print the result then pass this array to a method in order to create a new array which is the inverse of the array you passed (print the result). You cannot use a third array. Insert comments and version control in the program to document the program.
Write a Java program that takes an array of 10 "Int" values from the user and...
Write a Java program that takes an array of 10 "Int" values from the user and determines if all the values are distinct or not. Return TRUE if all the values of the array are distinct and FALSE if otherwise.
This Array implementation allows duplicates. Add a method that searches the array and remove all the...
This Array implementation allows duplicates. Add a method that searches the array and remove all the values in the array that does not have a duplicate. void removeNoDups( ) ( 12 points) For example if array had elements 100 200 100 100 200 400 500 300, once this new method is run it should return 100 200 100 100 200 removing 400, 500 and 300 which do not have duplicate values in the array. So in short this method allows...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT