• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compare string to a String in array

 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I create a String array and have x elements in it and then be able to compare a string to check if it matches any of the elements inside the array?

I don't want to write a function to iterate through the array.

Thanks
Imad
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like an excellent case for using Arrays.binarySearch(Object[],Object).

Do be sure to note the requirement that the array be sorted.

Any specific reason you need to use an array instead of a Collection?
[ May 19, 2008: Message edited by: Stevi Deter ]
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply. This is good to know. I don't want to add too many lines of code thats why I am not using collection. here is my code. I would appreciate if you could tell me how can I make it better. this code is based on your suggestion of using binarySearch method of Arrays class.

String acct_Rte_Cdes [] = {"X", "Y", "Z", "A", "B", "C", "D", "E",
"J", "I", "K", "H", "G", "F", "L"} ;

in my compare method i do the following

private boolean myCompareMethod (String acctRteCde){

boolean rtValue = false ;
Arrays.sort (acct_Rte_Cdes, 0, 14);

if ( Arrays.binarySearch (acct_Rte_Cdes, acctRteCde)){
rtValue = true ;
}

return rtValue ;
}

Please let me know if there is a better way.

Thanks
Imad
 
Timothy Frey
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem here is you're going out of your way to sort the array, THEN using a binary search on it. The binary search would be helpful if your array was already sorted but this is going to be slower than just iterating over the array in the first place.

Plus, now this function has a side effect of sorting the array, potentially introducing a subtle bug somewhere else in the program. Some sorting algorithms (quicksort?) may actually have worse performance when trying to sort an already-sorted array.

Anyway, long story short, just iterate over the array .
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without having a broader idea of what you're trying to accomplish, it's hard to say how to make it better, although I'm unclear as to the use of your compare method.

I will note that the code won't compile because Arrays.binarySearch() returns an int, not a boolean.

If you just want to check for existence, as opposed to needing to know where in the array the item is, you might find you can write far less code, and far more understandable code, using a List:



List also lets you get the index where the string is, without having to sort:

 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks to both of you. That really helped. I could easily do something like this and it would work

private int my compareMethod (String acctRteCde){

int rtValue = - 1;
if ( acctRteCde.equals ("X") || acctRteCde.equals("Y")......){
rtValue = 1 ;
}

else if (acctRteCde.equals("Z") || acctRteCde.equals("A") .....){
rtValue = 2 ;
}

return rtValue ;
}

I have 15 elements in two array so rather than having 15 "||" I want to try something different.

Here is my new code

String acct_Rte_Cdes_A_Array [] = { "X", "Y", "Z", "A", "B", "C", "F", "E" };
String acct_Rte_Cdes_B_Array [] = {"J", "K", "D", "G", "Q", "L", "M"};

List acct_Rte_Cdes_A = Arrays.asList(acct_Rte_Cdes_A_Array) ;
List acct_Rte_Cdes_B = Arrays.asList(acct_Rte_Cdes_B_Array) ;

private int compareRateCodes (Account acct){

int rtValue = -1 ;

String acctRteCde = ((AccountHistory)acct.getHistories().get(0)).getAccountRatingCode();

if ( acct_Rte_Cdes_A.contains(acctRteCde) ){

rtValue = 1 ;
}
else if ( acct_Rte_Cdes_B.contains(acctRteCde) ){

rtValue = 2 ;
}

return rtValue ;
}

What do you think.
Thanks again
Imad
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think your proposed solution is far clearer and easier to maintain than a huge if/else or switch statement.

The only thing I'd suggest is to come up with more descriptive names. What differentiates the two code groups?

I'm a strong proponent of descriptive variables names. Makes maintenance of the code so much easier.
 
Muhammad Imad Qureshi
Ranch Hand
Posts: 238
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stevi

This is a one time batch program. Only analyst knows what differentiates two groups of rate codes. And I don't really care since there won't be any maintenance.

But thank you so much for your help. Most of the time I do routine maintenance job so today after a long time I learned something new in Java. Thanks

Imad
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13045
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally I would create a java.util.HashSet from the array of String - then you can test with a simple contains( String s ) method call.

Bill
 
Binesh Kochi
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. It works!!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic