it is possible to store data in of two arraylist into <list>?
here's my code with two arrays that will merge:
ArrayList arrPrices = new ArrayList();
List<StockInfoPrice> lstStockInfoPrice = new List<StockInfoPrice>();
Util oUtils = new Util();
arrPrices = oUtils.GetPrices(SymbolIndex);
ArrayList arrDetails = new ArrayList();
List<StockInfoDetails> lstStockInfoDetails = new List<StockInfoDetails>();
Util oUtils = new Util();
arrPrices = oUtils.GetDetails(SymbolIndex);
You can do it with linq simply:
lstStockInfoPrice.AddRange(arr1.Cast<StockInfoPrice>());
lstStockInfoPrice.AddRange(arr2.Cast<StockInfoPrice>());
See Cast in IEnumerable.
It is possible.
You could try the following if oUtils.GetPrices(SymbolIndex) returns StockInfoPrice;
lstStockInfoPrice.AddRange(oUtils.GetPrices(SymbolIndex));
I this Util class isn't your own, then you're stuck with Marius' answer. However, if you control that Util class then you could make the GetPrices and GetDetails methods return someting with type IEnumerable and IEnumerable respectively.
Then, you can add the whole lot to another list with List.AddRange() method.
As an aside, your allocation in the declaration of arrPrices is a waste of time - the allocated object is never used and will then be subject to garbage collection.
Your GetPrices() method returns an ArrayList - ie, a new arrayList, and
arrPrices = oUtils.GetPrices(SymbolIndex);
simply makes arrPrices refer to the new list. There are then no references to the one you allocated when you declared arrPrices, so it's thrown away.
Do it like this:-
ArrayList arrPrices;
List<StockInfoPrice> lstStockInfoPrice = new List<StockInfoPrice>();
Util oUtils = new Util();
arrPrices = oUtils.GetPrices(SymbolIndex);
If you want to move the value from arrPrices to lstStockInfoPrice and lstStockInfoDetails, you could iterate over the array list and put the elements in the list. Something like this:
foreach(var o in arrPrices)
{
lstStockInfoPrice.Add(o); // or Add((StockInfoPrice)o)
}
Related
I have a question about Enumerable.Repeat function.
If I will have a class:
class A
{
//code
}
And I will create an array, of that type objects:
A [] arr = new A[50];
And next, I will want to initialize those objects, calling Enumerable.Repeat:
arr = Enumerable.Repeat(new A(), 50);
Will those objects have the same address in memory?
If I will want to check their hash code, for example in that way:
bool theSameHashCode = questions[0].GetHashCode() == questions[1].GetHashCode();
This will return me true, and if I will change one object properties, all other objects will change it too.
So my question is: is that properly way, to initialize reference type objects? If not, then what is a better way?
Using Enumerable.Repeat this way will initialize only one object and return that object every time when you iterate over the result.
Will those objects have the same address in memory?
There is only one object.
To achieve what you want, you can do this:
Enumerable.Range(1, 50).Select(i => new A()).ToArray();
This will return an array of 50 distinct objects of type A.
By the way, the fact that GetHashCode() returns the same value does not imply that the objects are referentially equal (or simply equal, for that matter). Two non-equal objects can have the same hash code.
Just to help clarify for Camilo, here's some test code that shows the issue at hand:
void Main()
{
var foos = Enumerable.Repeat(new Foo(), 2).ToArray();
foos[0].Name = "Jack";
foos[1].Name = "Jill";
Console.WriteLine(foos[0].Name);
}
public class Foo
{
public string Name;
}
This prints "Jill". Thus it shows that Enumerable.Repeat is only creating one instance of the Foo class.
When using the following code to create an array:
var foos = Enumerable.Repeat(new Foo(), 2).ToArray();
The reason why each location in the array is the same is because you are passing an object, and not a function that creates an object, the code above is the same as:
var foo = new Foo();
var foos = Enumerable.Repeat(foo , 2).ToArray();
The reason above also explains why using a Select statement, like in the code below, creates a new object for each entry, because you are passing a function that dictates how each object is created, rather than the object itself.
Enumerable.Range(1, 2).Select(i => new Foo()).ToArray();
I would use a simple for loop to populate an array with new reference types.
I am trying to do
var mahByteArray = new ArrayList<byte>();
And it does not work.
It says this:
The non-generic type 'System.Collectios.ArrayList' cannot be used with
type arguments
What is the proper way to do declare a byte ArrayList?
You are confusing Java ArrayList collections with C# List generic collections. Both are used to declare collections, but the first one is used in Java as being a type defined in the generic class List for Collections framework and in the last one is used in C# language as an implicit generic type.
So, you must declare as being a List type. See details on List.
var mahByteArray = new List<byte>();
or
List<byte> mahByteArray = new List<byte>() { 2, 3, 4 };
sure you can use a ArrayList
var mahByteArray = new ArrayList();
mahByteArray.Add((byte) 230);
ArrayList<> isn't generic. You can use generic List<> instead
var mahByteArray = new List<byte>();
ArrayList is not generic. Use System.Collections.Generic.List<T> instead. The List<T> class is the generic equivalent of the ArrayList class. It implements the IList<T> generic interface using an array whose size is dynamically increased as required.
var mahByteArray = new List<byte>();
Also take a look at this: Difference between ArrayList and Generic List.
EDIT:
To whoever marked the question as duplicate. That question is for how to create a deep copy. My question was how to make sure a the copy constructor is called when copying a list of class elements.
I'm trying to make a deep copy of a List that contain custom class elements. If I have a List of strings I can just use
List<string> secondList = new List<string>(firstList);
and then freely modify the elements in the second list without effeting the ones in the firwst list. But when I try to do the same with a custom class type both lists get changed. To try and solve it I made a small test program that just has this class.
class TestClass
{
public string name;
public TestClass(string n)
{
name = n;
}
public TestClass(TestClass original)
{
name = original.name;
}
}
And all my program does is this
TestClass t = new TestClass("Name1");
List<TestClass> list1 = new List<TestClass>();
list1.Add(t);
List<TestClass> list2 = new List<TestClass>(list1);
list2[0].name = "Name2";
That last line of code changes the name of the first element in both lists, which I do no want.
The issue here is that your objects are reference types, and the lists hold references to those objects.
This means that even though your second list has a COPY of the references from the first list, the references are still pointing to the original objects.
In order to solve this, you must clone not the references in the lists but instead the actual objects that you have stored in the lists.
You have already defined a copy constructor for your class, so you can use that to make a deep copy of the list as follows:
var list2 = list1.Select(item => new TestClass(item)).ToList();
You create a reference with this line of Code:
List<TestClass> list2 = new List<TestClass>(list1);
But you won't like to use Call-by-Reference. You Need Call-by-Value
in this Approach.
so the working code in lambda-expression is the following one:
TestClass t = new TestClass("Name1");
List<TestClass> list1 = new List<TestClass>();
list1.Add(t);
List<TestClass> list2 = new List<TestClass>();
list2 = list1.Select(item => new TestClass(item)).ToList();
list2[0].name = "Name2";
Have fun with it...
I tried searching by "C# new string array pass dynamic" but could not find anything relevant.
int[] IDs = someMethodCall();
List<string> values = new List<string>();
foreach (int i in IDs)
{
values.Add(i.ToString());
}
someClass sc = new someClass();
sc.Value = new string[] { "values.string1", "values.string2", ... };
What I'm trying to do is to pass the strings from values to sc.Value, so I don't have to write them out (since I don't what they'll be beforehand).
sc.Value is a string[] as defined by the class I'm using from an API (not written by me).
What is the best way to do this dynamically? In other words, how to pass in dynamic values to a string[] construction?
If I'm not missing something,you can just use ToArray method
sc.Value = values.ToArray();
BTW, you don't even need to create a list in the first place:
sc.Value = someMethodCall().Select(x => x.ToString()).ToArray();
I'm a little confused by the way you word your questioning, but I think you are trying to send your list to an array, which is easily done using the code below:
List<string> values = new List<string>();
sc.Value = values.ToArray();
How about just using the built-in method ToArray:
sc.Value = values.ToArray();
Comes with List, and is an extension method for IEnumerable if you can use LINQ.
I'm aware that an ArrayList is probably not the way to go with this particular situation, but humor me and help me lose this headache.
I have a constructor class like follows:
class Peoples
{
public string LastName;
public string FirstName;
public Peoples(string lastName, string firstName)
{
LastName = lastName;
FirstName = firstName;
}
}
And I'm trying to build an ArrayList to build a collection by calling on this constructor. However, I can't seem to find a way to build the ArrayList properly when I use this constructor. I have figured it out with an Array, but not an ArrayList.
I have been messing with this to try to build my ArrayList:
ArrayList people = new ArrayList();
people[0] = new Peoples("Bar", "Foo");
people[1] = new Peoples("Quirk", "Baz");
people[2] = new Peopls("Get", "Gad");
My indexing is apparently out of range according to the exception I get.
It should be:
people.Add(new Peoples(etc.));
instead of
people[0] = new people()...;
Or better yet:
List<People> people = new List<People>();
people.Add(new People);
Just to be complete. Using a straight array:
People[] people = new People[3];
people[0] = new People();
Try people.Add(new Peoples("Bar", "Foo");
You should add elements to the list. Like the following
ArrayList people = new ArrayList();
people.Add(new Peoples("Bar", "Foo"));
You need to do
people.Add (new Peoples("Bar", "Foo"));
people.Add (new Peoples("Quirk", "Baz"));
people.Add (new Peoples("Get", "Gad"));
When you attempt to call people[i] without first populating the array list you will get the IndexOutOfRangeException. You must first add to the ArrayList.
ArrayList list = new ArrayList();
list.Add(new Peoples("Bar", "Foo"));
You can then access the list by the index which would be done in a foreach or for loop.
Is there a reason you are not using List<Peoples> which would give you a strongly typed collection?
Also, you have publicly accessible fields in the class although I realise you probably just threw together that code for the question.
You should use the ArrayList.Add function to add to the array list.
ArrayList peoplesArray = new ArrayList();
peoplesArray.Add(new Peoples("John","Smith");
FYI, ArrayList is considered evil by many. As Kevin said, it would be better to us List<People>.
List is what is called a generic. Google 'strongly typed', 'boxing', and 'generics' for a better understanding of why.
back to your original question:
An array's size must be declared when instantiated, i.e.
People[] people = new People[5];
this creates 5 empty cells in the array so you can access the cells using a subscript i.e. [0]
An ArrayList or List<T> when instantiated using the default constructor has no cells i.e.
List<People> people = new List<People>();
people[0] does not exist at this point.
use people.Add(new People("first", "last")); to add a new cell to the list. now the subscript [0] is valid, but [1] is still invalid because there is only one cell.
A list i.e. ArrayList or List can grow dynamically by using .Add(). Once added to a list, you can reference them using the subscript [i], but you cannot use the subcript to add them.