0% found this document useful (0 votes)
45 views16 pages

Data Structure: Eka, Erick, Reddy

The document compares and contrasts arrays and lists in C#. It finds that lists are generally faster than arrays for updating and selecting elements, since arrays have a fixed size while lists can dynamically resize. However, arrays are faster for inserting elements since lists must first resize their length. The document also outlines when each type of data structure would be preferable to use, such as using an array when the size is known and unlikely to change or a list when dynamic sizing and built-in functions are needed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views16 pages

Data Structure: Eka, Erick, Reddy

The document compares and contrasts arrays and lists in C#. It finds that lists are generally faster than arrays for updating and selecting elements, since arrays have a fixed size while lists can dynamically resize. However, arrays are faster for inserting elements since lists must first resize their length. The document also outlines when each type of data structure would be preferable to use, such as using an array when the size is known and unlikely to change or a list when dynamic sizing and built-in functions are needed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

1

Data
Structure
List
Eka, Erick, Reddy
Sekolah Tinggi Teknik Surabaya

Array
Fixed Size
Initialization:
int[] abc = new int[10];

Accessing by its index


int tmp = abc[0];

2
Sekolah Tinggi Teknik Surabaya

List
Dynamic Size
Initalization
List<int> abc = new List<int>();

Inserting
abc.Add(123);

Accessing by its index


int tmp = abc[0];

3
Sekolah Tinggi Teknik Surabaya

Array Vs List (1)


Size
Array
Fixed Size, but you can use
Array.Resize
List
Dynamic Size

4
Sekolah Tinggi Teknik Surabaya

Array Vs List (2)


Dimension
Array : Support multi dimension
Example :

List : One Dimension


But we can make it multi too

5
Sekolah Tinggi Teknik Surabaya

Array Vs List (3)


Function Built In
Like Sort, Reverse, IndexOf,
Find, etc
Array
Use System.Array function
List
Use its own function

6
Sekolah Tinggi Teknik Surabaya

Performance Test
Prepare the object to test

Performance test in inserting,


updating, and select 1.000.000
element.
Using Stopwatch class from
System.Diagnostics.
7
Sekolah Tinggi Teknik Surabaya

Insert 1.000.000
Element

List is slower than Array when


inserting, because List resize its
length first

8
Sekolah Tinggi Teknik Surabaya

Inserting Script

9
Sekolah Tinggi Teknik Surabaya

Update 1.000.000
Element

When updating its element, list


is much faster than array.

1
0
Sekolah Tinggi Teknik Surabaya

Updating Script

1
1
Sekolah Tinggi Teknik Surabaya

Select 1.000.000
Element

List is 4 times faster than array


when selecting its element.

1
2
Sekolah Tinggi Teknik Surabaya

Selecting Script

1
3
Sekolah Tinggi Teknik Surabaya

When We Use Array


?
Knowing its size, and its size
rarely change
Just do basic thing with its
element

1
4
Sekolah Tinggi Teknik Surabaya

When We Use List ?


Wanting a dynamic array
Need a lot of function built in
Need to remove one or more of its
element
AddRange function

1
5
Sekolah Tinggi Teknik Surabaya

Reference
Arrays Tutorial,
http://msdn.microsoft.com/en-us
/library/aa288453(v=vs.71).
aspx, accessed on April 5th
2014.
C# List, http://
www.dotnetperls.com/list,
accessed on April 5th 2014.
1
7
Sekolah Tinggi Teknik Surabaya

You might also like