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());
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;
}
}
Here I have 2 datatables, I want to make some changes runtime, so I used 2 for loops. But when user hit is huge like 20k+, system get down. It took more than 2 minutes every hit, I want to optimize, can anyone help on this?
Datatable BuybackResponse = ResourceCenterDAL.GetBuyBack(2);;
Datatable dt = ResourceCenterDAL.GetBuyBack(4);;
if (BuybackResponse.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int k = 0; k < BuybackResponse.Count; k++)
{
var Exch = !string.IsNullOrEmpty(Convert.ToString(dt.Rows[i]["Nsetradingsymbol"])) ? "NSE" : "BSE";
//if (Exch == BuyBackExchangeMapping[Convert.ToInt32(BuybackResponse[k]["Exchange"])] &&
if (BuybackResponse[k]["ProductCode"].Trim().ToLower() == Convert.ToString(dt.Rows[i]["Nsetradingsymbol"]).Trim().ToLower() ||
BuybackResponse[k]["ProductCode"].Trim().ToLower() == Convert.ToString(dt.Rows[i]["Bsetradingsymbol"]).Trim().ToLower())
{
if (!Convert.ToString(dt.Rows[i]["isBuyBack"]).Trim().ToLower().Equals("y"))
dt.Rows[i]["isBuyBack"] = "Y";
dt.Rows[i]["productcode"] = BuybackResponse[k]["ProductCode"];
BBcount++;
}
else
{
if (string.IsNullOrEmpty(Convert.ToString(dt.Rows[i]["isBuyBack"])))
dt.Rows[i]["isBuyBack"] = "N";
}
}
if (BBcount.Equals(BuybackResponse.Count)) { break; }
}
}
So i'm trying to make a program that uses the League of Legends API in C#.
I found a NuGet package that makes using the API a lot easier.
Everything works fine so far except the code execution stops after the
first for loop i use.
Here's the code: (of course i took out the api key)
string[] summnames;
long[] champids;
long[] teamids;
long[] champs;
CreepScoreAPI.ParticipantLive[] enemy;
CreepScoreAPI.ParticipantLive[] ally;
CreepScoreAPI.ParticipantLive centsumm;
CreepScoreAPI.ParticipantLive[] champsss;
CreepScoreAPI.ChampionStatic[] champions;
CreepScoreAPI.Summoner[] sumners;
CreepScoreAPI.League[] leaguesz;
Dictionary<string, List<CreepScoreAPI.League>>[] leagues;
int[] champidsint;
string[] champnames;
int s;
int se;
public async Task<string> game(string summname)
{
string data;
CreepScoreAPI.CreepScore cs = new CreepScoreAPI.CreepScore("api key");
var summoner = await cs.RetrieveSummoner(CreepScoreAPI.CreepScore.Region.EUNE, summname);
long summid = summoner.id;
var thegame = await summoner.RetrieveCurrentGameInfo();
CreepScoreAPI.ParticipantLive[] participants = thegame.participants.ToArray();
for (int i = 0; i <= 9; i++) { summnames[i] = participants[i].summonerName;}
for (int i = 0; i <= 9; i++) { champids[i] = participants[i].championId;}
for (int i = 0; i <= 9; i++) { teamids[i] = participants[i].teamIdLong;}
for (int i = 0; i <= 9; i++) { champids[i] = participants[i].championId;}
for (int i = 0; i <= 9; i++) { champidsint[i] = Convert.ToInt32(champids[i]);}
for (int i = 0; i <= 9; i++) { champions[i] = await cs.RetrieveChampionData( CreepScoreAPI.CreepScore.Region.EUNE, champidsint[i], CreepScoreAPI.Constants.StaticDataConstants.ChampData.All, "en_US", "7.1.1",false ); }
for (int i = 0; i <= 9; i++) { champnames[i] = champions[i].name; }
for (int i = 0; i <= 9; i++) { sumners[i] = await cs.RetrieveSummoner(CreepScoreAPI.CreepScore.Region.EUNE, summnames[i]); }
for (int i = 0; i <= 9; i++) { leagues[i] = await sumners[i].RetrieveLeague(); }
/* teamsorter */
foreach (CreepScoreAPI.ParticipantLive p in participants)
{
if (p.summonerId == summid)
{
centsumm = p;
}
if (p.teamIdLong == centsumm.teamIdLong)
{
ally[s] = p;
s++;
}
if (p.teamIdLong != centsumm.teamIdLong)
{
enemy[se] = p;
se++;
}
}
data = " I'LL FORMAT A STRING THAT OUTPUTS ALL THE DATA I NEED HERE";
return data;
}
When I call the game function and input the name of the summoner i gets to the first for loop and doesn't even populate the summnames[] array and stops execution with no error code.
What I'm trying to do with all the loops is populate the variables I made before the function so I can use them later for other purposes.
I think you should assign all the arrays and set their length to at least 10.
This could solve the problem.
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();
I've got a booking service in which you can select by an enum if you want to display all seats, all available seats or all booked seats.
My problem is that I don't know how to make it so i.e. only the booked seats are shown, because as of now if I select "only booked seats" it shows the correct number of reserved seats, but it iterates from the beginning of the entire array instead of the ones that I want, so if there are 3 reserved seats, it will show seat 0,1,2 instead of the ones that are actually reserved.
I am pretty sure that I need to change the for (int i = 0; i < count; i++) to for (int i = 0; i < totalNumberOfSeats; i++) instead to actually loop through all seats instead of just as many as there are of the kind that I want to display, but then I get out of bound exception and I don't know how to proceed.
public string[] GetSeatInfoStrings(DisplayOptions choice)
{
int count = GetNumOfSeats(choice);
if (count <= 0)
{
return null;
}
string[] strSeatInfoStrings = new string[count];
for (int i = 0; i < count; i++)
{
strSeatInfoStrings[i] = GetSeatInfoAt(i);
}
return strSeatInfoStrings;
}
public string GetSeatInfoAt(int index)
{
int row = GetRow(index);
int col = GetCol(index);
string seatInfo;
if (string.IsNullOrEmpty(GetName(m_nameMatrix[row, col])))
{
seatInfo = MarkAsVacant(row, col);
}
else
{
seatInfo = MarkAsReserved(row, col);
}
return seatInfo;
}
I've got a method IsReserved(int index) and tried something like this
if (IsReserved(i))
{
// Want to return all seats that are reserved
}
else if (!IsReserved(i))
{
// Want to return all seats that are NOT reserved
}
As far as for that method working, it is okay, but the problem is that I don't know what to put within the brackets.
This is a question where we cant help without knowing your complete model. There is little detail. May be you want something like this:
string[] strSeatInfoStrings = new string[count];
int counter = 0;
for (int i = 0; i < totalNumberOfSeats; i++)
{
var seatInfo = GetSeatInfoAt(i);
if (seatInfo == "reserved") //some kind of checking
{
strSeatInfoStrings[counter] = seatInfo;
counter++; //run another counter
}
}
return strSeatInfoStrings;
You can avoid all the hassle of array and counter and just use a List<T>..
var strSeatInfoStrings = new List<string>();
for (int i = 0; i < totalNumberOfSeats; i++)
{
var seatInfo = GetSeatInfoAt(i);
if (seatInfo == "reserved") //some kind of checking
strSeatInfoStrings.Add(seatInfo);
}
return strSeatInfoStrings;
It's probably easier to use a List than an array in this case, because with a List you don't need to know the size before you start adding to it.
public string[] GetSeatInfoStrings(DisplayOptions choice)
{
List<string> lstSeatInfoStrings = new List<string>();
for (int i = 0; i < totalNumberOfSeats; i++)
{
string seatInfo = GetSeatInfoAt(i);
if (seatInfo.Reserved)
{
lstSeatInfoStrings.Add(seatInfo);
}
}
if (lstSeatInfoStrings.Count == 0)
{
return null;
}
return lstSeatInfoStrings.ToArray();
}