本文整理汇总了C#中System.Collections.ArrayList.BinarySearch方法的典型用法代码示例。如果您正苦于以下问题:C# ArrayList.BinarySearch方法的具体用法?C# ArrayList.BinarySearch怎么用?C# ArrayList.BinarySearch使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Collections.ArrayList
的用法示例。
在下文中一共展示了ArrayList.BinarySearch方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Form1
public Form1()
{
InitializeComponent();
ArrayList folderyMuzyczne, folderyNiemuzyczne, utworyGrane;
folderyMuzyczne = new ArrayList();
folderyNiemuzyczne = new ArrayList();
utworyGrane = new ArrayList();
StreamReader czytnik = new StreamReader("test.txt");
string linia = czytnik.ReadLine();
string[] dane = linia.Split(';');
Song piosenka = new Song(dane[0], dane[1], dane[2]);
int numer = utworyGrane.BinarySearch(piosenka);
if (numer < 0)
{
utworyGrane.Add(piosenka);
}
else
{
piosenka = (Song)utworyGrane[numer];
piosenka.nadania++;
utworyGrane[numer] = piosenka;
}
}
示例2: SaveButton_Click
private void SaveButton_Click(object sender, EventArgs e)
{
if (SubjectBox.Text.Length == 0)
{
MessageBox.Show("Cannot proceed with empty name");
return;
}
ArrayList list = new ArrayList((string[])Host.SubjectList.DataSource);
list.Sort();
int id = list.BinarySearch(SubjectBox.Text);
if (id >= 0)
{
MessageBox.Show("Subject with that name already exists!");
return;
}
id = ~id;
list.Insert(id, SubjectBox.Text);
string[] n = new string[list.Count];
list.CopyTo(n);
Host.SubjectBox2.AutoCompleteCustomSource.Add(SubjectBox.Text);
Host.SubjectList.DataSource = n;
Host.RenewSubjectList();
Host.SubjectList.SelectedItem = SubjectBox.Text;
this.Close();
}
示例3: findElement
// in the middle
private string findElement()
{
string winner;
ArrayList al = new ArrayList();
LinkedList<int> ll = new LinkedList<int>();
al = insertInHeadAL(al);
ll = insertInHeadLL(ll);
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
al.BinarySearch(al.Count / 2);
stopWatch.Stop();
TimeSpan ts = stopWatch.Elapsed;
Console.WriteLine("\n\n=================================================================================");
Console.WriteLine("Time to find element in the middle of ArrayList with {0} elements: {1}", al.Count, ts);
stopWatch.Reset();
stopWatch.Start();
ll.Find(ll.Count / 2);
stopWatch.Stop();
TimeSpan ts2 = stopWatch.Elapsed;
Console.WriteLine("Time to find element in the middle of LinkedList with {0} elements: {1}", ll.Count, ts2);
if (ts > ts2)
{
Console.WriteLine("\tLinkedList is quicker!");
return winner = "LinkedList";
}
else
{
Console.WriteLine("\tArrayList is quicker!");
return winner = "ArrayList";
}
}
示例4: ListAncestBoxesWithID
// ----------------------------------------
/// <summary>
/// Finding all ancestors (all boxes box) with given box type in Ferda Archive tree structure
/// </summary>
/// <param name="Box">Box in archive, for which function searches the ancestors</param>
/// <param name="ID">type of searched boxes (string identifier)</param>
/// <returns></returns>
public static IBoxModule[] ListAncestBoxesWithID(IBoxModule Box, string ID)
{
ArrayList MyBoxes = new ArrayList();
IBoxModule[] Ancestors = Box.ConnectionsFrom().ToArray();
foreach (IBoxModule b in Ancestors)
{
if (b.MadeInCreator.Identifier == ID) // this ancestor has desired type
MyBoxes.Add(b);
else // ancestor doesn't have desired type. Further we searche among it's ancestors (recurse)
{
IBoxModule[] b_ancestors = ListAncestBoxesWithID(b, ID); // recurse
foreach (IBoxModule bb in b_ancestors)
MyBoxes.Add(bb);
}
}
// eliminating the duplicites
MyBoxes.Sort();
IBoxModule[] SortedBoxes = (IBoxModule[])MyBoxes.ToArray(typeof(IBoxModule));
ArrayList MyUniqueBoxes = new ArrayList();
foreach (IBoxModule bbb in SortedBoxes)
{
if (MyUniqueBoxes.BinarySearch(bbb) < 0)
MyUniqueBoxes.Add(bbb);
}
IBoxModule[] resultArray = (IBoxModule[])MyUniqueBoxes.ToArray(typeof(IBoxModule));
return resultArray;
}
示例5: arrayList
public void arrayList()
{
ArrayList a = new ArrayList() { "a" ,"b" };
foreach (var item in (IEnumerable)a)
Console.WriteLine(item.ToString());
Console.WriteLine(a.BinarySearch("b"));
}
示例6: Main
static void Main(string[] args)
{
Console.WriteLine("Basic Array List Testing:");
ArrayList al = new ArrayList();
al.Add("Hello");
al.Add("World");
al.Add(5);
al.Add(new FileStream("deleteme", FileMode.Create));
Console.WriteLine("The array has " + al.Count + " items");
foreach (object o in al)
{
Console.WriteLine(o.ToString());
}
Console.WriteLine("\nRemove, Insert and Sort Testing:");
al = new ArrayList();
al.Add("Hello");
al.Add("World");
al.Add("this");
al.Add("is");
al.Add("a");
al.Add("test");
Console.WriteLine("\nBefore:");
foreach (object s in al)
Console.WriteLine(s.ToString());
al.Remove("test");
al.Insert(4, "not");
al.Sort();
Console.WriteLine("\nAfter:");
foreach (object s in al)
Console.WriteLine(s.ToString());
al.Sort(new reverseSorter());
Console.WriteLine("\nReversed:");
foreach (object s in al)
Console.WriteLine(s.ToString());
al.Reverse();
Console.WriteLine("\nReversed again, different method:");
foreach (object s in al)
Console.WriteLine(s.ToString());
Console.WriteLine("\nBinary Search Example:");
al = new ArrayList();
al.AddRange(new string[] { "Hello", "World", "this", "is", "a", "test" });
foreach (object s in al)
Console.Write(s + " ");
Console.WriteLine("\n\"this\" is at index: " + al.BinarySearch("this"));
}
示例7: TestStandardArrayList
public void TestStandardArrayList()
{
//
// Construct array list.
//
ArrayList list = new ArrayList();
// Add items to the lists.
for (int ii = 0; ii < strHeroes.Length; ++ii)
list.Add(strHeroes[ii]);
// Verify items added to list.
Assert.Equal(strHeroes.Length, list.Count);
//
// [] Use BinarySearch to find selected items.
//
// Search and verify selected items.
for (int ii = 0; ii < strFindHeroes.Length; ++ii)
{
// Locate item.
int ndx = list.BinarySearch(strFindHeroes[ii]);
Assert.True(ndx >= 0);
// Verify item.
Assert.Equal(0, strHeroes[ndx].CompareTo(strFindHeroes[ii]));
}
// Return Value;
// The zero-based index of the value in the sorted ArrayList, if value is found; otherwise, a negative number,
// which is the bitwise complement of the index of the next element.
list = new ArrayList();
for (int i = 0; i < 100; i++)
list.Add(i);
list.Sort();
Assert.Equal(100, ~list.BinarySearch(150));
//[]null - should return -1
Assert.Equal(-1, list.BinarySearch(null));
//[]we can add null as a value and then search it!!!
list.Add(null);
list.Sort();
Assert.Equal(0, list.BinarySearch(null));
//[]duplicate values, always return the first one
list = new ArrayList();
for (int i = 0; i < 100; i++)
list.Add(5);
list.Sort();
//remember this is BinarySeearch
Assert.Equal(49, list.BinarySearch(5));
}
示例8: Main
static void Main(string[] args)
{
ArrayList al = new ArrayList();
al.Add("Paris");
al.Add("Ottowa");
al.AddRange(new string[] { "Rome", "Tokyo", "Tunis", "Canberra" });
Console.WriteLine("Count: {0}", al.Count);
Console.WriteLine("Capacity: {0}", al.Capacity);
Console.WriteLine("Print values using foreach");
PrintValues(al);
Console.WriteLine("Print values using IEnumerator");
PrintValuesByEnumerator(al);
// Get second item in list as string
string str = (string)al[1]; // need to cast, would also unbox if stored type was value type
Console.WriteLine("al[1] = {0}", str);
// Get first three items
ArrayList firstThree = al.GetRange(0, 3);
PrintValues(firstThree);
// Remove next two
al.RemoveRange(3, 2);
PrintValues(al);
// Get, insert and remove
object itemOne = al[1];
al.Insert(1, "Moscow");
al.RemoveAt(2);
PrintValues(al);
// Sort
al.Sort();
PrintValues(al);
// Search
int targetIndex = al.BinarySearch("Moscow");
Console.WriteLine("Index of Moscow: {0}", targetIndex);
// Trim capacity
al.TrimToSize();
Console.WriteLine("Count: {0}", al.Count);
Console.WriteLine("Capacity: {0}", al.Capacity);
// Creates a new ArrayList with five elements and initialize each element with a value.
ArrayList al2 = ArrayList.Repeat("Boston", 5); // static method
PrintValues(al2);
}
示例9: SaveButton_Click
private void SaveButton_Click(object sender, EventArgs e)
{
if ((string)CurrentSubject == SubjectBox.Text)
{
this.Close();
return;
}
//string[] list = (string[])Host.SubjectList.DataSource;
ArrayList list = new ArrayList((string[])Host.SubjectList.DataSource);
int id = list.BinarySearch(CurrentSubject);
list.RemoveAt(id);
id = list.BinarySearch(SubjectBox.Text);
if (id >= 0)
{
MessageBox.Show("Subject with that name already exists!");
return;
}
id = ~id;
list.Insert(id, SubjectBox.Text);
string[] n = new string[list.Count];
list.CopyTo(n);
Host.SubjectList.DataSource = n;
//change all subjects with that name
foreach (DataRow row in Host.MainDataTable.Rows)
{
if (row["Subject"].ToString() == (string)CurrentSubject)
{
row["Subject"] = SubjectBox.Text;
}
}
Host.SubjectBox2.AutoCompleteCustomSource.Remove((string)CurrentSubject);
Host.SubjectBox2.AutoCompleteCustomSource.Add(SubjectBox.Text);
Host.RenewSubjectList();
Host.SubjectList.SelectedItem = SubjectBox.Text;
this.Close();
}
示例10: Main
static void Main(string[] args)
{
ArrayList l = new ArrayList();
l.Add("Gateau");
l.Insert(0, "patate");
//l.RemoveAt(0);
Console.WriteLine(l.Contains("Gateau"));
foreach (object o in l)
Console.WriteLine(o);
l.BinarySearch("Gateau");
}
示例11: ArgumentNullException
void IIdentifierCreationService.ValidateIdentifier(Activity activity, string identifier)
{
if (identifier == null)
throw new ArgumentNullException("identifier");
if (activity == null)
throw new ArgumentNullException("activity");
if (activity.Name.ToLower(CultureInfo.InvariantCulture).Equals(identifier.ToLower(CultureInfo.InvariantCulture)))
return;
ArrayList identifiers = new ArrayList();
Activity rootActivity = GetRootActivity(activity);
identifiers.AddRange(GetIdentifiersInCompositeActivity(rootActivity as CompositeActivity));
identifiers.Sort();
if (identifiers.BinarySearch(identifier.ToLower(CultureInfo.InvariantCulture), StringComparer.OrdinalIgnoreCase) >= 0)
throw new ArgumentException(string.Format("Duplicate Component Identifier {0}", identifier));
}
示例12: Intersect
public TopicInfoArray Intersect(ArrayList topicInfoArrayToIntersect)
{
TopicInfoArray answer = new TopicInfoArray();
this.Array.Sort(null);
topicInfoArrayToIntersect.Sort(null);
foreach(TopicVersionInfo topicInfo in this.Array)
{
if(topicInfoArrayToIntersect.BinarySearch(topicInfo) >= 0)
{
if( answer.Array.BinarySearch(topicInfo) < 0 )
{
answer.Add(topicInfo);
}
}
}
return answer;
}
示例13: Sample4
static void Sample4()
{
ArrayList myList = new ArrayList();
myList.Add(new MyObject() { ID = 4 });
myList.Add(new MyObject() { ID = 1 });
myList.Add(new MyObject() { ID = 5 });
myList.Add(new MyObject() { ID = 3 });
myList.Add(new MyObject() { ID = 2 });
myList.Sort();
int foundIndex = myList.BinarySearch(new MyObject() { ID = 4 });
if (foundIndex >= 0)
{
Debug.WriteLine(((MyObject)myList[foundIndex]).ID.ToString());
}
else
{
Debug.WriteLine("Element not found");
}
}
示例14: CheckCounterSignaturesReference
/// <summary>
/// Counter signatures should all contain a reference to the parent signature SignatureValue element
/// </summary>
/// <returns>If the function returns true the check was OK</returns>
public virtual bool CheckCounterSignaturesReference()
{
CounterSignatureCollection counterSignatureCollection;
XadesSignedXml counterSignature;
string referenceUri;
ArrayList parentSignatureValueChain;
bool referenceToParentSignatureFound;
bool retVal;
retVal = true;
parentSignatureValueChain = new ArrayList();
parentSignatureValueChain.Add("#" + this.signatureValueId);
counterSignatureCollection = this.XadesObject.QualifyingProperties.UnsignedProperties.UnsignedSignatureProperties.CounterSignatureCollection;
for (int counterSignatureCounter = 0; (retVal == true) && (counterSignatureCounter < counterSignatureCollection.Count); counterSignatureCounter++)
{
counterSignature = counterSignatureCollection[counterSignatureCounter];
referenceToParentSignatureFound = false;
for (int referenceCounter = 0; referenceToParentSignatureFound == false && (referenceCounter < counterSignature.SignedInfo.References.Count); referenceCounter++)
{
referenceUri = ((Reference)counterSignature.SignedInfo.References[referenceCounter]).Uri;
if (parentSignatureValueChain.BinarySearch(referenceUri) >= 0)
{
referenceToParentSignatureFound = true;
}
parentSignatureValueChain.Add("#" + counterSignature.SignatureValueId);
parentSignatureValueChain.Sort();
}
retVal = referenceToParentSignatureFound;
}
if (retVal == false)
{
throw new CryptographicException("CheckCounterSignaturesReference() failed on at least one counter signature");
}
retVal = true;
return retVal;
}
示例15: BinarySearch_Null
public void BinarySearch_Null ()
{
ArrayList al = new ArrayList ();
al.Add (this);
Assert.AreEqual (-1, al.BinarySearch (null), "null");
}