0% found this document useful (0 votes)
22 views8 pages

Lec15.2 - Arrays - Part 2

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)
22 views8 pages

Lec15.2 - Arrays - Part 2

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/ 8

Lecture 15

Arrays - Part 2

By: Asif Shahzad


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

//modifyArray(nums);
//System.out.println(nums[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;
}
Introduction to Multidimensional Arrays
• Java do not allow direct multi-dimensional arrays
like C. In C all elements are placed one after an
other. And pointer arithmetic is done to calculate
the address of an element … using w(in+j).

• In Java, you can make array of arrays. It can serve


the purpose of multi-dimensional arrays. Where
each sub array is a separate array object.

• So, 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
• Multi Dimensional arrays are array of arrays. So 3 indicates the ‘nums’
array is of size 3 (3 rows). And 4 indicates, each element points to an
array of size 4 (4 columns)
• About lengths
• nums length is 3, not 12 (you must understand, why?)
• nums[0] length is 4
Non-Rectangular Multi-Dimensional Arrays
• 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 in main 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.
Multi-Dimensional Arrays – Initialize at
Instantiation Time
• 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}
};
Multi-Dimensional Arrays – Initialize at
Instantiation Time
• 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

You might also like