Hello,
I would like to implement the following code written in java using c#
java:
public void op (List myList) {
....
myList.add(myObjecy);
Collections.sort(myList);
}
The problem when trying to convert this type of code is that the sort
method is not located outside the class, so the only way (the way that
I see) to sort in c# is to declare a concrete class and not an
interface, i.e.
c#:
public void op (List<ObjectmyList) {
....
myList.add(myObjecy);
myList.sort();
}
Is there any solution to this problem?
Thanks,
Efi 6 3235
Efi,
That's pretty much how you have to do it.
As a side note, your op is very inefficient. You are always resorting
the list after you add the item to the end. Instead of adding the item and
sorting it, you should call BinarySearch, and find the index that you should
add the item at, and then add it there.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
<fo****@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hello,
I would like to implement the following code written in java using c#
java:
public void op (List myList) {
...
myList.add(myObjecy);
Collections.sort(myList);
}
The problem when trying to convert this type of code is that the sort
method is not located outside the class, so the only way (the way that
I see) to sort in c# is to declare a concrete class and not an
interface, i.e.
c#:
public void op (List<ObjectmyList) {
...
myList.add(myObjecy);
myList.sort();
}
Is there any solution to this problem?
Thanks,
Efi
Hi,
Thanks, I am just curious, what is reason behind this type of design?
Efi
Nicholas Paldino [.NET/C# MVP] wrote:
Efi,
That's pretty much how you have to do it.
As a side note, your op is very inefficient. You are always resorting
the list after you add the item to the end. Instead of adding the item and
sorting it, you should call BinarySearch, and find the index that you should
add the item at, and then add it there.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
<fo****@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hello,
I would like to implement the following code written in java using c#
java:
public void op (List myList) {
...
myList.add(myObjecy);
Collections.sort(myList);
}
The problem when trying to convert this type of code is that the sort
method is not located outside the class, so the only way (the way that
I see) to sort in c# is to declare a concrete class and not an
interface, i.e.
c#:
public void op (List<ObjectmyList) {
...
myList.add(myObjecy);
myList.sort();
}
Is there any solution to this problem?
Thanks,
Efi
fo****@gmail.com wrote:
Thanks, I am just curious, what is reason behind this type of design?
Sorting is a O(n * log(n)) operation. That is, the time spent sorting is
proportional to the length of the list times the log of the number of
items in the list. Adding at the end is amortized to O(1), BTW.
BinarySearch is a O(log(n)) operation. Inserting into the middle of a
list is a O(n) operation. The dominant term is n, so on the whole it's
O(n).
As you can probably see, the larger n gets, the more expensive adding
and sorting is than inserting.
-- Barry
-- http://barrkel.blogspot.com/
Hi,
My question wasn't about the performance.
Why C# is not using the same design as java i.e. a sort function
outside the concrete class.
Thanks,
Efi
Barry Kelly כתב:
fo****@gmail.com wrote:
Thanks, I am just curious, what is reason behind this type of design?
Sorting is a O(n * log(n)) operation. That is, the time spent sorting is
proportional to the length of the list times the log of the number of
items in the list. Adding at the end is amortized to O(1), BTW.
BinarySearch is a O(log(n)) operation. Inserting into the middle of a
list is a O(n) operation. The dominant term is n, so on the whole it's
O(n).
As you can probably see, the larger n gets, the more expensive adding
and sorting is than inserting.
-- Barry
-- http://barrkel.blogspot.com/ fo****@gmail.com wrote:
Why C# is not using the same design as java i.e. a sort function
outside the concrete class.
I don't know. Here are two possible reasons:
* To make sorting more discoverable - it's easier for beginners to find
instance methods than static methods on different, utility classes.
* To make sorting more efficient, since an instance method can sort the
internal array rather than going through interface dispatch for each
element access.
-- Barry
-- http://barrkel.blogspot.com/
A partial solution is to embrace generics:
public void op <T>(List<TmyList)
{ ...
myList.add(myObjecy);
myList.sort();
} fo****@gmail.com wrote:
c#:
public void op (List<ObjectmyList) {
...
myList.add(myObjecy);
myList.sort();
}
Is there any solution to this problem?
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Softer |
last post by:
Hi
I have been installed Longhorn, alfa version from PDC 2003. I tryed to install Visual Studio 8.0 "Whidbey", version Beta March 2004, under Longhorn and doesen't work. :
I instaled Whidbey under...
|
by: Doug Dew |
last post by:
This won't compile:
using IEnumerable<T> = System.Collections.Generic.IEnumerable<T>;
namespace MyNamespace
{
public class MyClass<T> : IEnumerable<T>
{
// Appropriate stuff here
}
|
by: ljlevend |
last post by:
I've noticed that in VS.NET 2.0 Beta 1 that none of the methods in
System.Collections.Generic.List are overridable. In my app I currently have
over 50 strongly typed ArrayLists that inherit from...
|
by: Mark Rae |
last post by:
Hi,
I'm in the process of updating an ASP.NET v1.1 web app to v2. The app uses
ActiveDirectory a great deal, and I'm trying to use the new
System.Collections.Generic namespace where possible,...
|
by: =?Utf-8?B?TWluIFlvbmc=?= |
last post by:
I'm not sure if I've just lost it but, I no longer see the property SyncRoot
on a Queue. I see that it's a member of the ICollection Base Object, but it's
not exposed.
Was this intended? Besides...
| |
by: Fred Heida |
last post by:
Hi,
i'm trying to (using managed C++) implment the IEnumerable<Tinterface on
my class..
but have a problem with the 2 GetEnumerator method required.... what i have
done is...
...
|
by: Marco Shaw |
last post by:
I've got some C# code to create a custom PowerShell cmdlet with these
statements:
....
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
....
...
|
by: pantagruel |
last post by:
Hi,
This is in a windows service in .NET 1.1 I seem to remember reading
somewhere that in .NET 1.1 Windows Services are limited in what
namespaces they can use - is this so? the only thing that ...
|
by: confused1234 |
last post by:
Hi i'm trying to pass a field of type
System.Collections.Generic.ICollection< FeedImage>
to a function.
I dont't have a clue what this icollection is.
but i tried some code
...
|
by: Fred Mellender |
last post by:
I am trying to use reflection to output the fields (names and values) of an
arbitrary object -- an object dump to a TreeView.
It works pretty well, but I am having trouble with generic lists,...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |