I’ve managed to search in the array, but how do i display the location of the value searched in the array for the code below?
import javax.swing.*;
import java.util.Arrays;
public class Listahan {
public static void main(String[] args){
int list1[] = {1,3,2,5,7,8,5,6,9,4};
int list2[] = {2,1,4,3,2,1,4,2,0,2};
int list3[] = new int[10];
int a;
System.out.print("List1: ");
for(a=0; a<10; a++){
System.out.print(list1[a]+" ");
}
System.out.print("\nList2: ");
for(a=0; a<10; a++){
System.out.print(list2[a]+" ");
}
System.out.print("\nList3: ");
for(a=0; a<10; a++){
list3[a]=list1[a]+list2[a];
System.out.print(list3[a]+" ");
}
int searchValue = Integer.parseInt(JOptionPane.showInputDialog("Input a value to search in List3: "));
int Result = Arrays.binarySearch(list3,searchValue);
if(Result!=-3){
System.out.println("\n\nThe value "+searchValue+" is in List3!");
System.out.println("There are "+Result+" of it in List3.");
}else{
System.out.println("\n"+searchValue+" cannot be found in List3.");
}
}
}
for example if i searched for 9, the output would be:
The value 9 is in List3!
The are 4 of it in List3.
Located at: list3[4], list3[5], list3[6], list3[8]
There are a few things going on in the code that needs to be pointed out.
Arrays.binarySearchmethod is a position, rather than occurrences.A binary search works only on sorted lists.
In the code above, the lists
list1,list2, andlist3are all unsorted lists. The binary search algorithm has a requirement that the list that it performs the search on must be sorted prior to applying the search algorithm.This requirement is noted in the Javadoc for the
Arrays.binarySearchmethod:[Emphasis added]
The return value of a the
Arrays.binarySearchmethod is a position, rather than occurrencesOnce again, there is a problem with your expectations for how the
binarySearchmethodworks.
The above line of code, the return value from the
binarySearchmethod, which is a position at which the specified element was found, is being used as if it is the number of occurrences of that element in the list.Furthermore, the
ifstatement that is using theReturnvariable is probably working due to programming by coincidence:According to the
Arrays.binarySearchjavadoc, that method will return a non-zero value if the specified element is found in the list. It returning a value that is not-3does not signify any specific return state — therefore, this code is probably just running as a consequence of the list containing values in a certain way, so the code will break as soon as eitherlist1orlist2is changed at all.You’ll want to take a look at the Javadocs of the
Arrays.binarySearchmethod to see what exactly that method will return for you.Additional note
As an additional note, if you wish to show the number of occurrences and positions at which the specified elements occurs, you might be better off just performing a
forloop on the list, and checking each element if they areequalsto each other.Here’s a bit of pseudocode:
Final note about the coding style
And, as a final note, the coding style could be improved a bit to make things easier to read and to conform to the Java coding standards.
Add some spaces around punctuations
If we were writing in English, we add spaces after the punctuation to make it easier to read. We should do the same when coding to make the code easier to read. Adding spaces after commas and operators should make things a bit easier on the eyes:
Secondly, the variable names should start with lower-case, so the
Returnvariable should be something likereturnValueor better yetposition, as theArrays.binarySearchmethod returns a position at which the element was found.For more information, please take a look at the Code Conventions for the Java Programming Language.