I am sure if i look hard enough i can find my answer but so far i can't find a clear cut answer.
What i am trying to do is use the value of an item in a listbox which contains 7 items as a numerical identifier for a specific array element. (all items in the list are strings)
array[listbox.value] = my new data for that array element
i know i can pull the string of the item out but and that i can identify a specific item in the list with.
list1.Items[value].ToString();
i just want to know if i can do reverse the alternative is a pain to code as its a lot more lines of code comparing the string in the list to each item in my array until i find a match while i know all items in the list are the same order as the array.
Instead of using an array, you can use a dictionary.
Dictionary<string, valueType> myArray = new Dictionary<string, valueType>();
myArray["Item1"] = some value
myArray["Item2"] = some value
... etc
Then later
myArray[listbox.Value.ToString()] = my new value
Thats really the best way to refer to an array index by a string value. The type for the indexer does not necessarily need to be a string, it can be any type that is uniquely identifiable.
Related
I have a list with some elements and I want to remove elements from another list. An item should be removed if its value Contains (not equals) the value from another list.
One of the ways is to do this:
var MyList = new List<string> { ... }
var ToRemove = new List<string> { ... }
MyList.RemoveAll(_ => ToRemove.Any(_.Contains));
It works...
but, I have a LOT of lists (>1 million) and since the ToRemove can be sorted, it would make sense to use that in order to speed the process.
It's easy to make a loop that does it, but is there a way to do this with the sorted collections?
Update:
On 20k iterations on a text with our forbidden list, I get this:
Forbidden list as List -> 00:00:07.1993364
Forbidden list as HashSet -> 00:00:07.9749997
It's consistent after multiple runs, so the hashset is slower
Since this is a removal of strings that contain strings that are in another list, a HashSet wouldn't be much help. Actually not much would be unless you were looking for exact full matches or maintain an index of all substrings (expensive and AFIK only SQL Server does this semi-efficiently outside the BigData realm).
If all you cared about was if it starts with items in 'ToRemove', sorting could help. Sort the 'MyList' and foreach string in 'ToRemove' custom binary search to find any string starting with that string and RemoveAt index until not starts with, then decrement index backwards removing until not starts with.
Well, sorting ToRemove may be beneficial because of binary search O(log n) complexity (you will need to rewrite _ => ToRemove.Any(_.Contains)).
But, instead, using a HashSet<string> instead of List<string> for ToRemove will be much faster, because finding an element in a hashset (using Contains) is O(1) operation.
Also, using LinkedList<string> for MyList can potentially be beneficial, since removing an item from a linked list is generally faster than removing from an array based list because of array size adjusting.
I would like to ask if there is a data structure or some sort of dictionary that would help me to solve this problem.
First I create array of arrays of int.
11,32,21,10;
455,476,465,454;
11,32,476,455;
...
10,32,21,11;
Besides that I am adding points to separate array of arrays
Pt11,Pt32,Pt21,Pt10;
Pt455,Pt476,Pt465,Pt454;
Pt11,Pt32,Pt476,Pt455;
...
Pt10,Pt32,Pt21,Pt11;
Is there a way to create a dictionary to add array of points by name, and the name is array of integers - 10,32,21,11 - .
But the problem I have, is that I want to add to the same dictionary point array if name is mixed - 11,32,21,10.
So dictionary would point to the same collection - if I call 10,32,21,11 or 11,32,21,10. In other words 10,32,21,11 and 11,32,21,10 is the same name because elements are just ordered differently.
I do not know if it clear as I mixing several things:
1. Is it possible to create at least a dictionary whose name is an array?
2. Then if yes it probably would not point to the same points array if I add elements dictionary.Add(10,32,21,11, pt[]); dictionary.Add(11,32,21,10, pt[]);
The dificulty I'm facing is as follows:
I need to create a dictionary with something like 10 main definitions in it. What I actually need is to be able to recognize some X amount of strings that should represent one certain string. I want to have like 10 main strings and to be able to add different representative string to each one of them. Example: I have the strings "animal", "fruit" and "object" and I want to assing e.g. the strings "dog", "cat" and "snake" to the string "animal". The point is that everytime I face one of those strings, I'll want replace it with "animal".
I imagine this as some kind of dictionary and I've read the documentary about this class in c#, but I'm not quite sure it's the best method so that's why I'm asking you. My idea was to create a new entry each time I face one of the substrings and to set that substring (e.g. "dog") as a key with value - the main string (in this case "animal"), but I find it quite inappropriate.
Following question - could you suggest a good enough method to store the data from that "dictionary" locally/online, so that I can collect data troughout the time I'm using my code.
Thanks a lot, friendly members of this community! :D
What would be best in your case would be to inverse your logic. You should use a Dictionary with a string a key and List as value and retrieve the value using the key which is a member of your list.
var d = new Dictionary<string,List<string>>();
d.Add("Animal", new List("Dog","Cat");
d.FirstOrDefault(x => x.Value.Contains("Cat")).Key;
If I understand correctly, you can just use a dictionary:
var d = new Dictionary();
d.Add("dog", "animal");
....
d["dog"]; //this gives you animal.
You can do this with each item you want to replace, and the dictionary will give you its replacement value.
My app scan Bluetooth devices and show in a table. But, sometimes it scan the same device two or more times and show the same device many times in the table.
I need to filter it. When the Name or UUID of the device is repeated the table will show just one time.
EDIT:
This What I tried, but isn't work...
CBPeripheral peripheral = this._peripherals [indexPath.Row];
List<string> filter = new List<string>();
filter.Add (peripheral.Identifier.AsString());
string[] array = {};
foreach (var c in filter) {
if (!ReferenceEquals (c, array)) {
int x = array.Length;
filter.CopyTo (0, array, 0, x);
}
}
foreach (string i in array) {
Console.WriteLine ("ARRAY: "+i.ToString());
}
Assuming your data is List<string>, you either need to
a. check for duplicates when you add new item to your list
if (!data.Contains(bt_id)) {
data.Add(bt_id);
}
b. remove duplicates after the fact
// requires System.Linq
var display_data = data.Distinct();
While browsing the devices in the area, store every device in an array.
Then you can either
clean the array of doubles and use that to populate the tableview
Add every object from the array to the tableview, and check if what you're adding to the tableview doesn't already exist in the array.
Or if you can, simply store their UDID or any unique code that you can use in a dictionary, and use the same technique as above before filling your tableview with the devices.
Ask me if i'm unclear or if you need more help
EDIT 1 :
You can find a way to clean an array of doubles on this post :
The best way to remove duplicate values from NSMutableArray in Objective-C?
You can also use sort descriptors to sort the array and then compare every element to the next one and delete if necessary. you can find information about sorting arrays easily on the internet, as well as cleaning arrays.
If you can store you values in an NSSet (There is a Mutable Variant as well) sets are unique in that they don't allow duplicates. Be aware though that an NSSet is unordered so there's also a variant for that NSOrderedSet. Have a play around with sets i think they'll fit your purpose quite nicely and they can be always converted back to an array once you've got rid of the dupes.
i am using array control in which i am saving value one by one.
now i have to delet one of the element and refresh it simultaneuosly.
for example....
string[] arr= new string(25);
arr[0]="A";
arr[1]="B";
arr[2]="C"; and so on....
now after deleting second element via arr[1]=null;
i want refreshed array like mentioned below...
arr[0]="A";
arr[1]="C"; and so on....
please help...
thanks in advance,,,
It sounds like you should be using a List<string> rather than an array, this would give exactly the functionality you are describing.
Although arrays can be resized (thanks #Austin Brunkhorst), this is not "cheap" and you would you would need to move everything around yourself.
It should be noted, that with lots of inserts and removes Lists can get very inefficient, so you'd be better off with a LinkedList<string>. These have advantages and disadvantages. Google linked list for more info.
When you have a static data amount you should use Array, BUT when you have dinamic data amount you should use List<>.
If you want to resize arrays, you have to create a new and copy all elements from the old to the new one.
arr = arr.Where(s => s != null).ToArray();
If you would use a List<string> you could use methods like List.Remove or List.RemoveAt.
If you'll be adding/deleting entries at arbitrary positions in your collection a lot, you'd be better off using a LinkedList<string> instead
Instead of Array you can go with List
List<int> list = new List<int>();
list.Add(2);
list.Add(3);
list.Add(5);
list.Add(7);
you will get more options like
Contains
Exists
IndexOf
For Removing the items you will get the functions like
Remove
ex: dogs.Remove("bulldog"); // Remove bulldog
RemoveAt
ex: list.RemoveAt(1);
RemoveAll