• (nodebb)

    This is clearly Java because C# doesn't have final. Also I doubt that even in Java ToArray does a deep clone of the items.

  • (nodebb)

    I notice that you didn't say what the candidate shined. (Shoes, maybe?) Unless you meant that the candidate shone...

  • LZ79LRU (unregistered) in reply to Mr. TA

    That and nobody ever uses ArrayList in C# any more as it only exists for legacy reasons.

  • DeeKay (unregistered)

    Some of us started coding before there were so many useful helper methods. I still catch myself resorting to "old ways" since I never noticed that some new useful helper was added years after I started working in the language. Guilty as charged.

    I guess that's just me.

  • TheCPUWizard (unregistered)

    Once again, the posti8ng is TRWTF.....

  • Tim R (unregistered)

    This type of code typical of people who are used to relatively low-level languages like C# and Java . It's overly verbose but will still work fine unless performance is critical (which it isn't in 99% of cases).

    Also note that toArray() obviously does not create a deep copy; it's just a shallow copy.

  • Sauron (unregistered)

    .NET is TRWTF.

  • Eluvatar (unregistered) in reply to LZ79LRU

    Also, ArrayList in C# isn't generic

  • Rob (unregistered) in reply to Mr. TA

    Indeed. There are several errors in this article, besides incorrectly assuming it's C# (these would possibly all be true if this were C#). 1) Java's toArray returns a shallow clone. All elements are copied as-is. 2) new ArrayList<Element>() creates an ArrayList with an internal buffer of 10, not 0 (this is documented). 3) The resizing adds 50% if that's enough, not 100% (this is documented). But the end conclusion is valid - new ArrayList<>(elements) would be a good solution.

  • Unregistered (unregistered) in reply to Tim R

    TRWTF is thinking C# and Java are "low-level languages".

  • (nodebb)

    The others are right, this is C#, it's Java. toArray() doesn't create a deep copy, it's a shallow copy. new ArrayList() does not initialize with size zero but with size 10. Size zero wouldn't make any sense, it would have to create a new internal array on the very first addition.

  • (nodebb)

    @DeeKay. Yeah. Lotta that going on out in the field.

    Mr. TA makes a good point. Is this Java or C#? Or failed anonymization? If you subsititute "sealed" for "final" it reads as C#. Which also ties in with @Deekay's point.

    If someone has spent enough years writing both Java and C#, you end up knowing their commonalities in detail, but their differences not so much. e.g. is List<T>.ToArray()a deep or shallow copy? Might be a different answer in Java vs C#. Without looking it up I don't know. So the lesson for the busy coder writing in both is "Don't use .ToArray(); instead use a different idiom where you're sure how it works."

  • Erk (unregistered)

    I'm not sure that I'd expect a simple getter to return a copy and not the actual object being gotten. I guess maybe there was a specification or some sample code that prompted the use of a copy?

    On the other hand, if I get a list I can operate on, I do assume that those operations will be reflected back into the place where I got the list.

    In this case, Java's Collections.unmodifiableList() should have been used. Or the actual list. Depending on what the method is supposed to do.

  • Erk (unregistered)

    Wait, what?!? "return new ArrayList<int>(elements)"??

    That's just wrong. I think you mean to say "return new ArrayList<Element>(elements)"? Though in both Java (and I'm pretty sure C#) you can simply do "return new ArrayList<>(elements)"

  • MaxiTB (unregistered)

    As everyone else stated, this article is wrong. The code shown is Java and has nithing to do with .NET. Maybe its the decade defunct J# but I doubt that.

  • MaxiTB (unregistered) in reply to Tim R

    There is nothing low level about C# or Java, both are managed languages, Java is even mostly interpreted in its history and to thus day on many platform it supports. Low level languages are basically considered languages like C and Assembler, which closely align with the base machine language.

    The correct term for languages like C# and Java is general purpose language BTW.

  • (nodebb) in reply to MaxiTB

    C is not a low level language. It does not closely align with a base machine language, not even PDP-11.

  • (nodebb) in reply to jeremypnet

    No idea what you are talking about, but obviously you haven't coded K&R C in the past and disassembled the output.

  • LZ79LRU (unregistered)

    Personal thought time. C is my least favorite language to work with by far. It occupies that uncomfortable middle ground between languages where it's easy to get most work done and assembly such that it has none of the ease of use and perks of the high level stuff but at the same time none of the charm and nostalgia of messing around in assembly. So when ever I have to use it I always feel that I wish I was using one or the other instead.

  • Hanzito (unregistered)

    About the copying: size doubling isn't efficient, but it's not as bad as it's made out to be. Suppose your array is 8 elements: then you create an array of length 1, then one of length 2 and copy 1 element, then one of length 4 and copy 2 elements, and finally one of length 8 and copy 4 elements. In total, you've copied 7 elements: n - 1. It's linear. And just a shallow copy of values/pointers. A decent GC takes care of the deallocations. Sure, the new line is better, but this won't bring the server down.

  • (nodebb) in reply to Erk

    In this case, Java's Collections.unmodifiableList() should have been used.

    Even better would have been List.copyOf()

  • Duke of New York (unregistered)
    Comment held for moderation.
  • Sou Eu (unregistered) in reply to Mr. TA

    In Java, toArray creates an array and just assigns the elements of the list to that array - no cloning involved.

    In Java, you can pass in the initial capacity to the constructor of ArrayList to avoid allocating and deallocating the internal array as the ArrayList grows. Based on the nature of generics in Java, I'm not sure if passing in an array would make an ArrayList of a single element or an ArrayList with an element for each element of the array.

  • JC (unregistered)

    That's Java not C#... toArray() instantiates a new array, but doesn't do a deep copy.

    Also, empty ArrayLists start at 10 elements and grow with a 1.5x factor.

    Finally: ArrayList has a constructor taking a Collection, that if it's an instance ArrayList it does an optimized path, so the cheapest copy would have been:

    return new ArrayList<>(elements);

    and be done with it.

  • Erk (unregistered) in reply to dkf

    If it should be a copy or a read only view of the list (unmodifiableList) depends on requirements.

    If you're in a high performance situation copying lists if it's not needed will be punishing while you could conceivably cache the unmodifiable list and only have to create it once.

    But, it all comes down to what that list is supposed to do. Is it a copy, a read only representation or is it supposed to work like e.g. a keySet? We don't really know.

  • Craig (unregistered)
    Comment held for moderation.

Leave a comment on “A Simple List Copy”

Log In or post as a guest

Replying to comment #:

« Return to Article