Performance and Memory allocation comparison between List and Set

HashSet consumes about 5.5 times more memory than ArrayList for the same number of elements (although they’re both still linear), and has significantly slower iteration (albeit with the same asymptotics); a quick Google search suggests a 2-3x slowdown for HashSet iteration versus ArrayList. If you don’t care about uniqueness or the performance of contains, then … Read more

Android. How do I keep a button displayed as PRESSED until the action created by that button is finished?

I used a function like void setHighlighted(boolean highlight) { button.setBackgroundResource( highlight ? R.drawable.bbg_pressed : R.drawable.button_background); } where button_background is a selector defined in button_backgroung.xml: <?xml version=”1.0″ encoding=”utf-8″?> <selector xmlns:android=”http://schemas.android.com/apk/res/android” > <item android:state_pressed=”true” android:drawable=”@drawable/bbg_pressed”></item> <item android:state_focused=”true” android:drawable=”@drawable/bbg_selected”></item> <item android:drawable=”@drawable/bbg_normal”></item> </selector> That is, this code does not interfere with the pressed state used by the Android framework; … Read more

How to use LINQ to find all combinations of n items from a set of numbers?

Usage: var results = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }.DifferentCombinations(3); Code: public static class Ex { public static IEnumerable<IEnumerable<T>> DifferentCombinations<T>(this IEnumerable<T> elements, int k) { return k == 0 ? new[] { new T[0] } : elements.SelectMany((e, i) => elements.Skip(i + 1).DifferentCombinations(k – 1).Select(c => (new[] {e}).Concat(c))); } }