Java: convert List to a join()d String

String.join With Java 8 you can do this without any third party library. If you want to join a Collection of Strings you can use the String.join() method: List<String> list = Arrays.asList(“foo”, “bar”, “baz”); String joined = String.join(” and “, list); // “foo and bar and baz” Collectors.joining If you have a Collection with another … Read more

How do I clone a generic list in C#?

If your elements are value types, then you can just do: List<YourType> newList = new List<YourType>(oldList); However, if they are reference types and you want a deep copy (assuming your elements properly implement ICloneable), you could do something like this: List<ICloneable> oldList = new List<ICloneable>(); List<ICloneable> newList = new List<ICloneable>(oldList.Count); oldList.ForEach((item) => { newList.Add((ICloneable)item.Clone()); }); … Read more

Array versus List: When to use which?

It is rare, in reality, that you would want to use an array. Definitely use a List<T> any time you want to add/remove data, since resizing arrays is expensive. If you know the data is fixed length, and you want to micro-optimise for some very specific reason (after benchmarking), then an array may be useful. … Read more

Using LINQ to remove elements from a List

Well, it would be easier to exclude them in the first place: authorsList = authorsList.Where(x => x.FirstName != “Bob”).ToList(); However, that would just change the value of authorsList instead of removing the authors from the previous collection. Alternatively, you can use RemoveAll: authorsList.RemoveAll(x => x.FirstName == “Bob”); If you really need to do it based … Read more

Why is [] faster than list()?

Because [] and {} are literal syntax. Python can create bytecode just to create the list or dictionary objects: >>> import dis >>> dis.dis(compile(‘[]’, ”, ‘eval’)) 1 0 BUILD_LIST 0 3 RETURN_VALUE >>> dis.dis(compile(‘{}’, ”, ‘eval’)) 1 0 BUILD_MAP 0 3 RETURN_VALUE list() and dict() are separate objects. Their names need to be resolved, the … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)