本文整理汇总了C#中List.LastIndexOf方法的典型用法代码示例。如果您正苦于以下问题:C# List.LastIndexOf方法的具体用法?C# List.LastIndexOf怎么用?C# List.LastIndexOf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类List
的用法示例。
在下文中一共展示了List.LastIndexOf方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
static void Main()
{
int number = 10;
Random rand = new Random();
List<int> myList = new List<int>();
List<int> resultList = new List<int>();
for (int i = 0; i < number; i++)
{
myList.Add(rand.Next(1, number));
}
foreach (var item in myList)
{
Console.WriteLine(item);
}
myList.Sort();
Console.WriteLine("Sorted List");
foreach (var item in myList)
{
Console.WriteLine(item);
}
int index = 0;
int curr = myList[index];
int counter = 0;
Console.WriteLine(myList.LastIndexOf(curr) + "{0}", 1 / 2);
while (index < myList.Count)
{
curr = myList[index];
counter = (myList.LastIndexOf(curr) - index) + 1;
if (((counter % 2) == 0) && (counter != 1))
{
Console.WriteLine("in");
for (int i = 1; i <= counter; i++)
{
resultList.Add(curr);
}
}
index = myList.LastIndexOf(curr) + 1;
}
foreach (var item in resultList)
{
Console.WriteLine(item);
}
}
示例2: ParseParentheses
private List<String> ParseParentheses(List<String> tokens)
{
int openingParenthesesIndex = tokens.LastIndexOf(Operators.OPEN_PARENTHESES);
while (openingParenthesesIndex >= 0)
{
int closingParenthesesIndex = tokens.IndexOf(Operators.CLOSE_PARENTHESES);
tokens = Unwind(tokens, openingParenthesesIndex, closingParenthesesIndex);
openingParenthesesIndex = tokens.LastIndexOf(Operators.OPEN_PARENTHESES);
}
return tokens;
}
示例3: Main
static void Main(string[] args)
{
//creating a list
var numbers = new List<int>() { 1, 2, 3, 4 };
numbers.Add(1);
numbers.AddRange(new int[3] { 5, 6, 7 });
foreach (var number in numbers)
Console.WriteLine(number);
Console.WriteLine();
Console.WriteLine("Index of 1: " + numbers.IndexOf(1));
Console.WriteLine("Last Index of 1: " + numbers.LastIndexOf(1));
Console.WriteLine("Count: " + numbers.Count);
for (int i = 0; i < numbers.Count; i++)
{
if (numbers[i] == 1)
numbers.Remove(numbers[i]);
}
foreach (var number in numbers)
Console.WriteLine(number);
numbers.Clear();
Console.WriteLine("Count: " + numbers.Count);
}
示例4: Filter
/// <summary>
/// Beginning at the end, of the set, return all elements until the element after the element which satifies the condition [stopAt]
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="orderedSet"></param>
/// <param name="stopAt"></param>
/// <returns></returns>
static IEnumerable<TestBuilder> Filter(List<TestBuilder> orderedSet, Func<TestBuilder, bool> stopAt)
{
var first = orderedSet.LastOrDefault(stopAt);
return first == null ?
orderedSet :
orderedSet.Skip(orderedSet.LastIndexOf(first));
}
示例5: Main
static void Main(string[] args)
{
List<String> nomes = new List<String>() { "epaminondas", "eva", "chico", "tião", "eva" };
List<String> outrosNomes = new List<String>() { "adão", "eva", "agnaldo" };
Console.WriteLine("quantidade de elementos: {0}", nomes.Count);
Console.WriteLine("posição da 1a. \"eva\" dentro da lista: {0}", nomes.IndexOf("eva"));
Console.WriteLine("posição da última \"eva\" dentro da lista: {0}", nomes.LastIndexOf("eva"));
Console.WriteLine();
nomes.Remove("chico");
nomes.RemoveAll(x => x.Contains('e'));
nomes.AddRange(outrosNomes);
foreach (var item in nomes)
{
Console.WriteLine(item);
}
nomes.Sort();
nomes.ForEach(new Action<string>(Imprimir));
Console.ReadKey();
}
示例6: PosTest2
public bool PosTest2()
{
bool retVal = true;
TestLibrary.TestFramework.BeginScenario("PosTest2: The generic type is type of string");
try
{
string[] strArray = { "apple", "banana", "dog", "chocolate", "dog", "food" };
List<string> listObject = new List<string>(strArray);
int result = listObject.LastIndexOf("dog");
if (result != 4)
{
TestLibrary.TestFramework.LogError("003", "The result is not the value as expected,result is: " + result);
retVal = false;
}
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("004", "Unexpected exception: " + e);
retVal = false;
}
return retVal;
}
示例7: SpanNumber
public static int SpanNumber(List<int> inputNumbers)
{
List<int> spanNumbers = new List<int>();
for (int i = 0; i < inputNumbers.Count; i++)
{
if (inputNumbers.LastIndexOf(inputNumbers[i]) >= 0)
{
int span = (inputNumbers.LastIndexOf(inputNumbers[i]) - i) + 1;
spanNumbers.Add(span);
}
}
spanNumbers.Sort((a, b) => -1 * a.CompareTo(b));
return spanNumbers[0];
}
示例8: PosTest1
public bool PosTest1()
{
bool retVal = true;
TestLibrary.TestFramework.BeginScenario("PosTest1: The generic type is int");
try
{
int[] iArray = new int[1000];
for (int i = 0; i < 1000; i++)
{
iArray[i] = i;
}
List<int> listObject = new List<int>(iArray);
int ob = this.GetInt32(0, 1000);
int result = listObject.LastIndexOf(ob);
if (result != ob)
{
TestLibrary.TestFramework.LogError("001", "The result is not the value as expected,result is: " + result);
retVal = false;
}
}
catch (Exception e)
{
TestLibrary.TestFramework.LogError("002", "Unexpected exception: " + e);
retVal = false;
}
return retVal;
}
示例9: Eval
public string Eval()
{
decimal result = 0;
int position;
string resultString = "";
foreach (string line in File.ReadLines("C:\\users\\skornish\\Desktop\\prefix.txt"))
{
List<string> list = new List<string>(line.Split(' '));
while (list.Contains("+") ||
list.Contains("-") ||
list.Contains("/") ||
list.Contains("*"))
{
position = 0;
foreach (string element in list)
{
if (element.Equals("+") ||
element.Equals("-") ||
element.Equals("/") ||
element.Equals("*"))
{
position = Math.Max(position, list.LastIndexOf(element));
}
}
if (list.ElementAt(position).Equals("+"))
{
result = decimal.Parse(list.ElementAt(position + 1)) +
decimal.Parse(list.ElementAt(position + 2));
}
else if (list.ElementAt(position).Equals("-"))
{
result = decimal.Parse(list.ElementAt(position + 1)) -
decimal.Parse(list.ElementAt(position + 2));
}
else if (list.ElementAt(position).Equals("/"))
{
result = decimal.Parse(list.ElementAt(position + 1)) /
decimal.Parse(list.ElementAt(position + 2));
}
else if (list.ElementAt(position).Equals("*"))
{
result = decimal.Parse(list.ElementAt(position + 1)) *
decimal.Parse(list.ElementAt(position + 2));
}
list.Insert(position, result.ToString());
list.RemoveAt(position + 3);
list.RemoveAt(position + 2);
list.RemoveAt(position + 1);
}
resultString = resultString + "," + result.ToString();
}
MessageBox.Show(resultString);
return resultString;
}
示例10: Main
static void Main(string[] args)
{
List<int> MyList = new List<int>();
string l = Console.ReadLine();
while (l != string.Empty)
{
MyList.Add(int.Parse(l));
l = Console.ReadLine();
}
MyList.Sort();
int index = 0;
int curr = 0;
bool isMajorant = false;
while (index < MyList.Count)
{
curr = MyList[index];
int counter = MyList.LastIndexOf(curr) - index + 1;
if (counter > (MyList.Count / 2))
{
Console.WriteLine("The majorant is: {0} -> {1} times", curr, counter);
isMajorant = true;
}
index = MyList.LastIndexOf(curr) + 1;
}
if (!isMajorant)
{
Console.WriteLine("The majorant does't exist");
}
}
示例11: Execute
public override CompileResult Execute(IEnumerable<FunctionArgument> arguments, ParsingContext context)
{
ValidateArguments(arguments, 2);
var number = ArgToDecimal(arguments, 0);
var refer = arguments.ElementAt(1);
bool asc = false;
if (arguments.Count() > 2)
{
asc = base.ArgToBool(arguments, 2);
}
var l = new List<double>();
foreach (var c in refer.ValueAsRangeInfo)
{
var v = Utils.ConvertUtil.GetValueDouble(c.Value, false, true);
if (!double.IsNaN(v))
{
l.Add(v);
}
}
l.Sort();
double ix;
if (asc)
{
ix = l.IndexOf(number)+1;
if(_isAvg)
{
int st = Convert.ToInt32(ix);
while (l.Count > st && l[st] == number) st++;
if (st > ix) ix = ix + ((st - ix) / 2D);
}
}
else
{
ix = l.LastIndexOf(number);
if (_isAvg)
{
int st = Convert.ToInt32(ix)-1;
while (0 <= st && l[st] == number) st--;
if (st+1 < ix) ix = ix - ((ix - st - 1) / 2D);
}
ix = l.Count - ix;
}
if (ix <= 0 || ix>l.Count)
{
return new CompileResult(ExcelErrorValue.Create(eErrorType.NA), DataType.ExcelError);
}
else
{
return CreateResult(ix, DataType.Decimal);
}
}
示例12: MaxSpan
/// <summary>
/// Finds the biggest span between a leftmost and rightmost appearance of some value.
/// </summary>
/// <param name="numbers">List input.</param>
/// <returns>Biggest span.</returns>
public static int MaxSpan(List<int> numbers)
{
List<int> spanCounters = new List<int>();
for (int i = 0; i < numbers.Count; i++)
{
int lastindexof = numbers.LastIndexOf(numbers[i]);
if (lastindexof != -1)
{
var spanCounter = (lastindexof - i) + 1;
spanCounters.Add(spanCounter);
}
}
spanCounters.Sort((a, b) => -1 * a.CompareTo(b));
return spanCounters[0];
}
示例13: addJump
public static List<int> addJump(List<int> path, int n)
{
var position = path.LastIndexOf((int)Way.step);
if (position < 0)
{
return null;
}
path[position] = (int)Way.jump;
if (position > 0)
{
path.RemoveAt(position - 1);
}
return path.Sum() == n ? path : null;
}
示例14: Reorder
static List<string> Reorder(string[] words)
{
List<string> listOfWords = new List<string>(words);
for (int i = 0; i < listOfWords.Count; i++)
{
string currentWord = listOfWords[i];
int position = (currentWord.Length % (words.Length + 1));
listOfWords.Insert(position, currentWord);
if (position > i)
{
listOfWords.RemoveAt(listOfWords.IndexOf(currentWord));
}
else
{
listOfWords.RemoveAt(listOfWords.LastIndexOf(currentWord));
}
}
return listOfWords;
}
示例15: Run
// this solution borrows heavily from solutions proposed by others
public string Run()
{
int max = 0;
int maxCycleLen = 0;
List<int> remainders;
for (int i = 983; i < 1000; i++)
{
int r = 10, count;
remainders = new List<int>();
for (count = 0; !remainders.Contains(r); count++)
{
remainders.Add(r);
r = 10 * (r % i);
}
int cycleLen = count - remainders.LastIndexOf(r);
if (cycleLen > maxCycleLen)
{
maxCycleLen = cycleLen;
max = i;
}
}
return max.ToString();
}