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

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 32: Line 32:
|<code>h = {}</code> or  
|<code>h = {}</code> or  
<code>h = Hash.new</code>
<code>h = Hash.new</code>
|<code> TO DO </code>
|<code>h = new Hashtable();</code>
|-
|-
|'''Initialize'''
|'''Initialize'''

Revision as of 17:27, 6 June 2008

Introduction

Both Ruby and Java support arrays and hash tables

Hashes in Ruby

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"]


Hashes in Java

Arrays in Java

Hashtable Function Ruby Java
Create h = {} or

h = Hash.new

h = new Hashtable();
Initialize h = {"key1" => "value1", "key2" => "value2" } TO DO
Add Key to Existing Hashtable h["key3"] = "value3" To Do
Remove a Key - Value pair h.delete("key1") to do
Check if Key Exists h.key?("key1") to do
Check if Value Exists h.value?("value1") to do
Retrieve a Value Given a Key h["key1"] to do

External Links

Java Hastable Examples