By Ryan on November 30, 2009
Sometimes you need to use an ArrayList. It can be when you’re lazy or when you want flexibility. Eventually, you’ll want to get away from the ArrayList and use your stores objects as standard object array. Doing this isn’t very complicated but it is slightly hidden.
We’re going to pretend for a moment that we have [...]
Posted in Java | Tagged array, arraylist, Java, objects
By Ryan on September 22, 2009
This wasn’t a revelation or anything. My AP Computer Science class was wondering what would happen if one were to set an ArrayList to final. Remember, when you add the final prefix to an variable declaration, it becomes constant and therefore not changeable. (That definition is where we got lost.)
I wrote some sample code that [...]
Posted in Java | Tagged array, change, final, Java, list
By Ryan on July 26, 2009
I needed to use a cookie to store a hash string so a user would be remembered the next time they come to visit a site. I was required to store their user id (numeric) and also a secret string that gets hashed. This typically would have required two cookies but I didn’t want two [...]
Posted in PHP | Tagged array, cookie, json, PHP
By Ryan on July 15, 2009
Mootools doesn’t offer a native way to randomize an array but it does offer a way to get a single random element from an array.
You can extend Mootools to have an Array.randomize method. It’s really easy to do.
Array.implement({
randomize: function() {
return this.sort(function() {return 0.5 – Math.random();});
}
});
Now you can easily call randomize on an array and you’ll [...]
Posted in Mootools | Tagged array, Mootools, randomize
By Ryan on February 11, 2009
There is a function called array_rand but it only returns the key for a random element in an array. So here’s a quick function for you that can do it all in just a couple of lines.
function array_random($array) {
return $array[array_rand($array)];
}
Yes, the word array does appear four times, but that isn’t to confuse you but make [...]
Posted in PHP | Tagged array, element, PHP, random