Collections
Collections are used to store objects and not primitive types. In this task, the goal is to create a collection, and add an element or two to it.
You are encouraged to solve this task according to the task description, using any language you may know.
Java
ArrayList arrayList = new ArrayList(); arrayList.add(new Integer(0)); /*other features of ArrayList*/ //define the type in the arraylist, you can substitute a proprietary class in the "<>" private ArrayList<int> myarrlist; int i; int sum; myarrlist = new ArrayList<int>(); //add several values to the arraylist to be summed later for(i=0; i<10; i++) { myarrlist.add(i); }
//loop through myarrlist to sum each entry for(i=0; i<myarrlist.size(); i++) { sum+=myarrlist.get(i); }
//remove the last entry in the ArrayList myarrlist.remove(myarrlist.size()-1)
//clear the ArrayList myarrlist.clear();
JavaScript
var array = []; array.push('abc'); array.push(123); array.push(new MyClass); alert( array[2] );
var map = {}; map['foo'] = 'xyz'; //equivalent to: map.foo = 'xyz'; map['bar'] = new MyClass; //equivalent to: map.bar = new MyClass; map['1x; ~~:-b'] = 'text'; //no equivalent alert( map['1x; ~~:-b'] );
Objective-C
Compiler: gcc
The NSMutableArray class in Objective C provides a simple, editable array data structure.
NSMutableArray *array = [[NSMutableArray alloc] init]; [array addObject:@"String1"]; [array addObject:@"String2"]; [array insertObject:@"String3" atIndex:1]; NSLog( @"%@, %@, %@", [array lastObject], [array objectAtIndex:0], [array objectAtIndex:1] ); // prints: String2, String1, String3
Perl
Interpreter: Perl
In perl the value of any variable can be (whats called in perl) a reference to any other simple or complex variable structure, including objects. With hashs (or associative arrays), even the key can be a reference to another variable structure.
my %hash = ( name=>'David', age=>'30' ); my $var = [ 'plain string', \%hash, [3, 2, 1], { City=>'Salt Lake City', State=>'Utah' } ];
$var is a scalar, but its value is an arrayref where the first element is just a string, the second is a hash as defined in %hash, the third is an array, the forth is another hash.
PHP
$students = array(); array_push($students, array('name' => 'Joe Smith', 'age' => 21, height=> '72.5', gpa => 3.42 ));
Python
Interpreter: Python 2.5
In Python practically everything is an object, so using any of the provided structures can function as a collection. http://docs.python.org/tut/node7.html
collection = [0, '1'] # Lists are mutable (editable) and can be sorted in place collection = (0, 1) # Tuples are immutable (not editable) collection = {0: "zero", 1: "one"} # Dictionaries (Hash) collection = set([0, '1']) # sets (Hash)
Ruby
Ruby is a 100% object oriented language, so you can use the default Array or Hash structures as collection objects.
Scala
val l = List(1,2,3,4,-1,-2,-4) l.filter{0<} // get the positive values List(1,2,3,4) l.head // 1 -- first element l.tail // List(2,3,4,-1,-2,-4) -- the rest of the list l.take(3) // List(1,2,3) -- the first 3 elements in the list l.drop(4) // List(-1,-2,-4) -- get rid of the first 4 element in the list 88 :: l.tail // List(88,2,3,4,-1,-2,-4) l.take(2) ::: 18 :: l.takeRight(2) // List(1,2,18,-2,-4)