CSC/ECE 517 Summer 2008/wiki1 6 arraysandhashes: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
Line 13: Line 13:
One way to create an array in Ruby is to to use the new class method:
One way to create an array in Ruby is to to use the new class method:


   planets = Array.new(5)
   planets = Array.new(8)


This creates an empty array named months. This is similar to the Java initialization:
This creates an empty array named months. This is similar to the Java initialization:


   String[] planets = new String[5]
   String[] planets = new String[8]


Arrays can also be initialized to values in both Java and Ruby. For example, we can initialize an arrays of months by entering the following in Java follows, noting the use of curly braces in array initialization:
Arrays can also be initialized to values in both Java and Ruby. For example, we can initialize an arrays of months by entering the following in Java follows, noting the use of curly braces in array initialization:


   String[] planets = {"Mercury", "Venus", "Earth", "Mars" }
   String[] planets = {"Mercury", "Venus", "Earth", "Mars", "Jupiter",
        "Saturn", "Uranus", "Neptune" }


In Ruby, we can perform a similar operation, instead using square brackets:
Some people consider Pluto a planet, but this arguable. In Ruby, we can perform a similar operation, instead using square brackets:


   planets = [ "Mercury", "Venus", "Earth", "Mars" ]
   planets = [ "Mercury", "Venus", "Earth", "Mars", "Jupiter",
        "Saturn", "Uranus", "Neptune" ]


The similarities between Java and Ruby with respect to array creation end here. Ruby provides additional convenient operations on array creation that Java lacks.
The similarities between Java and Ruby with respect to array creation end here. Ruby provides additional convenient operations on array creation that Java lacks.
Line 31: Line 33:
For example, Ruby provides an additional way to define an array of strings using the %w notation. It assumes that all elements are strings, but can save typing:
For example, Ruby provides an additional way to define an array of strings using the %w notation. It assumes that all elements are strings, but can save typing:


   planets = %w[ Mercury Venus Earth Mars ]
   planets = %w[ Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune ]


An array can also be created by allowing the initialization of a default value to each element in the array:
An array can also be created by allowing the initialization of a default value to each element in the array:

Revision as of 22:43, 2 June 2008

Arrays and Hashes

Arrays and hashes are built into Ruby. Arrays, of course, are built into Java too, but hashes are only available through library classes. Compare Ruby and Java arrays, as well as hashes. Write equivalent code sequences in the two languages that illustrate the convenience of programming these constructs in both languages.

Arrays

While Java and Ruby both provide built-in support for arrays, they differ in the operations that can be performed on them. Arrays in both languages are objects. In Java, arrays are defined by the

Comparison of Common Operations in Ruby and Java

Creating an Array

One way to create an array in Ruby is to to use the new class method:

 planets = Array.new(8)

This creates an empty array named months. This is similar to the Java initialization:

 String[] planets = new String[8]

Arrays can also be initialized to values in both Java and Ruby. For example, we can initialize an arrays of months by entering the following in Java follows, noting the use of curly braces in array initialization:

 String[] planets = {"Mercury", "Venus", "Earth", "Mars", "Jupiter", 
        "Saturn", "Uranus", "Neptune" }

Some people consider Pluto a planet, but this arguable. In Ruby, we can perform a similar operation, instead using square brackets:

 planets = [ "Mercury", "Venus", "Earth", "Mars", "Jupiter",
        "Saturn", "Uranus", "Neptune" ]

The similarities between Java and Ruby with respect to array creation end here. Ruby provides additional convenient operations on array creation that Java lacks.

For example, Ruby provides an additional way to define an array of strings using the %w notation. It assumes that all elements are strings, but can save typing:

 planets = %w[ Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune ]

An array can also be created by allowing the initialization of a default value to each element in the array:

 months = Array.new(5, "planets")

Furthermore, since Ruby offers range capabilities, one can initialize an array using ranges:

 digits = Array(1..x)

The above Ruby code creates an array of digits with elements from 1 through 100. The equivalent Java code is far more cumbersome and potentially error-prone, as the programmer must micromanage the details of array indexing:

  for (int j = 1; j <= x; j++)
    digits[j - 1] = j;

Accessing Array Elements

Adding Elements

Removing Elements

Splicing Elements

Sorting Elements

Multidimensional Arrays

Hashes

Back to the assignment page