Question

static int find(int x, List<Integer> a) Given an integer x which is assumed to be in the list a, write a method that returns

Given an integer x which is assumed to be in the list a, write a method that returns the position of the first occurrence of x in a. Positions are counted as 0,1,2,.... If x does not appear in the list, you should throw an IllegalArgumentException.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

code screenshot :

import java.util.*; public class Solution{ static int find(int x,List<Integer> a) { int pos = a.indexOf(x)in indexof method r

output :

10CEBOOR. CROPI P9 C COLO CONJUU JULULLON. JUVU java.lang. IllegalArgumentException: x does not exist in a

code to copy :

import java.util.*;
public class Solution{
   static int find(int x,List<Integer> a){
       int pos = a.indexOf(x); // indexOf method return position of x in a, if it does not exist in a then returns -1
       if( pos != -1 ){ // checking for position

           return pos;

       }else{

       throw new IllegalArgumentException("x does not exist in a "); // throwing exception with a message

       }
   }
  
   public static void main(String[] args) {
       List<Integer> a=new ArrayList<Integer>(); //
      
       a.add(7);
       a.add(5);
       a.add(3);
       a.add(8);
       try{
          
           System.out.println(find(3,a));

       }
       catch (Exception e) {

           System.out.println(e);

       }


       try{
          
           System.out.println(find(2,a));

       }
       catch (Exception e) {

           System.out.println(e);

       }
      
      
   }
}

** please upvote if it was helpful

Add a comment
Know the answer?
Add Answer to:
Given an integer x which is assumed to be in the list a, write a method...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a method that given a non-empty ArrayList of Integers parameterand also an Integer parameter, the...

    Write a method that given a non-empty ArrayList of Integers parameterand also an Integer parameter, the method returns a new ArrayList containing the elements from the original ArrayList that come after the first occurrence of Integer parameter in the original ArrayList. If the original ArrayList does not contain an occurrence of the Integer parameter return an empty ArrayList . The original ArrayList must be unaffected by the method execution. Following are some examples of the invocation of the method using...

  • In java please Write an efficient method that takes a string txt and an integer M...

    In java please Write an efficient method that takes a string txt and an integer M as arguments and returns the position of the first occurrence of M consecutive blanks in the string. If there is no such occurrence it should return the length of txt (txt.length). Give a runtime analysis of the method. 4.

  • In Java Write a method factorial that accepts an integer parameter n and that uses recursion...

    In Java Write a method factorial that accepts an integer parameter n and that uses recursion to compute and return the value of n factorial (also known as n!). Your method should throw an IllegalArgumentException if n is negative. Several calls and their return values are shown below. Call Output factorial(0); 1 factorial(1); 1 factorial(3); 6 factorial(5); 120 factorial(10); 3628800 factorial(-4); IllegalArgumentException

  • Given an array and a starting position write a function replaceFromN, that takes an integer array...

    Given an array and a starting position write a function replaceFromN, that takes an integer array 'array', the size of the array size and a starting positions 'n' as parameters and replaces the elements starting from that index onward with the sequence 1,2,3,... The function returns nothing. void replaceFromN(int array[], int size, int n) For example, given array= {15,12,4,9,2,3} n =2 the function should modify array to be {15,12,1,2,3,4}

  • Write a method MaxMagnitude with two integer input parameters that returns the largest magnitude value.

    6.12 LAB: Max magnitudeWrite a method MaxMagnitude with two integer input parameters that returns the largest magnitude value. Use the method in a program that takes two integer inputs, and outputs the largest magnitude value.Ex: If the inputs are 5 7 , the method returns:7Ex: If the inputs are -8 -2, the method returns:-8Note: The method does not just return the largest value, which for -8-2 would be -2. Though not necessary, you may use the absolute-value built-in math method.Your...

  • Write a method called reverseFirstK that accepts an integer k and a queue of integers as...

    Write a method called reverseFirstK that accepts an integer k and a queue of integers as parameters and reverses the order of the first k elements of the queue, leaving the other elements in the same relative order. For example, if a queue named q stores [10, 20 30, 40, 50, 60, 70, 80, 90], the call of reverseFirstK (4, q):should change the queue to store [40, 30 20, 10, 50, 60, 70, 80, 90]. If k is 0 or...

  • Write a recursive method contains(int target, LLNode<Integer> list) that returns true if list contains target and...

    Write a recursive method contains(int target, LLNode<Integer> list) that returns true if list contains target and false otherwise. For our example list contains(15, values) would return true while contains(10, values) would return false. (JAVA language)

  • Write a method max() which take a generic singly linked list as an argument and returns...

    Write a method max() which take a generic singly linked list as an argument and returns the maximum element reference in the list.      If the list is empty, please throw an empty collection exception.      The method prototype is defined as follows. Please write down your code in the method body enclosed in braces: public static <T extends Comparable<T>> T max(SingleLinkedList<T> list) throws EmptyCollectionException { } 2) What is the big O notation of the max method? a) O(N)...

  • Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements...

    Java help: Please help complete the locate method that is in bold.. public class LinkedDoubleEndedList implements DoubleEndedList { private Node front; // first node in list private Node rear; // last node in list private int size; // number of elements in list ////////////////////////////////////////////////// // YOU MUST IMPLEMENT THE LOCATE METHOD BELOW // ////////////////////////////////////////////////// /** * Returns the position of the node containing the given value, where * the front node is at position zero and the rear node is...

  • Please write in Java Recall that the ADT list class methods are;  void List() ...

    Please write in Java Recall that the ADT list class methods are;  void List()  bool isEmpty()  int size()  void add(int item, int pos)//inserts item at specified position (first postion is 1)  void remove(int index)//removes item from specified position  void removeAll()  int indexOf(int item)//returns the index of item  int itemAt(int index)//returns the item in position specified by index Implementation of LIST ADT operations and details are hidden. Only the methods listed above are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT