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

From Expertiza_Wiki
Jump to navigation Jump to search
Line 36: Line 36:
|'''Initialize'''
|'''Initialize'''
|<code>h = {"key1" => "value1", "key2" => "value2" }</code>
|<code>h = {"key1" => "value1", "key2" => "value2" }</code>
| TO DO
| Not Available
|-
|-
|'''Add Key to Existing Hashtable'''
|'''Add Key to Existing Hashtable'''
Line 44: Line 44:
|'''Remove a Key - Value pair'''
|'''Remove a Key - Value pair'''
|<code>h.delete("key1")</code>
|<code>h.delete("key1")</code>
|to do
|<code>h.remove</code>
|-
|-
|'''Check if Key Exists'''
|'''Check if Key Exists'''

Revision as of 17:43, 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" } 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