[go: up one dir, main page]

0% found this document useful (0 votes)
18 views15 pages

Lec15.1 - Arrays - Part 1

Uploaded by

Ali Roohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views15 pages

Lec15.1 - Arrays - Part 1

Uploaded by

Ali Roohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Lecture 15

Arrays - Part 1

By: Asif Shahzad


Java Arrays
1. Java arrays are ordered collections of primitive or object references
2. Java arrays are homogeneous – all elements of an array must be of
same type.

3. Three steps to create and use an array:


1. Declaration
2. Instantiation (‘construction’ in simple words)
3. Initialization
Array Declaration
• Declaration tell compiler the array name and the type of its elements
• int nums[];
• double dobs[];
• Student stds[];
• float[][] twoDim; // details late…

• Declaration do not require size of the array. Size is specified at the


time of instantiation i.e. when array object is created using new
keyword … see next slide.
Array Instantiation
• Instantiation is, when the array object is created using new keyword.
• Array size can be specified using literal, constant or variables.
• int[] nums; // declaration
• nums = new int[5]; // instantiation

• Declaration and instantiation – in single line:


• int[] nums = new int[size];
Array Initialization
• When an array is instantiated, its elements are initialized with their
default values. Depending on type of the array.
• You can initialize elements using multiple ways:
1. int[] nums = new int[] { 1, 2, 3, 4 };
2. We can initialize, each element explicitly. Selected or all using loop.
nums = new int[10];
for(int i=0; i<10; i++)
nums[i] = i+i;
Arrays of reference types, same way
• You can create arrays of reference types too:
Student[] stds = new Student[2];
Student s1 = new Student(1, “Ali”);
Student s2 = new Student(2, “Asad”);
Student stds[] = new Student[]{s1, s2};

• You can also create objects in elements list:


Student stds[] = new Student[]{
new Student(1,”Ali”),
new Student(2, “Asad”)
};
Enhanced For Loop
• Syntax of Enhanced for loop.
for(ElementType element : collection-name)
use element

• Assume ‘nums’ is an array of int:


for( int n : nums )
System.out.println( n );
END
Passing Arrays to Methods
• int[] array = { 1, 2, 3};
modifyElement(array[3]);
System.out.println(array[3]);

//modifyArray(array);
//System.out.println(array[2]);

• public static void modifyElement(int element) {


element *= 2;
}
• public static void modifyArray(int[] array2) {
for (int i= 0; i < array2.length; i++)
array2[i] *= 2;
}
Multidimensional Arrays
• Java do not allow direct multi-
dimensional arrays like C. But you can
make array of arrays.
• So in Java, multi-dimension is achieved
using array of arrays. Where each sub
array is a separate object.
• You can create non-rectangular arrays in
Java.
Using Multi-Dimensional Arrays
• Syntax is very simple:
• int[][] nums[][] = new int[3][4];
• We have declared and constructed the array
• MD arrays are array of arrays. So 3 indicates the ‘nums’ array is of size
3 (3 rows). And 4 indicates, each element points to any array of size 4
(4 columns)
• About lengths
• nums length is 3, not 12 (you must understand, why?)
• nums[0] length is 4
Using Multi-Dimensional Arrays
• You can initialized at the time of declaration and construction.
• Array size would be auto determined from the elements given to
initilaize:
int nums[][] = new int[][] {
{1,2,3},
{4,5,6}
};
Using Multi-Dimensional Arrays
Non-Rectangular
• As discussed earlier, multi-dimensional arrays in Java are array of arrays.
• The size of each sub arrays can be different. It means, there can be 4 elements in row 1, 3
elements in row 2, 5 elements in row 3.
• Such arrays can be initialized like this:
• int nums[][] = new int[][] {
{1,2,3},
{4,5,6,7,8,9}
};
• In above example:
• nums size is 2 (as there are two rows)
• nums[0] points to another arrays whose size is 3
• nums[1] points to another array whose size is 6
Using Multi-Dimensional Arrays
Non-Rectangular
• Size of each sub array can be dynamic and these sub arrays can be initialized after
the construction too. For example:
int nums[][] = new int[3][]; /* total 3 rows, we have no info about columns in
each row at the moment.*/
nums[0] = new int[4]; // row 1 has 4 columns
nums[1] = new int[6]; // row 2 has 6 columns
nums[2] = new int[3]; // row 3 has 3 columns

• We have created 3 arrays and stored their reference into the primary array, as its
elements. So ‘nums’ is array of arrays.
• Instead of literal, the sizes in above example can also be variable and taken from
user input. Because arrays would be created at run-time.
int nums[][] = new int[r][c];
where r and c are integers entered by user
Iterating Multi-dimensional Arrays
• The “nums” array created in previous slide, can be iterated like this:
for(int row = 0; row < nums.length; row++){
for(int col=0; col < nums[row].length; col++){
System.out.println( nums[row][col] );
}
}

• nums[x].length above means, the length of sub-array placed at index


x in nums array.

You might also like