• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to search the String array  RSS feed

 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all

Please look at the code below:



How do I search the String array for a particular element (say "baz")? Please suggest.

Regards
Mansukhdeep
 
Vijaypal Singh
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edit as a reminder to (a) UseCodeTags, and (b) don't provide direct answers, rather direct original posters in the right direction by giving hints, links to documentation, etc. JavaRanch is NotACodeMill, and people learn more effectively when they have to figure things out for themselves.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijaypal Singh wrote:import java.util.*;

public class AlgaeDiesel {
public static void main(String [] args){
String [] sa = {"foo","bar","baz"};
List<String> thisList = Arrays.asList(sa);
String searchParam = "bar";
//Arrays.sort(sa);
for(String s : sa){
System.out.println(s);
}
search(thisList,searchParam);
}


public static void search(final List<String> searchTarget,final String searchParam){
System.out.println("thisList------"+searchTarget);
if(searchTarget.contains(searchParam)){
int index = searchTarget.indexOf(searchParam);
System.out.println("Found at ------ "+index);
}else{
System.out.println("Search Param Not found ");
}
}
}


Thanks Bhaaaji!
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:



Have a closer look at the java.util.Arrays class. It offers various binarySearch() methods. Note that you will have to sort the array before you do a binarySearch (you do this already with Arrays.sort).

Using binarySearch() will almost certainly be more efficient than turning the array into a list and then use the list methods contains() and indexOf().
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D. Ogranos wrote:
Mansukhdeep Thind wrote:



Have a closer look at the java.util.Arrays class. It offers various binarySearch() methods. Note that you will have to sort the array before you do a binarySearch (you do this already with Arrays.sort).

Using binarySearch() will almost certainly be more efficient than turning the array into a list and then use the list methods contains() and indexOf().


Hi Mr Ogranos
I had a look into the various versions of binarySearch() methods in the Arrays class. But there is none present that searches an element of a String type array.
 
D. Ogranos
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mansukhdeep Thind wrote:Hi Mr Ogranos
I had a look into the various versions of binarySearch() methods in the Arrays class. But there is none present that searches an element of a String type array.


There's one which accepts an Object[] remember that Strings are Objects in Java.
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijaypal Singh, have you really been here for zix years and don't know about the Code button? And what sort of class name is AlgaeDiesel
If Mansukhdeep Thind requires this exercise for beginner's homework, those solutions will earn him a 0 because you are not searching the array itself. So please note what it says on Beginning Java:
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D. Ogranos wrote:
Mansukhdeep Thind wrote:Hi Mr Ogranos
I had a look into the various versions of binarySearch() methods in the Arrays class. But there is none present that searches an element of a String type array.


There's one which accepts an Object[] remember that Strings are Objects in Java.


Thanks sir. Got the solution. Myself! with a littl help of course :P

Regards
Mansukhdeep
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!