View RSS Feed

TcM

Comparison between Data Collections (Arrays etc…)

Rate this Entry
by
TcM
on 02-09-2009 at 10:57 AM (889 Views)
Comparison between Data Collections (Arrays etc…)
In this blog I’m going to explain some data collections such as arrays etc… and I’m going to tell you how to declare them in C# and Java, a typical scenario on when they can be used, and I’m going to explain some characteristics of these collections, such as speed, if they are dynamic or not, if it accepts more than one data type etc. if you would like to go more in depth about arrays, or have some problems, you can read my blog where I explain about Arrays and ArrayLists.
In this blog I will explain briefly the following collections; Array, ArrayList, List<>, HashTables, Dictionary<>, Stack, Queue
Collection: Array
Dynamic: No
Generic: No
Scenario: When we know the data type that we will store in the array, and it will have a fixed size that we know, for example we will only need only 5 inputs.
C#:
Code:
string[] myStringArray = new string[5];
Java:
Code:
String[] myStringArray = new String[5];
Collection: ArrayList
Dynamic: Yes
Generic: No
Scenario: When we need to store mixed data types, because ArrayLists store data as objects, so we can store all the data that we need in different formats in one big ArrayList and when we need a dynamic collection, because we don’t know how many ‘slots’ we will need
C#:
Code:
using System.Collections; 
ArrayList myArrayList = new ArrayList();
Java:
Code:
 import java.util.ArrayList;
ArrayList myArrayList = new ArrayList();
Collection: List<>
Dynamic: Yes
Generic: Yes
Scenario: When we know the data type that we will store in the collection, and when we need a dynamic collection, because we don’t know how many ‘slots’ we will need
C#:
Code:
 List<string> myList = new List<string>();
Java:
Code:
 import java.util.ArrayList;
List<String> myList = new ArrayList<String>();
import java.util.LinkedList;
List<String> myList = new LinkedList<String>();
Collection: HashTable
Dynamic: Yes
Generic: No
Scenario: When we will have different data types that we will store in the collection, and when we need a dynamic collection, because we don’t know how many ‘slots’ we will need
C#:
Code:
 using System.Collections; 
Hashtable myHashTable = new Hashtable();
Java:
Code:
 import java.util.Hashtable;
Hashtable myHashTable = new Hashtable();
Collection: Dictionary<>
Dynamic: Yes
Generic: Yes
Scenario: When we know the data type that we will store in the collection, and when we need a dynamic collection, because we don’t know how many ‘slots’ we will need
C#:
Code:
 Dictionary<int, string> myDictionary = new Dictionary<int, string>();
Java:
Code:
 import java.util.Dictionary;
Dictionary myDictionary = new Dictionary()
Sorry, but I’m not very sure about the Java Code.

Other

Well, there are more Collections, such as the stack and the queue. These two are basically just like an ArrayList, but they have a special property, the queue is just like an everyday normal waiting list / queue because it’s a FIFO, first in, first out and the stack is the opposite LIFO, last in, first out.

Conclusion
The collections that have <> are called generics, in the <> you will have to insert the data type that you will store in the collection, for example integers or strings or whatever. And the dictionary needs to parameters in the <> because one is the key value, just like a primary key in a database, it needs to be unique and it can be used for searching, and the other one is the value data type, meaning the data type that you will store in it.

I hope that this was useful to you. If you see any errors in the point lists please advise me, I will fix it, because when using copy/paste it can get confusing, although I did check them.

Submit "Comparison between Data Collections (Arrays etc…)" to Digg Submit "Comparison between Data Collections (Arrays etc…)" to del.icio.us Submit "Comparison between Data Collections (Arrays etc…)" to StumbleUpon Submit "Comparison between Data Collections (Arrays etc…)" to Google

Tags: None Add / Edit Tags
Categories
Uncategorized

Comments

  1. Turk4n -
    Turk4n's Avatar
    Interesting comparsion between Java and C# !!
    • |
    • permalink
  2. javapride -
    javapride's Avatar
    nice post
    • |
    • permalink

Trackbacks

Total Trackbacks 0
Trackback URL: