0% found this document useful (0 votes)
253 views29 pages

Chapter#7 - Jagged Arrays and ArrayLists

The document discusses jagged arrays and ArrayLists. It defines a jagged array as a one-dimensional array whose elements are one-dimensional arrays. It then provides an example of a jagged array. It defines an ArrayList as an array that grows dynamically as more space is needed. It compares key differences between arrays and ArrayLists, such as capacity and member functions. The document outlines members of the ArrayList class, such as Add(), Remove(), and Sort(). It provides an example of declaring and using an ArrayList.

Uploaded by

doha
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)
253 views29 pages

Chapter#7 - Jagged Arrays and ArrayLists

The document discusses jagged arrays and ArrayLists. It defines a jagged array as a one-dimensional array whose elements are one-dimensional arrays. It then provides an example of a jagged array. It defines an ArrayList as an array that grows dynamically as more space is needed. It compares key differences between arrays and ArrayLists, such as capacity and member functions. The document outlines members of the ArrayList class, such as Add(), Remove(), and Sort(). It provides an example of declaring and using an ArrayList.

Uploaded by

doha
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/ 29

1

CSC 220
Data Structures and Algorithms

Lecture # 4

Jagged Arrays and


ArrayLists
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Lecture Outlines 2

Lecture Outlines
• Jagged Arrays
• ArrayLists
 ArrayList and ArrayList Features
 ArrayList vs. Array
 ArrayList Capacity
 Members of ArrayList Class
 Using ArrayList
 ArrayList Examples

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Jagged Arrays 3

Jagged
Arrays
• Jagged Array: is a one-dimensional array whose elements are
one-dimensional arrays.
• Jagged arrays are nothing but arrays of arrays.
• Jagged arrays are fast because each row is treated as 1D
array.
• Declaring Jagged arrays:
 // array of (array of int)
int [][] j2;

 // array of (array of (array of int))


int [][][] j3;

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Jagged Arrays 4

Jagged Arrays
• The (cont.)
following figures illustrate
two jagged arrays
with three rows of different lengths.

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Jagged Arrays 5

Jagged Array -
Example

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Jagged Arrays 6

Jagged Array – Example


(cont.)

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
ArrayLists 7

ArrayLists
• ArrayList: is an array that grows dynamically as more space is
needed. It automatically resizes itself.

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
ArrayList Features 8

ArrayList Features

 An auto-resizing array that can hold any type of


object with many convenient methods.

 Maintains most of the benefits of arrays, such as


fast random access.

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Comparing Arrays to Collections 11

Comparing Arrays to Collections

• An array cannot resize itself when full.


 A collection class, such as ArrayList, can resize.

• An array is intended to store elements of one type.


 A collection is designed to store elements of different types.

• Elements of an array cannot have read-only access.


 A collection can have read-only access property.

• In general, arrays are faster but less flexible.


 Collections are slightly slower but more flexible.
ArrayList vs. Array 9

ArrayList vs.
Array
• Construction:
 String[] names = new String[5];
 ArrayList namesList = new ArrayList();

• Storage:
 names[0] = "Tom";
 namesList.Add("Tom");

• Retrieval:
 String name = names[0];
 String name = namesList[0];

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
ArrayList vs. Array 10

ArrayList vs. Array


• Removal(cont.)
(of element #2):
 for(int i = 2; i < names.Length - 1; i++)
names[i] = names[i+1];
 namesList.RemoveAt(2);

• Search to see if "Marty" is there:


 for(int i = 0; i < names.Length; i++)
if(names[i].Equals("Marty")) { ... }
 if(namesList.Contains("Marty")) { ... }

• Erase all names from the list:


 for(int i = 0; i < names.Length; i++)
names[i] = null;
 namesList.Clear();
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
ArrayList Capacity 11

ArrayList Capacity
 The default capacity of the ArrayList (in .NET 1.0) is 16 items,
however, it becomes 0 in the last version.
 Bydefinition, when the ArrayList is full, it is increased
by a
growth factor of 2.0.
 This means that when the ArrayList is full, its new capacity becomes 32.

 You can specify a different initial capacity when you instantiate


an ArrayList. Here is how:
ArrayList mylist = new ArrayList(100);

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Members of ArrayList Class 12

Members of ArrayList Class


Add() Adds an element to the end of the ArrayList.

AddRange() Adds the elements of a collection to the end of the ArrayList.

Capacity() Returns the number of elements the ArrayList can hold.

Clear() Removes all elements from the ArrayList .

Contains() Determines if a specified item is in the ArrayList.

CopyTo() Copies the ArrayList or a segment of it to an array.

Count() Returns the number of elements currently in the ArrayList.

GetRange() Returns a subset of the ArrayList as an ArrayList.

IndexOf() Returns the index of the first occurance of specified item.

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Members of ArrayList Class 13

Members of ArrayList Class (cont.)


Insert() Inserts an element into the ArrayList at a specified index.

InsertRange() Inserts the elements of a collection into the


ArrayList starting at the specified index.
Item() Gets or sets an element at the specified index.

Remove() Removes the first occurrence of the specified item.

RemoveAt() Removes an element at the specified index.

Reverse() Reverses the order of the elements in the ArrayList.

Sort() Alphabetically sorts the elements in the ArrayList.

ToArray() Copies the elements of the ArrayList to an array.

TrimToSize() Sets the capacity of the ArrayList to the number of items


in the ArrayList.

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Using ArrayList 14

Using
ArrayList
• First thing to be done is to declare the ArrayList:
ArrayList grades = new ArrayList();

• Adding objects:
grades.Add(100);
grades.Add(84);
int position;
position = grades.Add(77);
Console.WriteLine("The 77 was added at

grade position:" +
position);
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
ArrayList Examples 15

ArrayList Examples

• Example (1)
 Add(), Insert(), foreach()
 Capacity(), Count()
 Remove(), RemoveAt(),
 Contains(), IndexOf()

• Example (2)
 InsertRange()
 AddRange()
 GetRange()

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Array Lists - Example (1) 16

Example (1)

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Example (1)  Add() 17

ArrayList grades = new ArrayList();


grades.Add(100);
grades.Add(84); int position;

position = grades.Add(77);
Console.WriteLine("The grade was added at
77 position: " + position);

0 1 2 3 4 5 31
grades 100 84 77 ………….

The Output:
The grade 77 was added at position: 2

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Example (1)  foreach() and Count() 18

0 1 2 3 4 5 31
grades 100 84 77 ………….

int total = 0;
double average = 0.0;
foreach(Object grade in grades)
total += (int)grade;
average = total/grades.Count;
Console.WriteLine("Th
e average grade is: " +
average);
The Output:

The average grade is: 87

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Example (1)  Insert() 19

0 1 2 3 4 5 31
grades 100 84 77 ………….

grades.Insert(1,99);

0 1 2 3 4 5 31
grades 100 99 84 77 ………….

grades.Insert(3,80);

0 1 2 3 4 5 31
grades 100 99 84 80 77 ………….

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Example (1)  Capacity() and Count() 20

0 1 2 3 4 5 31
grades 100 99 84 80 77 ………….

Console.WriteLine("The current capacity of grades is:"


+ grades.Capacity);
Console.WriteLine("The number of grades in grades is:"
+ grades.Count);

The Output:
The current capacity of grades is: 32
The number of grades in grades is: 5

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Example (1)  Contains() and Remove() 21

0 1 2 3 4 5 31
grades 100 99 84 80 77 ………….

if(grades.Contains(54))
grades.Remove(54);
else
Console.Write("Obj
ect not in the
ArrayList.");
The Output:

Object not in the ArrayList.

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Example (1)  Remove() and RemoveAt() 22

0 1 2 3 4 5 31
grades 100 99 84 80 77 ………….

grades.Remove(84);

0 1 2 3 4 5 31
grades 100 99 80 77 ………….

int pos;
pos = grades.IndexOf(99);
grades.RemoveAt(pos);

0 1 2 3 4 5 31
grades 100 80 77 ………….
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Array Lists - Example (2) 23

Example (2)

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Example (2)  Add() 24

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Example (2)  Add() 25

The Output:

The original list of names:


Mike
Beata
Raymond
Bernica
Jennifer

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Example (2)  InsertRange() and AddRange() 26

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Example (2)  InsertRange() and AddRange() 27

The Output:

The new list of names:


David
Michael
Mike
Beata
Raymond
Bernica
Jennifer
Terrill
Donnie
Mayo
Clayton
Alisa

Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Example (2)  GetRange() 28

The Output:

someNames sub-ArrayList:
Mike
Beata
Raymond
Bernica
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)

You might also like