I can use 'foreach' for iteration Hashtable, but i need in use cycle with index (for)
I use this:
int i = 0;
foreach (var field in Hashtable) {
i++;
//action
}
but i think this is not good
I NEED CYCLE 'FOR'
For loops are useful for collections that can be indexes by numbers.
Therefore the only useful way to iterate over a HashTable is to iterate over the Hashtables keys.
var keys = hashtable.Keys.ToArray();
for (int i = 0; i < keys.Length; i++)
var value = hashtable[keys[i]];
When i is even, do something, and when i is odd do something else.
Related
I have a huge list of strings _wordList List<string> containing about 100,000 values. The problem I'm having is that I also require multiple nested loops within this. The nested loop is also a list but with a structure containing only variables, containing about 0-100 values depending on what happens
for (int y = 0; y < _wordList.Count; y++)
{
string word = _wordList[y];
for(int x = 0; x < _secondWordList.Count; x++)
{
if (!word.Contains(_secondWordList[x].Word) || word == _secondWordList[x].Word)
continue;
// do other stuff
}
}
Here is part of the code, I won't post all of it since most of it will be irrelevant but within the second loop I have about 2 other short loops, the whole function completes in 350-600ms. What would the best way to optimize the loops? The word.Contains also have an impact of about 100-150ms on performance.
It seems that you're looking for a text search, and in this case, you can benefit from projects like LuceneNet.
If the call to string.Contains() didn't exist and you were looking for exact matches I was to suggest that you swap the list for hashset as it'll give you a great performance boost in your case. Like below.
static void Main(string[] args)
{
var wordList = new HashSet<string>(); //Assuming Initialized
var secondWordList = new List<X>(); //Assuming Initialized
for (var c = 0; c < secondWordList.Count; c++)
{
if(wordList.Contains(secondWordList[c].Word))
continue;
// do other stuff
}
}
With this, you're going to iterate on the smaller list and look for the value in the HashSet which has a complexity of O(1), which Means that it'll be extremely fast.
I have a simple question that I can't find a clean answer to when I google.
How do I iterate a foreach loop between index 40-60 and get the values for the indexes?
List<int> list1 = new List<int>();
for (int i = 0; i < 100; i++)
{
list1.Add(i);
}
foreach (var i in list1)
{
//How to iterate between index: 40 to 60 ?
}
The easiest approach would be to use a regular for loop:
for (int i = 40; i < 60; ++i)
{
int value = list1[i];
// Do something with the value
}
Note - if you want to get the value for index 60 too (i.e., the range is inclusive), you should use the <= operator instead of <.
Foreach loop doesn't use an index for traversing an array or collection. They take advantage of the enumerator in an IEnumerable. This answer here has a great explanation of that.
If you want you can add an int before the foreach loop and increment within the loop, but that's all you can do. I would advise that you filter your list1 beforehand and then use it in the foreach loop.
If you really want to use a foreach then you need to write this
List<int> list1 = new List<int>();
foreach(int i in Enumerable.Range(40, 20))
{
list1.Add(i);
}
But, lacking better info on your request to use foreach, then I agree to use a standard for-loop
Aside from other more appropriate answers (e.g. just use a for loop), here's a solution that uses the .Where() overload that provides the index of the element as a parameter to the predicate:
foreach (var i in list1.Where((x, index) => index >= 40 && index <= 60))
{
//How to iterate between index: 40 to 60 ?
}
I'm trying to make a look to print each of every value once:
Something like this.
Lets pretend that the object letters contains "one,two ...ten"
And then there is the object numbers that contains "1,2,3,4...10"
Now if I want the loop to print:
One
1
Two
2
Three
3
How would the loop look like?
I tried it like this:
foreach (var i in letters)
{
Console.WriteLine(i);
foreach(var a in numbers)
{
Console.WriteLine(a);
}
}
But this returns:
One
1
2
Two
1
2
Three
1
2
And that result isn't what I want..
How can I nest the loops to make it print the way I want it?
Maybe you can use IEnumerable<T>.Zip(...), see here, to make combinations.
var data = letters.Zip(numbers, (l, n) => new { letter = l, number = n})
foreach (var item in data) {
Console.Writeline(item.letter);
Console.Writeline(item.number);
}
use forloop insted of foreach use it like this
for (int i=0;i<letters.length;i++)
{
Console.WriteLine(letters[i]);
Console.WriteLine(numbers[i]);
}
Don't do two nested loops, they are for enumerating over all possible pairs from two collections.
Instead, make a loop on the index, and use it for both collections, or use LINQ's Zip method:
foreach (var pair in letters.Zip(numbers, (l, n) => new {Letter=l, Number=n})) {
Console.WriteLine("{0} - {1}", pair.Letter, pair.Number);
}
Assuming your Numbers and Letters are collections that derive from IEnumerable, you could do something like this:
var zipped = letters.Zip(numbers, Tuple.Create);
foreach (var tuple in zipped)
{
Console.WriteLine(tuple.Item1);
Console.WriteLine(tuple.Item2);
}
You need a single loop to iterate over both lists:
for (int index = 0; index < letters.Count; index++)
{
Console.WriteLine(letters[index]);
Console.WriteLine(numbers[index]);
}
This presupposes that your lists are the same length. If they're not you'd have to set the upper limit to the length of the shorter list.
for (int index = 0; index < Math.Min(letters.Count, numbers.Count); index++)
You're close - the second loop should not be within the first, but you should use one loop to iterate over both arrays. Try:
for (int i = 0; i < letters.size(); i++) {
Console.WriteLine(letters.getItem(i));
Console.WriteLine(numbers.getItem(i));
}
Note that this assumes a size() method to return the number of items and a getItem() method to return a specific item from the object.
What's happening is that for every time your outside loop runs, the inside one runs twice.
That's because your loops are nested, there's no getting around this.
If you absolutely must use nested loops for this, you'd have to add a check whether your number has been printed yet
Something like:
foreach(var i in letters)
{
Console.WriteLine(i);
foreach(var a in numbers)
{
if (a.isPrinted) //if it has been printed already
{
continue; //skip ahead
}
else
{
Console.WriteLine(a.number);
a.isPrinted = true;
break; //stop and jump out of the foreach loop
}
}
}
This also means that each number is actually an object that holds the bool isPrinted and int number
I wouldn't doing it like that, it's ridiculously inefficient.
You should do what others have already suggested.
So I've been trying to turn this codes for into a for each, but I keep running into problems, and I'm not all that sure how to do it, the only thing I've done that will work is a small code but it repeats several times, I'm still really new to C# and using for() so I'm not really understanding what to do. Sorry for being so new to this, and I appreciate all the help I can get!
double[] numbers = new double[9.7, 2.2, 4.3, 1.7, 5.6, 3.6, 4.5, 1.3, 4.6, 3.0];
static double ComputeSum(double[] array)
{
double sum = 0;
int intCounter;
for (intCounter = 0; intCounter < array.Length; intCounter++)
{
sum += array[intCounter];
}
return sum;
}
static double ComputeAverage(double[] array)
{
return ComputeSum(array) / array.Length;
}
You got your specific question answered, but not the more general question of "how do I turn a for loop into a foreach?
Let's look at your loop, with some better naming. (Don't name things according to their type in C#. Name things according to their meaning.)
for (index = 0; index< array.Length; index++)
{
sum += array[index];
}
The most important thing is that you understand the idea that this loop represents. A surface level reading is "for every valid index in this array, obtain the element value associated with that index and add it to the sum". But we can make that a bit more abstract. We could also say that the loop means "for every element in the collection, add it to the sum".
That is the key difference between the for loop and the foreach loop. The for loop concentrates on the index, and you fetch the value via the index. The foreach loop concentrates on the value itself, and doesn't consider the index at all.
Knowing that we can now see how to translate a for into a foreach. First locate the bit that is the value, and make a variable for it:
for (index = 0; index < array.Length; index++)
{
var item = array[index];
sum += item;
}
Now we see that the loop body can be expressed almost entirely in terms of an operation on the item, not on the index. We then translate that into a foreach:
foreach (var item in array)
{
sum += item;
}
Everything dealing with the index gets deleted. If you find you cannot delete everything dealing with the index, then you probably should not be using a foreach.
Since foreach loop presents elements of the collection one-by-one to your code, all you need to do is
foreach (double num in Numbers)
{
sump += num;
}
Console.WriteLine("{0}", sump);
You could also use Sum extension method to eliminate loop constructs altogether:
var sump = Numbers.Sum(); // <<== The loop is hidden inside the Sum() method
Console.WriteLine("{0}", sump);
EDIT: so it looks like this is normal behavior, so can anyone just recommend a faster way to do these numerous intersections?
so my problem is this. I have 8000 lists (strings in each list). For each list (ranging from size 50 to 400), I'm comparing it to every other list and performing a calculation based on the intersection number. So I'll do
list1(intersect)list1= number
list1(intersect)list2= number
list1(intersect)list888= number
And I do this for every list. Previously, I had HashList and my code was essentially this: (well, I was actually searching through properties of an object, so I
had to modify the code a bit, but it's basically this:
I have my two versions below, but if anyone knows anything faster, please let me know!
Loop through AllLists, getting each list, starting with list1, and then do this:
foreach (List list in AllLists)
{
if (list1_length < list_length) //just a check to so I'm looping through the
//smaller list
{
foreach (string word in list1)
{
if (block.generator_list.Contains(word))
{
//simple integer count
}
}
}
// a little more code, but the same, but looping through the other list if it's smaller/bigger
Then I make the lists into regular lists, and applied Sort(), which changed my code to
foreach (List list in AllLists)
{
if (list1_length < list_length) //just a check to so I'm looping through the
//smaller list
{
for (int i = 0; i < list1_length; i++)
{
var test = list.BinarySearch(list1[i]);
if (test > -1)
{
//simple integer count
}
}
}
The first version takes about 6 seconds, the other one takes more than 20 (I just stop there cuz otherwise it would take more than a minute!!!) (and this is for a smallish subset of the data)
I'm sure there's a drastic mistake somewhere, but I can't find it.
Well I have tried three distinct methods for achieving this (assuming I understood the problem correctly). Please note I have used HashSet<int> in order to more easily generate random input.
setting up:
List<HashSet<int>> allSets = new List<HashSet<int>>();
Random rand = new Random();
for(int i = 0; i < 8000; ++i) {
HashSet<int> ints = new HashSet<int>();
for(int j = 0; j < rand.Next(50, 400); ++j) {
ints.Add(rand.Next(0, 1000));
}
allSets.Add(ints);
}
the three methods I checked (code is what runs in the inner loop):
the loop:
note that you are getting duplicated results in your code (intersecting set A with set B and later intersecting set B with set A).
It won't affect your performance thanks to the list length check you are doing. But iterating this way is clearer.
for(int i = 0; i < allSets.Count; ++i) {
for(int j = i + 1; j < allSets.Count; ++j) {
}
}
first method:
used IEnumerable.Intersect() to get the intersection with the other list and checked IEnumerable.Count() to get the size of the intersection.
var intersect = allSets[i].Intersect(allSets[j]);
count = intersect.Count();
this was the slowest one averaging 177s
second method:
cloned the smaller set of the two sets I was intersecting, then used ISet.IntersectWith() and checked the resulting sets Count.
HashSet<int> intersect;
HashSet<int> intersectWith;
if(allSets[i].Count < allSets[j].Count) {
intersect = new HashSet<int>(allSets[i]);
intersectWith = allSets[j];
} else {
intersect = new HashSet<int>(allSets[j]);
intersectWith = allSets[i];
}
intersect.IntersectWith(intersectWith);
count = intersect.Count;
}
}
this one was slightly faster, averaging 154s
third method:
did something very similar to what you did iterated over the shorter set and checked ISet.Contains on the longer set.
for(int i = 0; i < allSets.Count; ++i) {
for(int j = i + 1; j < allSets.Count; ++j) {
count = 0;
if(allSets[i].Count < allSets[j].Count) {
loopingSet = allSets[i];
containsSet = allSets[j];
} else {
loopingSet = allSets[j];
containsSet = allSets[i];
}
foreach(int k in loopingSet) {
if(containsSet.Contains(k)) {
++count;
}
}
}
}
this method was by far the fastest (as expected), averaging 66s
conclusion
the method you're using is the fastest of these three. I certainly can't think of a faster single threaded way to do this. Perhaps there is a better concurrent solution.
I've found that one of the most important considerations in iterating/searching any kind of collection is to choose the collection type very carefully. To iterate through a normal collection for your purposes will not be the most optimal. Try using something like:
System.Collections.Generic.HashSet<T>
Using the Contains() method while iterating over the shorter list of two (as you mentioned you're already doing) should give close to O(1) performance, the same as key lookups in the generic Dictionary type.