Question

In: Computer Science

can you fix this problem plz: I want to return -1 if the element is not...

can you fix this problem plz:

I want to return -1 if the element is not found. fix for me just that.

thank you.

public class Search{
  
  
  
   public static void main(String[] args) {
      
      
      
      
      
       Character [] anArray = {'a','b','c','d'};
      
       Character st = 'a';
      
      
       iterativeSearch(anArray, st);
      
   //System.out.println(iterativeSearch(anArray, st));
   }

   public static <T> boolean iterativeSearch(T[] anArray, T number) {
      
      
       boolean found = false;
      
       int index = 0;
      
      
       while (!found && (index < anArray.length)) {
          
           if(number.equals(anArray[index])) {
              
               found = true;
              
               System.out.println("Element found at index: " + index);
           }
          
          
          
           index++;
          
          
          
       }
      
   return found;
      
      
      
   }

}

Solutions

Expert Solution

HIGHLIGHTED lines are modified

public class Search{
public static void main(String[] args) {
Character [] anArray = {'a','b','c','d'};
System.out.println(iterativeSearch(anArray, 'd'));
System.out.println(iterativeSearch(anArray, 'e'));
}

public static <T> int iterativeSearch(T[] anArray, T number) {
boolean found = false;
int index = 0;
  
while (!found && (index < anArray.length)) {
if(number.equals(anArray[index])) {
found = true;
System.out.println("Element found at index: " + index);
break;
}
index++;
}

if(found == false)
return -1;
return index;
}

}

// Hit the thumbs up if you are fine with the answer. Happy Learning!


Related Solutions

I need to make 2 possible suggestions on how Comcast can fix the problem with their...
I need to make 2 possible suggestions on how Comcast can fix the problem with their customer service, and how Comcast can possibly improve the whole business overall while briefly discussing the impacts of these suggestions. Please put in a paragraph with the heading above, I Must come up with 350 words in total.
I already have the code of this program, I just want to know how to fix...
I already have the code of this program, I just want to know how to fix the code to Implement the 5th function (System.nanotime() and System.currentTimeMillis() methods) What Code does: Introduction Searching is a fundamental operation of computer applications and can be performed using either the inefficient linear search algorithm with a time complexity of O (n) or by using the more efficient binary search algorithm with a time complexity of O (log n). Task Requirements In this lab, you...
I need to fix this code, and could you please tell me what was the problem...
I need to fix this code, and could you please tell me what was the problem options 1 and 9 don't work #include <stdio.h> #include <time.h> #include <stdlib.h> // generate a random integer between lower and upper values int GenerateRandomInt(int lower, int upper){     int num =(rand()% (upper - lower+1))+lower;     return num; } // use random numbers to set the values of the matrix void InitializeMatrix(int row, int column, int dimension, int mat[][dimension]){     for(int i =0; i<row; i++){...
For this first Pause-Problem, I want you to design three (brief!) studies (they can all be...
For this first Pause-Problem, I want you to design three (brief!) studies (they can all be variations on the same idea). Make sure to note the independent and dependent variables for all 1). One should use an independent two group design 2). One should use a matched OR natural pair design 3). One should use a repeated measures design
For this Pause-Problem, I want you to design three (brief!) studies (they can all be variations...
For this Pause-Problem, I want you to design three (brief!) studies (they can all be variations on the same idea). ***Make sure to note the independent and dependent variables for all 1). One should use an independent three group design – 2). One should use a matched OR natural set design – 3). One should use a repeated measures design
this is my linkedlist and to insert a new element in it , however i want...
this is my linkedlist and to insert a new element in it , however i want it to be user entered linkedlist #include <bits/stdc++.h> using namespace std;    // A linked list Node struct Node {     int data;     struct Node* next; };    // Size of linked list int size = 0;    // function to create and return a Node Node* getNode(int data) {     // allocating space     Node* newNode = new Node();        // inserting the required data     newNode->data...
i want good answer with example plz i have a project useing java language but theacher...
i want good answer with example plz i have a project useing java language but theacher tell us u must use (java.util.Date) only to get the current year and month how can i do that plz jst using that class no celender or somthing else i need current year and current month to calculate the gurantee year and the guranttee month thax☺️
I want to draw a sphere and I want to rotate it, How can I do...
I want to draw a sphere and I want to rotate it, How can I do it in JOGL? ( I am working on a program to visualize the sun ) please provide a pic of the code.
Can you fix the code and comment the fix Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -39 at...
Can you fix the code and comment the fix Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -39 at CaesarCipher.encrypt(CaesarCipher.java:28) at CaesarCipher.main(CaesarCipher.java:52) public class CaesarCipher{     char[] encoder = new char[52];     char[] decoder = new char[52];      public CaesarCipher(int rotation)      {        for(int k=0 ; k < 26 ; k++)        {            encoder[k] = (char) ('A' + (k + rotation) % 26);            decoder[k] = (char) ('A' + (k - rotation + 26) % 26);        }        for(int j...
In Python: how can I fix the IsADirectoryError: [Errno 21] Is a directory: ??? this is...
In Python: how can I fix the IsADirectoryError: [Errno 21] Is a directory: ??? this is my code: #importing sqlite and pandas import sqlite3 import pandas as pd #goal print("Welcome! The goal of this assigment is to create a database to find meanings and synonyms for given phrases.") #Connecting database conn = sqlite3.connect("keilavaldez.db") #Creating cursor to remove existing specified tables cur = conn.cursor() #creating tables cur.execute("CREATE TABLE Synsets([SynsetID] INTEGER,[Definition] text)") cur.execute("CREATE TABLE Phrases([SynsetID] INTEGER,[phrase] text)") conn.commit() #opening and reading table...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT