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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 2: Line 2:
Both the Ruby and Java support Arrays as part of the language. Ruby supports Hashtables as part of the language but Java does not. In this article I will compare the use of Arrays and hashtables in Ruby and Java.
Both the Ruby and Java support Arrays as part of the language. Ruby supports Hashtables as part of the language but Java does not. In this article I will compare the use of Arrays and hashtables in Ruby and Java.


== Hashes in Ruby ==
== Arrays and Hashtables ==
 
Arrays and Hashtables are basic data structures that are present in some form in almost all programming languages.
 
Arrays provide a way to store a list of objects and access the object based on it's position in the list. Access to the array elements is very fast if the programmer knows the value of the index where the item is located. Access to the array elements may be slow if the index is not known. This may require the programmer to search a large list of items resulting in poor performance.
 
Hashtables are similar to arrays except the index value is typically a string or other object. Hashtable look up performance is generally  very good. The contents of a hashtable are usually not ordered.


== Arrays in Ruby ==
== Arrays in Ruby ==

Revision as of 18:33, 6 June 2008

Introduction

Both the Ruby and Java support Arrays as part of the language. Ruby supports Hashtables as part of the language but Java does not. In this article I will compare the use of Arrays and hashtables in Ruby and Java.

Arrays and Hashtables

Arrays and Hashtables are basic data structures that are present in some form in almost all programming languages.

Arrays provide a way to store a list of objects and access the object based on it's position in the list. Access to the array elements is very fast if the programmer knows the value of the index where the item is located. Access to the array elements may be slow if the index is not known. This may require the programmer to search a large list of items resulting in poor performance.

Hashtables are similar to arrays except the index value is typically a string or other object. Hashtable look up performance is generally very good. The contents of a hashtable are usually not ordered.

Arrays in Ruby

Creating Arrays in Ruby

Creating an array in Ruby is as simple as

  a = []


Initializing an Array in Ruby

  a = ["one",45,"hello"]


Array Comparison

Array Function Ruby Java
Create h = [] or

h = Array.new

todo
Initialize h = ["one",1,"two"] todo
Element Access h[0] h[0]

Hashtable Comparison

Hashtable Function Ruby Java
Create h = {} or

h = Hash.new

h = new Hashtable();
Initialize h = {"key1" => "value1", "key2" => "value2" } Not Available
Add Key to Existing Hashtable h["key3"] = "value3" h.put("key3",new String("value3"));
Remove a Key - Value pair h.delete("key1") h.remove
Check if Key Exists h.key?("key1") h.containsKey("key1")
Check if Value Exists h.value?("value1") h.contains(new String("value1"));
Retrieve a Value Given a Key h["key1"] h.get("key1")

External Links

Java Hastable Examples