How do I declare a two dimensional array?

You can also create an associative array, or a “hash-table” like array, by specifying the index of the array. $array = array( 0 => array( ‘name’ => ‘John Doe’, ’email’ => ‘john@example.com’ ), 1 => array( ‘name’ => ‘Jane Doe’, ’email’ => ‘jane@example.com’ ), ); Which is equivalent to $array = array(); $array[0] = array(); … Read more

Converting char[] to byte[]

Convert without creating String object: import java.nio.CharBuffer; import java.nio.ByteBuffer; import java.util.Arrays; byte[] toBytes(char[] chars) { CharBuffer charBuffer = CharBuffer.wrap(chars); ByteBuffer byteBuffer = Charset.forName(“UTF-8”).encode(charBuffer); byte[] bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit()); Arrays.fill(byteBuffer.array(), (byte) 0); // clear sensitive data return bytes; } Usage: char[] chars = {‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’}; byte[] bytes … Read more

array_unique for objects?

array_unique works with an array of objects using SORT_REGULAR: class MyClass { public $prop; } $foo = new MyClass(); $foo->prop = ‘test1’; $bar = $foo; $bam = new MyClass(); $bam->prop = ‘test2’; $test = array($foo, $bar, $bam); print_r(array_unique($test, SORT_REGULAR)); Will print: Array ( [0] => MyClass Object ( [prop] => test1 ) [2] => MyClass … Read more

TypeScript typed array usage

You have an error in your syntax here: this._possessions = new Thing[100](); This doesn’t create an “array of things”. To create an array of things, you can simply use the array literal expression: this._possessions = []; Of the array constructor if you want to set the length: this._possessions = new Array(100); I have created a … Read more

List.Contains() is very slow?

If you are just checking for existence, HashSet<T> in .NET 3.5 is your best option – dictionary-like performance, but no key/value pair – just the values: HashSet<int> data = new HashSet<int>(); for (int i = 0; i < 1000000; i++) { data.Add(rand.Next(50000000)); } bool contains = data.Contains(1234567); // etc