Arrays and Collections
• Arrays
• Collections
DR. YOUSSEF ELMIR ELMIR@[Link]
Arrays and Collections
2
ARRAYS
Dr. Youssef Elmir (elmir@[Link])
ARRAYS
3
Arrays
An array is a data structure that stores a series of n
values of the same type (primitive or object).
It can be used as a variable, an argument, or a return
type of a method.
Access to each value is done with an integer index.
Let "tab" be an array of integers.
tab[i] is the ith integer of the array.
In Java, i varies from 0 to n-1 (where n is the size of
the array).
Accessing the "n"th index will cause an error!
Dr. Youssef Elmir (elmir@[Link])
ARRAYS
4
Declaration and Initialization
To define an array of integers, we can write:
int[] a; // or int a[];
This statement only declares the variable "a".
The "new" operator creates the array:
a = new int[100];
int a[] = new int[100];
We can also create an array and initialize it simultaneously with its values:
int[] primeNumbers = {2, 3, 5, 7};
It is even possible to initialize an anonymous array:
new int[] {11, 13, 17, 19};
This syntax is used to reinitialize an array without creating a new variable:
• primeNumbers = new int[] {11, 13, 17, 19};
Dr. Youssef Elmir (elmir@[Link])
ARRAYS
5
Array Size and Traversal
You can retrieve the size of an array using `[Link]`.
To fill or traverse an array, we can use a loop:
for (int i = 0; i < [Link]; i++)
[Link](a[i]);
Note:
In Java, it is legal to have an array of size 0, which is
different from null. This is useful in the case of a method that
returns an array as a return type.
Dr. Youssef Elmir (elmir@[Link])
ARRAYS
6
Arrays Copying
What is the result of copying one array variable to
another?
int[] nbr = nbrFirsts;
nbr[2] = 31; // nbrFirsts[2] = 31
nbrFirsts
nbr
Both variables are referring to the same array
Dr. Youssef Elmir (elmir@[Link])
ARRAYS
7
Arrays Copying
To copy all the values of one array into another, you need to use the
arraycopy method of the System class:
[Link](source, sourceIndex, target, targetIndex, length)
Example:
int[] sourceArray = {2, 3, 5, 7};
int[] targetArray = {1, 10, 100, 1000, 1010, 1100};
[Link](sourceArray, 1, targetArray, 2, 3);
for (int i = 0; i < [Link]; i++)
[Link](targetArray[i] + " ");
Result: 1 10 3 5 7 1100
Dr. Youssef Elmir (elmir@[Link])
ARRAYS
8
Multidimensional arrays
Multidimensional arrays are arrays of arrays
They use multiple indices to access elements
Example: suppose we want to manipulate a matrix (a 2-dimensional array)
double[][] mat=new double[3][2];
Or
double[][] mat={{1,6},{5,8},{1.5,7}};
Traversal
for ( int i = 0; i < [Link] ; i++)
for ( int j = 0; j < mat[i]. length ; j++)
[Link] (mat[i][j]+" ");
Dr. Youssef Elmir (elmir@[Link])
ARRAYS
9
Irregular Arrays
An irregular array is a multidimensional array
where the different rows have different
lengths (subarrays of different sizes).
Example: Triangular Matrix
Dr. Youssef Elmir (elmir@[Link])
ARRAYS
10
Irregular Arrays
To create an irregular array (e.g., triangular matrix), we start
by creating the array that contains the rows:
double[][] mat = new double[3][];
Then we create the rows of the triangular matrix:
for (int i = 0; i < [Link]; i++)
mat[i] = new int[i + 1];
Once the array is allocated, we can fill and access its
elements as long as we do not exceed the limits of each
subarray.
Dr. Youssef Elmir (elmir@[Link])
ARRAYS
11
The class Arrays
The [Link] package defines an
"Arrays" class that provides static (class)
utility methods for working with arrays
of objects or primitive types (the
methods are overloaded for all primitive
types).
Dr. Youssef Elmir (elmir@[Link])
ARRAYS
12
Arrays
Role Methods
int binarySearch(char[ ] a) , int binarySearch(int[ ] a) …
Search for an element
int binarySearch(Object[ ] a)
sort(char[ ] a) , sort(int[ ] a) ….. sort(Object[ ] a)
Sort the elements of an array
sort(char[ ] a, int fromIndex, int toIndex) , ...
fill(char[ ] a, char val) ,
Fill the array with a value fill(int[ ] a, long val) …..
fill(char[ ] a, int fromIndex, int toIndex, char val)…;
boolean equals(char[ ] a1, char[ ] a2),
Test the equality of two
boolean equals(int[ ] a1,int[ ] a2),
arrays
equals(Object[ ] a1,Object[ ] a2)…..
Represent an array as a
toString(type[] a)
String
Copy an array copyOf(type[] original, int newLength)
Dr. Youssef Elmir (elmir@[Link])
ARRAYS
13
Sorting an array
To sort an array we use the static sort method of the Arrays
class
Example:
double[] vec = new double[1000];
for ( int i = 0; i < [Link] ; i++)
vec [i] = [Link] ()*1000;
// sort the array
[Link] ( vec );
Dr. Youssef Elmir (elmir@[Link])
ARRAYS
14
Searching in an Array
For searching in a sorted array, you can use the
static binarySearch method of the Arrays class.
It returns the index of the found value v.
Otherwise, it returns a negative value pos,
where (-pos-1) indicates the position at which v
should be inserted.
Dr. Youssef Elmir (elmir@[Link])
ARRAYS
15
Search in a table
Example:
int [] vec = new int [1000];
for ( int i = 0; i < [Link] ; i++)
vec [i] = ( int ) ( [Link] ()*1000);
// search for the value 500
[Link] ( vec );
int pos = [Link] (vec,500);
if (pos >= 0)
[Link] ("position of 500: " +pos);
else
[Link] ("500 is not in the table, you can insert it in box"+ (-pos-1));
Dr. Youssef Elmir (elmir@[Link])
ARRAYS
16
Are there other set structures?
Arrays are elementary data structures
The [Link] package contains plenty of classes for managing more
advanced data structures:
• lists
• sets
• trees
It's about Collections!
Dr. Youssef Elmir (elmir@[Link])