I've got 2 ArrayLists and i want to find common elements from those 2.
Example:
Arraylist 1 contains: "Cat", "Dog", "Phone", "Watch", "Monkey".
Arraylist 2 contains: "Dog", "Phone", "Chair".
As a result i want to have a method which returns : "Dog" and "Phone".
I've used an Array before and the intersect worked with the Array, but I'm using an ArrayList now.
Try this,
ArrayList list1 = new ArrayList{ "cat", "dog" ,"phone", "watch"};
ArrayList list2 = new ArrayList{ "pen", "cat", "dog", "lamp" };
var elements = Enumerable.Intersect(list1.ToArray(), list2.ToArray()).ToArray();
ArrayList result = new ArrayList(elements);
Hope helps,
You can try this:
ArrayList first = new ArrayList();
first.Add("Cat");
first.Add("Dog");
ArrayList second = new ArrayList();
second.Add("Dog");
second.Add("Phone");
ArrayList final = new ArrayList();
foreach (var i in first.Cast<string>().Intersect(second.Cast<string>()))
final.Add(i);
The resulting ArrayList is Dog
I would implement an extension method for Intersect, so you can use it just as you would for any other iterable collection:
public static class ArrayListExtensions
{
public static ArrayList Intersect(this ArrayList source, ArrayList other)
=> new ArrayList(source.ToArray().Intersect(other.ToArray()).ToArray());
}
Usage:
var first = new ArrayList(new[] { "Cat", "Dog", "Phone", "Watch", "Monkey" });
var second = new ArrayList(new[] { "Dog", "Phone", "Chair" });
var intersection = first.Intersect(second); // intersection is now an ArrayList
Related
I've had a look at both ASP.Net c# adding items to jagged array and vb.net assign array to jagged array but I just can't seem to work this out...
So, essentially, it's this. I have a function that builds a list of values from a query or two. I'm adding each value returned to a list then converting the list to an array. All is good
However, I now need to return two values, so a multi dimension array appears more suitable. Essentially, this is what I want to do:
string[][] array2D = new string[2][];
array2D[0] = new string[3] { "one", "two", "three" };
array2D[1] = new string[3] { "abc", "def", "ghi" };
All good so far. However, I don't know the values that I want to plug in to the array at the time of array initialisation, so, this is what I'd expect to be able to do:
string[][] array2D = new string[2][];
//array2D[0] = new string[3] { "one", "two", "three" };
//array2D[1] = new string[3] { "abc", "def", "ghi" };
string[] deviceIDS = { "one", "two", "three" };
string[] groupIDS = { "abc", "def", "ghi" };
array2D[0] = new string[deviceIDS.Length] deviceIDS;
array2D[1] = new string[deviceIDS.Length] groupIDS;
But it really doesn't like the last two lines, report it needs a ;
You have already created arrays here:
string[] deviceIDS = { "one", "two", "three" };
string[] groupIDS = { "abc", "def", "ghi" };
So you only need to set references to these arrays:
array2D[0] = deviceIDS;
array2D[1] = groupIDS;
var listA = new List<string> {"One", "Two", "Three"};
var listB = new List<int> {1, 2, 3};
var listC = new List<Tuple<string, int>>();
Here I've got two Lists with the same length. Is there a way to fill third listC with LINQ?
"One", 1
"Two", 2
"Three", 3
I think you are looking the Zip extension method:
var listA = new List<string> { "One", "Two", "Three" };
var listB = new List<int> { 1, 2, 3 };
var listC = listA.Zip(listB, (s, i) => new Tuple<string, int>(s, i)).ToList();
How can I create a list with a fixed set of entries.
Learning C# and doing an exercise to list all cards in a deck of cards(without jokers). Going to use two foreach loops to print them out.
However I cannot get a default list of the cards(I am overloading the method). Looking at the docs http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx and some examples http://www.dotnetperls.com/list and each element is added in individually.
// from dotnet perls
class Program
{
static void Main()
{
List<string> dogs = new List<string>(); // Example List
dogs.Add("spaniel"); // Contains: spaniel
dogs.Add("beagle"); // Contains: spaniel, beagle
dogs.Insert(1, "dalmatian"); // Contains: spaniel, dalmatian, beagle
foreach (string dog in dogs) // Display for verification
{
Console.WriteLine(dog);
}
}
}
I have tried both the Add and Insert methods but cannot create my lists.
// my code
using System;
using System.Collections.Generic;
using System.Linq;
class Deck
{
static void Main()
{
List<string> deck = new List<string>();
deck.Insert("Ace", 2, 3, 4, 5, 6, 7, 8, 9, 10, "Jack", "Queen", "King");
List<string> colour = new List<string>();
colour.Add("Hearts", "Diamonds", "Spades", "Clubs");
foreach (string card in deck)
{
foreach(string suit in colour)
{
Console.Write(colour + " " + card);
}
}
}
}
List.Add or List.Insert doesn't take variable length parameters. You may need to use List.AddRange method.
List<string> deck = new List<string>();
deck.AddRange(new []{"Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"});
List<string> colour = new List<string>();
colour.AddRange(new []{"Hearts", "Diamonds", "Spades", "Clubs"});
Also note that I've converted the numerical parameters to string as the list is List<string>, otherwise it won't compile.
I have two lists of strings. How do I get the list of distinct values between them or remove the second list elements from the first list?
List<string> list1 = { "see","you","live"}
List<string> list2 = { "see"}
The result should be {"you","live"}.
It looks to me like you need Enumerable.Except():
var differences = list1.Except(list2);
And then you can loop through the differences:
foreach(var difference in differences)
{
// work with each individual string here.
}
If you want to get items from the first list except items in the second list, use
list1.Except(list2)
If you want to get items that are in the first list or in the second list, but not both, you can use
list1.Except(list2).Concat(list2.Except(list1))
This is the good way I find unique....
Unique from two list
var A = new List<int>() { 1,2,3,4 };
var B = new List<int>() { 1, 5, 6, 7 };
var a= A.Except(B).ToList();
// outputs List<int>(2) { 2,3,4 }
var b= B.Except(A).ToList();
// outputs List<int>(2) { 5,6,7 }
var abint= B.Intersect(A).ToList();
// outputs List<int>(2) { 1 }
here is my answer,
find distinct value's in two int list and assign that vlaues to the third int list.
List<int> list1 = new List <int>() { 1, 2, 3, 4, 5, 6 };
List<int> list2 = new List<int>() { 1, 2, 3, 7, 8, 9 };
List<int> list3 = new List<int>();
var DifferentList1 = list1.Except(list2).Concat(list2.Except(list1));
foreach (var item in DifferentList1)
{
list3.Add(item);
}
foreach (var item in list3)
{
Console.WriteLine("Different Item found in lists are{0}",item);
}
Console.ReadLine();
I am writing my testcode and I do not want wo write:
List<string> nameslist = new List<string>();
nameslist.Add("one");
nameslist.Add("two");
nameslist.Add("three");
I would love to write
List<string> nameslist = new List<string>({"one", "two", "three"});
However {"one", "two", "three"} is not an "IEnumerable string Collection". How can I initialise this in one line using the IEnumerable string Collection"?
var list = new List<string> { "One", "Two", "Three" };
Essentially the syntax is:
new List<Type> { Instance1, Instance2, Instance3 };
Which is translated by the compiler as
List<string> list = new List<string>();
list.Add("One");
list.Add("Two");
list.Add("Three");
Change the code to
List<string> nameslist = new List<string> {"one", "two", "three"};
or
List<string> nameslist = new List<string>(new[] {"one", "two", "three"});
Just lose the parenthesis:
var nameslist = new List<string> { "one", "two", "three" };
Posting this answer for folks wanting to initialize list with POCOs and also coz this is the first thing that pops up in search but all answers only for list of type string.
You can do this two ways one is directly setting the property by setter assignment or much cleaner by creating a constructor that takes in params and sets the properties.
class MObject {
public int Code { get; set; }
public string Org { get; set; }
}
List<MObject> theList = new List<MObject> { new MObject{ PASCode = 111, Org="Oracle" }, new MObject{ PASCode = 444, Org="MS"} };
OR by parameterized constructor
class MObject {
public MObject(int code, string org)
{
Code = code;
Org = org;
}
public int Code { get; set; }
public string Org { get; set; }
}
List<MObject> theList = new List<MObject> {new MObject( 111, "Oracle" ), new MObject(222,"SAP")};
This is one way.
List<int> list = new List<int>{ 1, 2, 3, 4, 5 };
This is another way.
List<int> list2 = new List<int>();
list2.Add(1);
list2.Add(2);
Same goes with strings.
Eg:
List<string> list3 = new List<string> { "Hello", "World" };
List<string> nameslist = new List<string> {"one", "two", "three"} ?
Remove the parentheses:
List<string> nameslist = new List<string> {"one", "two", "three"};
It depends which version of C# you're using, from version 3.0 onwards you can use...
List<string> nameslist = new List<string> { "one", "two", "three" };
I think this will work for int, long and string values.
List<int> list = new List<int>(new int[]{ 2, 3, 7 });
var animals = new List<string>() { "bird", "dog" };