Chapter#7 - Jagged Arrays and ArrayLists
Chapter#7 - Jagged Arrays and ArrayLists
CSC 220
Data Structures and Algorithms
Lecture # 4
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;
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
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
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Comparing Arrays to Collections 11
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 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.
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Members of ArrayList Class 12
Dr. Asim Abdallah Elshiekh, University of Tabuk, Fac. of Computers & IT Data Structures and Algorithms: Lecture (4)
Members of ArrayList Class 13
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
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:
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 ………….
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:
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:
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:
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)