AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



corejava-array: searching in an array

Take as input an array of integers and search element. Print the index of first occurrence of the element in the array. If it is not present, print -1.

Input:

1st line will contain the number of elements in the array (n) Next n lines will contain the elements of the array. Final line will contain the element to be searched.

Output:

The index of the element in the array. If not found, print -1.

Input:

6

12

61

50

25

65

12

12

Output:

0


corejava x 353
array x 12
Posted On : 2016-07-13 17:39:54.0
profile Boopalan - anyforum.in Boopalan
3730
up-rate
4
down-rate

Answers


Chek This code :

import java.util.ArrayList;
import java.util.Scanner;
public class ElementSearch {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<Object> list = new ArrayList<>();
int arraySize = scanner.nextInt();
int count = 0;
while (count < arraySize) {
list.add(scanner.nextInt());
//list.add(scanner.next());
//list.add(scanner.next());
count++;
}
System.out.println(list.indexOf(scanner.nextInt()));
}
}

Posted On : 2016-07-14 06:49:00
Satisfied : 3 Yes  0 No
profile SudheerKumar Javvadi - anyforum.in SudheerKumar Javvadi
0960
Reply This Thread
up-rate
4
down-rate
Comments
Question was asked to implement Array not ArrayList. But logically your code is right but not as per the requirement.
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1882  49150
Posted On :2016-07-14 13:13:06.0
Leave a Comment

import java.util.Arrays;
import java.util.Scanner;
public class ElementSearch {
public static void main(String[] args) {
try{
Scanner scanner = new Scanner(System.in);
int arraySize = scanner.nextInt();
Integer array[]=new Integer[arraySize];
int count = 0;
while (count < arraySize) {
array[count]=scanner.nextInt();
count++;
}
System.out.println(Arrays.binarySearch(array, scanner.nextInt()));
}catch(Exception e){
System.out.println(e);
}
}
}

Posted On : 2016-07-14 13:13:33
Satisfied : 1 Yes  1 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188249150
Reply This Thread
up-rate
5
down-rate
Comments
Your code not satisfied the condition actually if array element not found print -1 .But in your code it´s print -2 .Please check once your code.

Sample input i have taken (20 is not in array element) below .Check the same values from your end and let me know if your condition is satisfied or not:
6
12
61
50
25
65
12
20
-2
profile SudheerKumar Javvadi - anyforum.in SudheerKumar Javvadi
0  96  0
Posted On :2016-07-14 19:35:34.0
Leave a Comment
Yes, you are right. I just read about this method it returns (-(insertion point) - 1) if element not found, The insertion point is defined as the point at which the key would be inserted into the array and one more thing it should be sorted. so better yo use like this:

System.out.println(Arrays.asList(array).indexOf(scanner.nextInt()));
profile Rishi Kumar - anyforum.in Rishi Kumar
523  1882  49150
Posted On :2016-07-14 22:16:55.0
Leave a Comment
So, you have used the same concpet that i have used in the program right!!!.array convert to Arraylist and indexOf method for serach element.
profile SudheerKumar Javvadi - anyforum.in SudheerKumar Javvadi
0  96  0
Posted On :2016-07-14 23:51:01.0
Leave a Comment



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in