本文整理汇总了C#中ArrayList.LastIndexOf方法的典型用法代码示例。如果您正苦于以下问题:C# ArrayList.LastIndexOf方法的具体用法?C# ArrayList.LastIndexOf怎么用?C# ArrayList.LastIndexOf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ArrayList
的用法示例。
在下文中一共展示了ArrayList.LastIndexOf方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TestLastIndexOfBasic
public void TestLastIndexOfBasic()
{
//--------------------------------------------------------------------------
// Variable definitions.
//--------------------------------------------------------------------------
ArrayList arrList = null;
int ndx = -1;
//
// Construct array lists.
//
arrList = new ArrayList((ICollection)strHeroes);
//
// [] Obtain last index of "Batman" items.
//
ndx = arrList.LastIndexOf("Batman");
if (ndx != -1)
{
Assert.Equal(0, strHeroes[ndx].CompareTo((string)arrList[ndx]));
}
//
// [] Attempt to find null object.
//
// Remove range of items.
ndx = arrList.LastIndexOf(null);
Assert.Equal(-1, ndx);
// [] Call LastIndexOf on an empty list
var myList = new ArrayList();
var lastIndex = myList.LastIndexOf(6);
Assert.Equal(-1, lastIndex);
}
示例2: CompareObjects
//.........这里部分代码省略.........
{
bad.IndexOf(1, 0, -1);
}
catch(ArgumentException)
{
}
catch(Exception ex)
{
hsh1["IndexOf"] = ex;
}
try
{
bad.IndexOf(1, 0, bad.Count);
}
catch(ArgumentException)
{
}
catch(Exception ex)
{
hsh1["IndexOf"] = ex;
}
try
{
bad.IndexOf(1, bad.Count-1, bad.Count-2);
}
catch(ArgumentException)
{
}
catch(Exception ex)
{
hsh1["IndexOf"] = ex;
}
if(fVerbose)
Console.WriteLine("LastIndexOf(), " + good.Count + " " + bad.Count);
for(int i=0; i<good.Count; i++)
{
if(good.LastIndexOf(good[i]) != bad.LastIndexOf(good[i]))
{
hsh1["LastIndexOf"] = "(Object)";
}
if(good.LastIndexOf(i+1000) != bad.LastIndexOf(i+1000))
{
hsh1["LastIndexOf"] = "(Object)";
}
}
try
{
bad.LastIndexOf(null);
}
catch(ArgumentException)
{
}
catch(Exception ex)
{
hsh1["LastIndexOf"] = ex;
}
if(fVerbose)
Console.WriteLine("LastIndexOf(Object, Int32)");
for(int i=0; i<good.Count; i++)
{
if(good.LastIndexOf(good[i], good.Count-1) != bad.LastIndexOf(good[i], good.Count-1))
{
hsh1["LastIndexOf"] = "(Object, Int32)";
}
if(good.LastIndexOf(good[i], 0) != bad.LastIndexOf(good[i], 0))
{
示例3: CompareObjects
//.........这里部分代码省略.........
}
catch (Exception ex)
{
hsh1["IndexOf"] = ex;
}
try
{
bad.IndexOf(1, 0, bad.Count);
}
catch (ArgumentException)
{
}
catch (Exception ex)
{
hsh1["IndexOf"] = ex;
}
try
{
bad.IndexOf(1, bad.Count - 1, bad.Count - 2);
}
catch (ArgumentException)
{
}
catch (Exception ex)
{
hsh1["IndexOf"] = ex;
}
//LastIndexOf(Object)
for (int i = 0; i < good.Count; i++)
{
if (good.LastIndexOf(good[i]) != bad.LastIndexOf(good[i]))
{
hsh1["LastIndexOf"] = "(Object)";
}
if (good.LastIndexOf(i + 1000) != bad.LastIndexOf(i + 1000))
{
hsh1["LastIndexOf"] = "(Object)";
}
}
try
{
bad.LastIndexOf(null);
}
catch (ArgumentException)
{
}
catch (Exception ex)
{
hsh1["LastIndexOf"] = ex;
}
//LastIndexOf(Object, int)
for (int i = 0; i < good.Count; i++)
{
if (good.LastIndexOf(good[i], good.Count - 1) != bad.LastIndexOf(good[i], good.Count - 1))
{
hsh1["LastIndexOf"] = "(Object, int)";
}
if (good.LastIndexOf(good[i], 0) != bad.LastIndexOf(good[i], 0))
{
hsh1["LastIndexOf"] = "(Object, int)";
}
示例4: runTest
public virtual bool runTest()
{
int iCountErrors = 0;
int iCountTestcases = 0;
Console.Error.WriteLine( strName + ": " + strTest + " runTest started..." );
ArrayList arrList = null;
int ndx = -1;
String [] strHeroes =
{
"Aquaman",
"Atom",
"Batman",
"Black Canary",
"Captain America",
"Captain Atom",
"Batman",
"Catwoman",
"Cyborg",
"Flash",
"Green Arrow",
"Batman",
"Green Lantern",
"Hawkman",
"Huntress",
"Ironman",
"Nightwing",
"Batman",
"Robin",
"SpiderMan",
"Steel",
"Superman",
"Thor",
"Batman",
"Wildcat",
"Wonder Woman",
"Batman",
};
do
{
++iCountTestcases;
Console.Error.WriteLine( "[] Construct ArrayList" );
try
{
arrList = new ArrayList( (ICollection) strHeroes );
if ( arrList == null )
{
Console.WriteLine( strTest+ "E_101: Failed to construct new ArrayList" );
++iCountErrors;
break;
}
}
catch (Exception ex)
{
Console.WriteLine( strTest+ "E_10001: Unexpected Exception: " + ex.ToString() );
++iCountErrors;
break;
}
++iCountTestcases;
Console.Error.WriteLine( "[] Obtain last index of \"Batman\" items" );
try
{
if( ( ndx = arrList.LastIndexOf( "Batman") ) != -1 )
{
if ( strHeroes[ndx].CompareTo( (String)arrList[ndx] ) != 0 )
{
String strInfo = strTest + " error: ";
strInfo = strInfo + "On LastIndexOf==" + ndx + " ";
strInfo = strInfo + "Expected hero <"+ strHeroes[ndx] + "> ";
strInfo = strInfo + "Returned hero <"+ (String)arrList[ndx] + "> ";
Console.WriteLine( strTest+ "E_202: " + strInfo );
++iCountErrors;
break;
}
}
}
catch (Exception ex)
{
Console.WriteLine( strTest+ "E_10002: Unexpected Exception: " + ex.ToString() );
++iCountErrors;
break;
}
++iCountTestcases;
Console.Error.WriteLine( "[] Attempt to find null object" );
try
{
ndx = arrList.LastIndexOf( null);
if ( ndx != -1 )
{
String strInfo = strTest + " error: ";
strInfo = strInfo + "Expected index <-1> ";
strInfo = strInfo + "Returned index <"+ ndx + "> ";
Console.WriteLine( strTest+ "E_303: " + strInfo );
++iCountErrors;
break;
}
}
catch (Exception ex)
{
Console.WriteLine( strTest+ "E_10003: Unexpected Exception: " + ex.ToString() );
++iCountErrors;
//.........这里部分代码省略.........
示例5: TestInvalidIndex
public void TestInvalidIndex()
{
//--------------------------------------------------------------------------
// Variable definitions.
//--------------------------------------------------------------------------
ArrayList arrList = null;
int ndx = -1;
//
// Construct array lists.
//
arrList = new ArrayList((ICollection)strHeroes);
//
// [] Obtain last index of "Batman" items.
//
ndx = arrList.Count;
while ((ndx = arrList.LastIndexOf("Batman", --ndx)) != -1)
{
Assert.Equal(0, strHeroes[ndx].CompareTo((string)arrList[ndx]));
}
//
// [] Attempt to find null object.
//
// Remove range of items.
ndx = arrList.LastIndexOf(null, arrList.Count - 1);
Assert.Equal(-1, ndx);
//
// [] Attempt invalid LastIndexOf using negative endindex
//
Assert.Throws<ArgumentOutOfRangeException>(() => arrList.LastIndexOf("Batman", -1));
//
// [] Attempt invalid LastIndexOf using negative startindex
//
Assert.Throws<ArgumentOutOfRangeException>(() => arrList.LastIndexOf("Batman", -1000));
}
示例6: TestArrayListWrappers
public void TestArrayListWrappers()
{
//--------------------------------------------------------------------------
// Variable definitions.
//--------------------------------------------------------------------------
ArrayList arrList = null;
int ndx = -1;
string[] strHeroes =
{
"Aquaman",
"Atom",
"Batman",
"Black Canary",
"Captain America",
"Captain Atom",
"Batman",
"Catwoman",
"Cyborg",
"Flash",
"Green Arrow",
"Batman",
"Green Lantern",
"Hawkman",
"Huntress",
"Ironman",
"Nightwing",
"Batman",
"Robin",
"SpiderMan",
"Steel",
"Superman",
"Thor",
"Batman",
"Wildcat",
"Wonder Woman",
"Batman",
null
};
//
// Construct array lists.
//
arrList = new ArrayList((ICollection)strHeroes);
//Adapter, GetRange, Synchronized, ReadOnly returns a slightly different version of
//BinarySearch, Following variable cotains each one of these types of array lists
ArrayList[] arrayListTypes = {
(ArrayList)arrList.Clone(),
(ArrayList)ArrayList.Adapter(arrList).Clone(),
(ArrayList)ArrayList.FixedSize(arrList).Clone(),
(ArrayList)arrList.GetRange(0, arrList.Count).Clone(),
(ArrayList)ArrayList.ReadOnly(arrList).Clone(),
(ArrayList)ArrayList.Synchronized(arrList).Clone()};
foreach (ArrayList arrayListType in arrayListTypes)
{
arrList = arrayListType;
//
// [] Obtain last index of "Batman" items.
//
int startIndex = arrList.Count - 1;
int tmpNdx = 0;
while (0 < startIndex && (ndx = arrList.LastIndexOf("Batman", startIndex, startIndex + 1)) != -1)
{
Assert.True(ndx <= startIndex);
Assert.Equal(0, strHeroes[ndx].CompareTo((string)arrList[ndx]));
tmpNdx = arrList.LastIndexOf("Batman", startIndex, startIndex - ndx + 1);
Assert.Equal(ndx, tmpNdx);
tmpNdx = arrList.LastIndexOf("Batman", startIndex, startIndex - ndx);
Assert.Equal(-1, tmpNdx);
startIndex = ndx - 1;
}
//
// [] Attempt to find null object.
//
// Remove range of items.
ndx = arrList.LastIndexOf(null, arrList.Count - 1, arrList.Count);
Assert.NotEqual(-1, ndx);
Assert.Null(arrList[ndx]);
//
// [] Attempt invalid LastIndexOf using negative endindex
//
Assert.Throws<ArgumentOutOfRangeException>(() => arrList.LastIndexOf("Batman", arrList.Count - 1, -1000));
//
// [] Attempt invalid LastIndexOf using negative startindex
//
Assert.Throws<ArgumentOutOfRangeException>(() => arrList.LastIndexOf("Batman", -1000, 0));
//
// [] Attempt invalid LastIndexOf using endIndex greater than the size.
//.........这里部分代码省略.........