开发者

arraylist checks

开发者 https://www.devze.com 2022-12-15 11:24 出处:网络
I have an arraylist that contains urls in the form similar to : stackoverflow.com/questions/ask/hello stackoverflow.com/questions/ask

I have an arraylist that contains urls in the form similar to :

stackoverflow.com/questions/ask/hello

stackoverflow.com/questions/ask

s开发者_运维知识库tackoverflow.com/questions

stackoverflow.com/

If I only wanted to keep the first one and remove the rest, how would I go about doing that since technically they are not duplicates. I thought of using substring manipulation but not to sure how to implement that.any ideas appreciated.


Assuming I understand the question correctly, you can accomplish this by looping through your ArrayList, building a list of found domains, and simultaneously outputting a new list only when the found domain is not already a member of that first list.

Or, you could build a dictionary of domain to url by iterating through the ArrayList in reverse. Since a dictionary can only have one value per key, the URLs will overwrite themselves in the dictionary and you will only have one URL per domain. Since you iterated in reverse, you will be left with a dictionary containing the first match in the ArrayList. You could then use LINQ to grab just the values (e.g. MyDictionary.Select(elem => elem.Value)).

An example implementation of the second way I mentioned (in C#, you can convert it) is:

Dictionary<string, string> MyDictionary = new Dictionary<string, string>();
foreach(string Url in MyArrayList.Reverse())
    MyDictionary[Url.Split("/")[0]] = Url;

There are dozens of ways you could accomplish this task. These are just two examples.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号