I wanna to remove list element from my array..but I am not getting the solution ..
this is my code..
for (int k = 0; k < name.Length; k++)
{
for (int i = 0; i < list_emp_info.Count; i++)
{
DateTime resigndate = Convert.ToDateTime(list_emp_info[i].ResignDate);
if (resigndate <= DateTime.Now)
{
name[k] = name[k].Remove(list_emp_info[i]);
}
}
}
You can do this with Linq
list_emp_info = list_emp_info.Where(l=> Convert.ToDateTime(l.ResignDate) > DateTime.Now).ToArray();
Related
I want to remove a specific object from an array, put it in a smaller array without getting out of range. This is what I've tried but it won't work.
Skateboard[] newSkateboard = new Skateboard[_skateboards.Length - 1];
for (int i = 0; i < _skateboards.Length; i++)
{
if (skateboard.Code != _skateboards[i].Code)
{
newSkateboard[i] = _skateboards[i];
}
}
Sure.
var j = 0;
for (int i = 0; i < _skateboards.Length; i++)
{
if (skateboard.Code != _skateboards[i].Code)
{
newSkateboard[j] = _skateboards[i];
j = j + 1;
}
}
its only executing the code inside of the if(i==0) and ignoring the one inside if(i==1) there is also if(i==2) but i removed due to post restrictions im new to this but if i can figuere out why its not executing the code inside of the first else if statement i might be able to fix fix both
string[][] friendFamily = new string[][]
{
new string[]{"khzix","rengar","shaco" },
new string[]{"jhin","tf","karma" },
new string[]{"qiyanna","braum","thresh" }
};
for (int i = 0; i < friendFamily.Length; i++)
{
if (i == 0)
{
for (int x = 0; x < friendFamily[i].Length; x++)
{
for (int j = 0; j < friendFamily.Length; j++)
{
if (j == 1 || j == 2)
{
for (int k = 0; k < friendFamily.Length; k++)
{
Console.WriteLine("hey {0} this is {1}.", friendFamily[i][x], friendFamily[j][k]);
}
}
}
}
}
else if (i == 1)
{
for (int x = 0; x < friendFamily[i].Length; x++)
{
for (int j = 0; j < friendFamily.Length; j++)
{
if (j == 0 || j == 2)
{
for (int k = 0; k < friendFamily.Length; k++)
{
Console.WriteLine("hey {0} this is {1}.", friendFamily[i][x], friendFamily[j][k]);
}
}
}
}
}
I could see that your code works fine, all conditions are met. Actually, I felt pain reading your code. Try to understand what I've done here, it is just what you need:
using System;
using System.Linq;
string[][] friendFamilies = new string[][]
{
new string[] { "khzix","rengar","shaco" },
new string[] { "jhin","tf","karma" },
new string[] { "qiyanna","braum","thresh" }
};
var allFriends = friendFamilies.SelectMany(x => x);
foreach (var friendToGreet in allFriends)
{
foreach (var friendWhoGreet in allFriends.Where(x => x != friendToGreet))
{
Console.WriteLine($"Hey {friendToGreet} this is {friendWhoGreet}.");
}
}
I have a 2 dimensional list that includes student numbers where each row represents a different lecture. I want to find out if a student takes more than one lecture and if so turn it into an adjacency matrix.
List look like this:
18011011 18011024 18011055
18011022 18011024 18011034
18011011 18011023 18011045 18011100
And matrix should be like this:
0 1 1
1 0 0
1 0 0
Code:
for (int i = 0; i < Yollar.Count; i++)
{
for (int j = 0; j < Yollar[i].Count; j++)
{
for (int k = i + 1; k < Yollar.Count; k++)
{
for (int p = 0; p < Yollar[k].Count; p++)
{
if (Yollar[i][j] == Yollar[k][p])
{
AdjancencyMatrix[i, p] = 1;
}
}
}
}
}
for(int i = 0; i < Yollar.Count; i++)
{
for(int j = 0; j < Yollar[i].Count; j++)
{
for (int k =i +1; k < Yollar.Count; k++)
{
for(int p = 0; p < Yollar[k].Count; p++)
{
if (Yollar[i][j] == Yollar[k][p])
{
AdjancencyMatrix[i, k] = 1;
AdjancencyMatrix[k, i] = 1;
}
}
}
}
}
I figured out. I only need lecture indexes so if you use only row indexes and changed the AdjacencyMatrix indexes and problem solved
If somebody could please help solve the following issue:
System.ArgumentOutOfRangeException : Index was out of range. Must be
non-negative and less than the size of the collection. Paarameter name
: index
The code:
for (int i = 0; i <= dataGridView2.Rows.Count ; i++)
{
for (int j = 0; j <= dataGridView3.Rows.Count; j++)
{
if (!string.IsNullOrEmpty(dataGridView2.Rows[i].Cells["supplier_name"].Value as string) && !string.IsNullOrEmpty(dataGridView3.Rows[j].Cells["brands_supplier"].Value as string))
{
if (dataGridView2.Rows[i].Cells["supplier_name"].Value.ToString() == dataGridView3.Rows[j].Cells["brands_supplier"].Value.ToString())
{
dataGridView2.Rows[i].Cells["brands_name"].Value += dataGridView3.Rows[j].Cells["brands_nume"].Value + " ";
}
}
else
{
break;
}
}
}
You try to access an element of your datagrid which doesn't exist.
You have to set your for condition to
for (int i = 0; i < dataGridView2.Rows.Count ; i++)
and
for (int j = 0; j < dataGridView3.Rows.Count; j++)
don't use <= because the index of dataGridView.Rows[] is 0 based.
For example, if your datagrid contains 3 elements you can reach them with:
var row1 = dataGrid.Rows[0]
var row2 = dataGrid.Rows[1]
var row3 = dataGrid.Rows[2]
And you try to access
var row4 = dataGrid.Rows[3] // Error because this item doesn't exist (System.ArgumentOutOfRangeException)
also, but this item doesn't exist
I think problem in cycle
for (int i = 0; i <= dataGridView2.Rows.Count ; i++)
You run one more time
use
for (int i = 0; i < dataGridView2.Rows.Count ; i++)
I am trying to get positions in a list of some values to compare them with another list.
for (int i = 0; i <= commands.ToArray().Length; i++)
{
levensheteinvalues_commands.Add(commands.ToArray()[i].ToString());
levensheteinvalues_numbers.Add(
Program.ComputeLevenshteinDistance(args[0],
commands.ToArray()[i].ToString()));
}
for (int i = 0; i <= commands.ToArray().Length; i++)
{
if (smallestlevensheteinvalue == 0)
{
smallestlevensheteinvalue = levensheteinvalues_numbers[i];
}
else if (smallestlevensheteinvalue > levensheteinvalues_numbers[i])
{
smallestlevensheteinvalue = levensheteinvalues_numbers[i];
}
}
var indexes = levensheteinvalues_numbers.GetIndexes(smallestlevensheteinvalue);
Why doesn't
var indexes = levensheteinvalues_numbers.GetIndexes(smallestlevensheteinvalue);
work? And when I get the value how can I compare it to another list?
The code you have posted have some serious problems. This May solve your problem since your code as well as your approach is very unclear and ambiguous. I have blindly edited the code to fix the serious problems.
for (int i = 0; i < commands.Count(); i++) {
levensheteinvalues_commands.Add(commands.ElementAt(i).ToString());
Program.ComputeLevenshteinDistance(args[0], commands.ElementAt(i).ToString()));
}
for (int i = 0; i < commands.Count(); i++) {
if (smallestlevensheteinvalue == 0)
{
smallestlevensheteinvalue = levensheteinvalues_numbers[i];
}
else if (smallestlevensheteinvalue > levensheteinvalues_numbers[i])
{
smallestlevensheteinvalue = levensheteinvalues_numbers[i];
}
}
int index = levensheteinvalues_numbers.IndexOf(levensheteinvalues_numbers.Min());