I am trying to convert it in C# and here is my buggy code:
It would be more useful if you described in what way your code is buggy, but I have a somewhat different question:
Why are you copying the items manually in the first place?
If you simply want to copy the items from one collection to another, List<T>
offers a constructor List<T>(IEnumerable<T>)
which does exactly what you want: copies the items from the source collection to the newly created collection.
That replaces your code
List<NameAddress> route = dialog.getRoute();
for ( IEnumerable<NameAddress> e = route.All; e.hasMoreElements(); ) {
// some more to copy route to another List<>
}
with the much shorter
List<NameAddress> newRoute = new List<NameAddress>(dialog.getRoute());
Since MSDN states that the constructor is an O(n) operation, it almost certainly does the same kind of iteration under the hood, but instead of writing it yourself you take advantage of it already having been written as a part of the framework. Copying items from one list to another is a really common operation.
With IEnumerable<T1> dialog.getRoute()
and List<T2>
, the only requirement is that there exists a possible implicit conversion from T1 to T2. You can define your own conversion, if the framework does not understand how to map between the types.