Question

In: Computer Science

Create a static method with the appropriate inputs and outputs. Call each of them in the...

Create a static method with the appropriate inputs and outputs. Call each of them in the main method.

Write a method called removeAllInstances() which takes in a List and item4 . The method then proceeds to remove each item in the list that matches the given item. For example, if the method is passed the List [1, 4, 5, 6, 5, 5, 2] and the Integer 5, the method removes all 5’s from the List. The List then becomes [1, 4, 6, 2]. It should return nothing, since the changes the List it was provided. 5

Please do this in Java.

Solutions

Expert Solution

ANSWER:-

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
  
class GFG
{
   public static void removeAllInstances(List<Integer>l,int r)
   {
       Iterator itr = l.iterator(); // iterator use for list treverse
while (itr.hasNext())
{
int x = (Integer)itr.next();
if (x==r)
itr.remove();
}
   }
public static void main(String[] args)
{
   int num;
   Scanner sc=new Scanner(System.in); // scanner use for user input
   System.out.println("how many number you want to enter : ");
   int n=sc.nextInt();
List<Integer>l = new ArrayList<Integer>(); // create a list
System.out.println("Enter a numbers : ");
for(int i=0;i<n;i++)
{
num=sc.nextInt();  
l.add(num);
}
System.out.println("enter a number for remove : ");
int r=sc.nextInt();
System.out.println("Original list : "+l);
removeAllInstances(l,r);
System.out.println("Modified ArrayList : "+ l);
}
}

OUTPUT:-

// If any doubt please comment


Related Solutions

Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method called stringToListOfWords() which takes in a String converts it into a list of words. We assumes that each word in the input string is separated by whitespace.3 2Use the equals() method. 3The split() method of String can split an input up along a provided special string called a regular expression or regex. A regex is much like a code...
Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method called isPermutaion() which takes in two List objects which contain the same types. It returns true if the Lists are permutations of each other and false if not. Two lists are permutations if they contain the same elements, including the same number of duplicates, but they don’t have to contain the elements in the same order. For example, [1,2,...
Create a static method with the appropriate inputs and outputs. Call each of them in the...
Create a static method with the appropriate inputs and outputs. Call each of them in the main method. Write a method named allMultiples() which takes in a List of integers and an int. The method returns a new List of integers which contains all of the numbers from the input list which are multiples of the given int. For example, if the List is [1, 25, 2, 5, 30, 19, 57, 2, 25] and 5 was provided, the new list...
Explain the inputs (and outputs if you know them) of the following Excel functions: • Norm.S.Dist()....
Explain the inputs (and outputs if you know them) of the following Excel functions: • Norm.S.Dist(). • Norm.Inv(). • T.Dist().
Objectives • Use the static methods and static fields. •Use the method-call/return mechanism. • Use the...
Objectives • Use the static methods and static fields. •Use the method-call/return mechanism. • Use the random-number generation. • Overload methods. Problem Specification Write a program that lets a user play "Rock, Paper, Scissors" against the computer or computer against computer. In user-computer game, the program should ask the user to choose one of the three choices, and then the computer randomly picks one (without knowing what the user has chosen). For this problem, the user should be asked to...
Write a static method startsWith that inputs two Strings and returns a boolean. If the first...
Write a static method startsWith that inputs two Strings and returns a boolean. If the first input starts with the substring that is the second input, then the method returns true; otherwise, it returns false. For example, startsWith( "radar installation", "rad" ) returns true startsWith( "radar installation", "installation" ) returns false startsWith( "radar installation", "" ) returns true startsWith( "", "a" ) returns false startsWith( "", "" ) returns true
Write a static method endsWith that inputs two Strings and returns a boolean. If the first...
Write a static method endsWith that inputs two Strings and returns a boolean. If the first input ends with the substring that is the second input, then the method returns true; otherwise, it returns false. For example, endsWith( "radar installation", "rad" ) returns false endsWith( "radar installation", "installation" ) returns true endsWith( "radar installation", "" ) returns true endsWith( "", "a" ) returns false endsWith( "", "" ) returns true
Create a detailed process flowchart that includes process inputs, process outputs, activities, activity times, processing and...
Create a detailed process flowchart that includes process inputs, process outputs, activities, activity times, processing and labor requirements for a fictional retail chain of coffee shops. Capacity analysis of the process, including the strategies used to address expected customer demand. Suggested metrics used to measure the process performance. Identify the process problem and make suggestions on how the process can be improved
Draw a block diagram of an elevator controller. Clearly identify inputs and outputs of each block....
Draw a block diagram of an elevator controller. Clearly identify inputs and outputs of each block. Assume the elevator has 4 floors. Draw a state diagram for the elevator controller and briefly explain what the input, state variables, and ouput represent.
Consider the following inputs and outputs associated with a DCS system.
  Consider the following inputs and outputs associated with a DCS system. It is interesting to note that EXACTLY the same could be applicable to a SCADA system (although the SCADA system could use either a dedicated RTU or else a PLC). Break the following signals up (either going to, or away from the RTU-type devices) and indicate which are: i) digital inputs, ii) digital outputs, iii) analog inputs and iv) analog outputs: 18.1 ( 1 mark) START signal 18.2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT