Vulpes
posted
5419 posts
since
Feb 28, 2011
from
|
|
Re: Convert array string into integer
|
|
|
|
|
|
|
|
|
|
It's not in fact the case that any of the parameters to Array.BinarySearch need to be an integer (at least for the overloads which are normally used) though the return value is always an integer which represents the index of the item if found. If the item is not found then the method returns a negative integer.
Also the array needs to be sorted first before this method can be used.
So I think you can do it like this - I've made the search case insensitive which would be normal when looking for book titles:
using System; using System.Collections; //needed to do case insensitive search
public class DebugSix04 { public static void Main() { String[] books = { "Rich Dad", "Poor Dad", "Catch-22", "Harry Potter", "Programming Using C#", "Wizard of Oz", "The Deep" }; IComparer comparer = new CaseInsensitiveComparer(); Array.Sort(books, comparer);
Console.Write("What book are you looking for? ");
string enterBookName = Console.ReadLine();
int x = Array.BinarySearch(books, enterBookName, comparer);
if (x < 0) Console.WriteLine("{0} not found", enterBookName); else Console.WriteLine("Yes, we carry {0}", books[x]); } }
|
|
|
|
|
|
Maha
posted
816 posts
since
Aug 13, 2006
from
|
|
Re: Convert array string into integer
|
|
|
|
|
|
|
|
|
|
|
Thank you for your help.
I wish to know whether both arguments inside the parenthesis of BinarySearch() method can be different types. That means for example: BinarySearch(int, string)
|
|
|
|
|
|
Vulpes
posted
5419 posts
since
Feb 28, 2011
from
|
|
Re: Convert array string into integer
|
|
|
|
|
|
|
|
|
|
Here's a list of the overloads of the Array.BinarySearch method:
http://msdn.microsoft.com/en-us/library/system.array.binarysearch.aspx
As you'll see, there isn't one which takes an int as its first parameter.
In fact all of them take the array itself as the first parameter. This is necessary because it's a static method of the Array class.
The first and second parameters are always of different types.
|
|
|
|
|
|
Maha
posted
816 posts
since
Aug 13, 2006
from
|
|
Re: Convert array string into integer
|
|
|
|
|
|
|
|
|
|
|
That means any type of array is considered only as an array (not the type it belongs to) inside the parenthesis of BinarySearch() method. I wish to know whether my understanding is correct.
|
|
|
|
|
|
Vulpes
posted
5419 posts
since
Feb 28, 2011
from
|
|
Re: Convert array string into integer
|
|
|
|
|
|
|
|
|
|
Well, if you study the overloads, there are two types:
1. Those which take an Array as the first parameter.
2. Those which take a T[] as the first parameter where T is a generic type parameter.
The first group are weakly typed and are not bothered at compile time what the type of the array is. Consequently, if you try to search for an element which is not compatible with the type of the array, you will get an exception.
The second group are strongly typed and the compiler infers the type of T (and hence the type of element to look for) from the type of the array that is being passed to it. The compiler will choose these overloads, rather than the others, when it can.
|
|
|
|
|
|
Maha
posted
816 posts
since
Aug 13, 2006
from
|
|
Re: Convert array string into integer
|
|
|
|
|
|
|
|
|
|
|
Thank you for your explanation.
Could you explain what is meant by "This is necessary because it's a static method of the Array class".
This program has been altered to take two different types in BinarySearch() method. Please use T[] and correct it.
using System; namespace BinarySearchDemo { class Program { static void Main(string[] args) { int[] zip = new int[5] { 1000, 2000, 3000, 4000, 5000 };
Console.Write("Entre a ZIP code "); string enter = Console.ReadLine();
int num = Array.BinarySearch(zip, enter); if (num < 0) Console.WriteLine("Zip is not found"); else Console.WriteLine("Array position is {0}", num);
Console.ReadKey(); } } }
|
|
|
|
|
|
Vulpes
posted
5419 posts
since
Feb 28, 2011
from
|
|
Re: Convert array string into integer
|
|
|
|
|
|
|
|
|
|
Well, a static method of the Array class has no instance associated with it. You therefore have to pass it one as an argument.
I've altered the code so that the compiler will now choose the Array.BinarySearch<T>(T[], T) overload where T is 'int'.
Notice that it's not necessary to specify the generic type parameter <int> here - it will still work fine if you omit it. This is because the compiler will infer the type to be int as zip is an int array and zipcode is a scalar int variable:
using System;
namespace BinarySearchDemo { class Program { static void Main(string[] args) { int[] zip = new int[5] { 1000, 2000, 3000, 4000, 5000 };
Console.Write("Enter a ZIP code "); string enter = Console.ReadLine(); int zipCode; int.TryParse(enter, out zipCode); // zip will be 0 if enter is invalid int num = Array.BinarySearch<int>(zip, zipCode); // <int> is optional here
if (num < 0) Console.WriteLine("Zip is not found"); else Console.WriteLine("Array position is {0}", num);
Console.ReadKey(); } } }
|
|
|
|
|
|
Maha
posted
816 posts
since
Aug 13, 2006
from
|
|
Re: Convert array string into integer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|