collections

Fixed Size to List

For declaration perspective the following is allowed IList<string> list= new string[3]; list.Add("Apple"); list.Add("Manago"); list.Add("Grapes"); 1) It compiles fine,But runtime i am getting "Collection was of fixed size" error. Ofcourse ,collection is dynamically grown by size,why did such declaration is accepted by...

Difference between ReadOnlyCollection string[] in the context of collection

IList<string> strList = new string[] { "Apple", "Mango", "Orange" }; IList<string> lst = new ReadOnlyCollection<string>(new[]{"Google", "MSN","Yahoo"}); In both cases i can not use "Add()" method for adding new items.then almost both declarations are same? ...

Overriding List<T>'s Add()

Can't i overload List's Add method ? class ListDemo<T>:List<T> { public override T Add<T>(T value) { return base.Add(value); } } I am receiving the following error : 1) Type parameter 'T' has the same name as the type parameter from outer type 'CollectionsExample.ListDemo 2) 'CollectionsExample.ListDemo.Add(T)...

VB6 Collection Remove Doesn't Fire Class_Terminate

I apologize in advance; this is a long question. I've tried to simplify as much as I can but it's still a bit more long-winded than I'd care to see. In some legacy code, we've got a VB6 collection. This collection adds objects via the .Add method and removes them via the .Remove method. However, via tracing I can see that sometimes w...

What is the best approach for having some kind of lazy iterator where the return is evaluated only on request?

import java.util.Collection; import example.Event; public interface Query { public boolean hasMore (); public Collection<Event> getNext ( long count ) throws Exception; } This is the interface I have, which I want to implement. The implementation is supposed to be like this: import java.util.ArrayList; import java.util.Col...

Iterate Doctrine Collection ordered by some field

I need something like this: $products = Products::getTable()->find(274); foreach ($products->Categories->orderBy('title') as $category) { echo "{$category->title}<br />"; } I know is it not possible, but... How can I do something like this without creating a Doctrine_Query? Thanks. ...

window.onload != <body onload="">

This is rather interesting, I think. Consider following code, both the window.onload and body onload="" call the same function. However, the results are different. It appears to me that window.onload has a problem with collections. Here's the code: <html> <script type="text/javascript"> window.onload = getSpanElements(); function ge...

Add items to a Dictionary<int, List<int>>

So I am looping through some objects and initializing a Dictionary> object. So first I check if the key exists, if it does I will add to the List If it doesn't, I will create a new key and new List Is that the right logic? I will have to do a: new List<int>(); the first time I insert an item right? i.e.: if(myDic.ContainsKey(car....

Checking if my dictionary<int, list<int>> has an item in the list.

I am doing this right now: foreach(..) { if(myDic.ContainsKey(car.ID) && myDic[car.ID].Contains(car.MfgID)) { // do something } } I was wondering if I can perform the check, but check if it doesn't exist, and then just do a continue. How would I do that? using a || or &&? I cant' assume the key/value is there so I don't want it t...

How to track if (List<T>)collection is modified?

I have a my custom collection derived from List(.MyItem.) I want to trace if my collection is modified, how can I do that? I am able to track Add Remove operations etc...by implement new definition of them but I don't see any method in List that gives me opportunity to track if collection is modified... ...

Is it OK to pass a generic Dictionary as a param to a method expecting IDictionary

AssemblyInstaller.Install expects a System.Collections.IDictionary. Am I right to be 'allergic' to using non-generic collections such as Hashtable or should I get over myself?! e.g. using System.Collections.Generic; using System.Configuration.Install; using System.Reflection; using AssemblyWithInstaller; namespace InstallerDemo { ...

Generic type in C#: restricting the type parameter to be a collection

I need to write a generic class where the type parameter must be something implementing ICollection<T>. Inside of MyClass I need the collection's item type (in the code snippet marked as ???). class MyClass<TCollection> where TCollection : ICollection<???> { // ... public void store(??? obj) { /* put obj into collection */ } // ...

Collections - Items to Remember

I am new to C#.Working in ASP.NET 3.5 ( C# 3.0). What are the collection classes do i need to familiar with inorder to develop effective code ? like IList, and counterpart IList<T>,List,List<T> are enough ? Thank you very much to all ,for giving me a very good guidance. ...

Using yield return to return objects that inherit form a base type

I have a bas class called Media with two classes that inherit from it, Photo and Video. I am trying to create a collection for the media base class to hold those photo and video objects. So I have created a MediaList class as follows: public class MediaList: ICollection<Media> { private readonly XElement _mediaElement; public M...

Reconciling two collections of objects.

I have a form where users can modify a collection of objects using a DataGrid. When the form is opened I create a deep copy of the original collection and if the Cancel button is pressed I just discard that copy. The problem is that when the OK button is pressed I have to reconcile the changes which might be: Modified properties of ex...

Readable way of knowing if there is at least one difference between two Map<Long, Integer> in Java ?

I have two maps of type Map<Long, Integer>, one named "old" representing the old state of an object, and the other named "new" representing the new state of the same object. Is there a simple & readable way of knowing if the old state and the new state are different (ie. if the state has changed)? Ideally, I'd like some "java.utils" or...

Java: cast collection type to subtype

Suppose class B extends class A. I have a List<A> that I happen to know only contains instances of B. Is there a way I can cast the List<A> to a List<B>? It seems my only option is to iterate over the collection, casting one element at time, creating a new collection. This seems like an utter waste of resources given type erasure makes ...

Compare two lists for updates, deletions and additions

Simple question. I have a new list and an old list. In Java is there a standard way/library that allows me to compare these two lists and determine which items have been updated/deleted or are completely new? E.g. I should end up with three lists - Deleted items (items in old but not in new), Updated items (items in both), New items (it...

Java Modcount (ArrayList)

In Eclipse, I see that ArrayList objects have a modCount field. What is its purpose? (number of modifications?) ...

Pre Java 5 collections and the unwillingness to change

We have several areas where code like this can be seen public Map extractData(ResultSet rs) throws SQLException, DataAccessException { Map m = new HashMap(); while(rs.next()){ Jurisdiction j = new Jurisdiction(); m.put("code",rs.getLong(1) ); m.put("type",rs.getSt...