0% found this document useful (0 votes)
0 views

Lecture#04_Arrays

The document provides an overview of arrays in object-oriented programming, specifically in Java, covering one-dimensional and multidimensional arrays, their declaration, initialization, and input methods. It includes examples of how to create and manipulate arrays, as well as alternative declaration syntax and methods to find the length of an array. Additionally, it outlines programming exercises related to matrix manipulation and displaying 2D arrays.

Uploaded by

qmumer7
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Lecture#04_Arrays

The document provides an overview of arrays in object-oriented programming, specifically in Java, covering one-dimensional and multidimensional arrays, their declaration, initialization, and input methods. It includes examples of how to create and manipulate arrays, as well as alternative declaration syntax and methods to find the length of an array. Additionally, it outlines programming exercises related to matrix manipulation and displaying 2D arrays.

Uploaded by

qmumer7
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

OBJECT ORIENTED

PROGRAMMING
ARRAY
S
OUTLINE

◾ Arrays

◾ One-Dimensional Arrays

◾ Multidimensional Arrays

◾ Alternative Array Declaration


Syntax
◾ Finding Length of an Array
ONE-DIMENSIONAL ARRAYS
◾ Obtaining an array in Java is a two-step process:

1. D eclare a variable of the desired array type.


2. Allocate the memory to array using new.
◾ Step 1:
◾ DataType var-name[];
◾ At this point, the value of array is set to null, which represents an
array with no value.
◾ Step 2
◾ array-variable= new DataType[size];
◾We must specify the type and number of elements to allocate.
◾The elements in the array allocated by new will automatically be
initialized to zero
EXAMPLE: ONE-DIMENSIONAL ARRAYS
class Array {
public static void main(String
month_days[5] = 30;
args[]) month_days[6] = 31;
{
month_days[7] = 31;
int month_days[];
month_days = new month_days[8] = 30;
int[12]; month_days[9] = 31;
month_days[0] = 31; month_days[10] = 30;
month_days[1] = 28; month_days[11] = 31;
month_days[2] = 31;
System.out.println("April has " + month_days[3]
month_days[3] = 30; + " days.");
month_days[4] = 31;
}}
ONE-DIMENSIONAL ARRAYS

◾ It is possible to combine the declaration of the array variable with


the allocation
of the array itself

◾ int month_days[] = new int[12];

◾ Arrays may be initialized when they are declared using an array


initializer list

◾ int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
INPUT IN A N ARRAY FRO M KEYBOARD
◾ Java does not provide any direct way to take array input.
◾ But we can take array input by using the method of the
Scanner class.
import java.util.Scanner; //creates an array in the memory
public class ArrayInputExample1 of length 10
{ int[] array = new int[n];
public static void main(String[] args) System.out.println("Enter elements of
{ the arra y: ");
int n; for(int i=0; i<n; i++){
Scanner sc=new Scanner(System.in); //reading array elements
System.out.print("Enter the number from the user
of elements y ou want to store: "); array[i]=sc.nextInt();
//reading the number of elements }
from the that we want to enter System.out.println("Array
n=sc.nextInt(); elements are: ");
// accessing array elements using
the for loop for (int i=0; i<n; i+
+)
{
MULTIDIMENSIONAL ARRAYS

◾ Multidimensional arrays are actually arrays of arrays

◾ Example:

◾ int twoD[][] = new int[4][5];

◾ This allocates a 4 by 5 array and assigns it to twoD. Internally

this matrix is implemented as an array of arrays of int.


A C O N CEPTUAL VIEW OF A 4 BY 5, 2D ARRAY
Right index determines column

[0] [0] [0] [1] [0] [2] [0] [3] [0] [4]

Left index
[1] [0] [1] [1] [1] [2] [1] [3] [1] [4]
determines row

[2] [0] [2] [1] [2] [2] [2] [3] [2] [4]

[3] [0] [3] [1] [3] [2] [3] [3] [3] [4]

int twoD[][] = new int[4][5];


EXAMPLE: MULTIDIMENSIONAL ARRAYS
// Demonstrate a two-dimensional array.
class TwoDArray {
public static void main(String
args[]) { int twoD[][]= new
int[4][5];
int i, j, k = 0; Output
for(i=0; i<4; i+ 01234
+) for(j=0;
j<5; j++) { 56789
twoD[i][j] = 10 11 12 13 14
k; k++;
} 15 16 17 18 19
for(i=0; i<4; i+
+) {
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + "
"); System.out.println();
INPUT IN A 2D ARRAY FRO M
KEYBOARD
import java.util.Scanner; // Read the matrix values
public class ArrayInputExample2 System.out.println("Enter elements of the
{ array: ");
public static void main(String args[]) //loop for row
{ for (i = 0; i < m; i++)
int m, n, i, j; //inner loop for column
Scanner sc=new Scanner(System.in); for (j = 0; j < n; j++)
System.out.print("Enter number of array[i][j] =
rows: " ); sc.nextInt();

//taking row //accessing array elements


m = System.out.println("Elements of array
sc.nextInt(); are: "); for (i = 0; i < m; i++) {
System.out.pri for (j = 0; j < n; j++)
nt("Enter //prints the array elements
number of System.out.print(array[i][j] +
columns: "); " ");
//throws cursor to the next
//taking column line
n = sc.nextInt(); System.out.println();
2D ARRAYS

◾ When we allocate memory for a multidimensional array, we need only to


specify the memory for the first (leftmost) dimension.
◾ W e can allocate the remaining dimensions separately.
int twoD[][] = new int[4]
[ ]; twoD[0] = new
int[5]; twoD[1] = new
int[5]; twoD[2] = new
int[3]; twoD[3] = new
int[5];
2D ARRAYS

◾ When we allocate dimensions manually, we don’t need to allocate the


same number of elements for each column.
◾ Since multidimensional arrays are actually arrays of arrays, the length of
each array is under our control.
◾ We can create a two-dimensional array in which the sizes of the
second dimension are unequal.
EXAMPLE:2D ARRAYS
/ Manually allocate differing size for(i=0; i<4; i++)
second
for(j=0; j<i+1;
dimensions.
j++) { twoD[i]
class TwoDAgain {
[j] = k; k++;
public static void main(String }
args[]) { int twoD[][] = new for(i=0; i<4; i+
int[4][]; twoD[0] = new +) { for(j=0;
j<i+1; j++)
int[1];
System.out.print(twoD[i][j]
twoD[1] = new int[2];
+ " "); System.out.println();
twoD[2] = new int[3]; }
twoD[3] = new int[4]; }
2D ARRAYS

◾ It is possible to initialize multidimensional arrays when we


create them

◾ Example
double m[][] = {

{ 0*0, 1*0, 2*0,


3*0 },
We can use
{ 0*1, 1*1, 2*1, expressions as well
3*1 }, as literals inside array
initializers
{ 0*2, 1*2, 2*2,
3*2 },
}
; { 0*3, 1*3, 2*3,
3*3 }
3 DIMENSIONAL ARRAYS CONCEPTUAL VIEW
EXAMPLE: 3D
ARRAYS
/ Demonstrate a three-dimensional
for(i=0; i<3; i++) {
array.
for(j=0; j<4; j+
class threeDMatrix {
+) { for(k=0;
public static void main(String args[]) Output
k<5; k++)
00 00
{ System.out.print(th 0
00 00
int threeD[][][] = new int[3][4][5]; reeD[i][j][k] + " 0
"); 00 00
0
int i, j, k; System.out.printl
00
00
00
00
0
0
for(i=0; i<3; i++) n(); 01 23
4
for(j=0; j<4; j++) } 02 4 6
80
0 0 00
System.out.println( 032 6
4 9
68
for(k=0; k<5; k++) ); 124
0 8 12
16
threeD [i][j][k]=i*j*k; 06 12 18
ALTERNATIVE ARRAY DECLARATION SYNTA X

◾ There is a second form that may be used to declare an array:


◾ type[] var-name;

◾ The square brackets follow the type specifier, and not the name
of the array variable.
◾ The following two declarations are equivalent:
◾ int a1[] = new int[3];
◾ int[] a1 = new int[3];
FINDING LENGTH OF A N ARRAY
/ This program demonstrates the length array
member. class Length {
public static void main(String args[]) {
int a1[] = new int[10];
int a2[] = {3, 5, 7, 1, 8, 99, 44, -10};
int a3[] = {4, 3, 2, 1};

System.out.println("length of a1 is " +
a1.length); System.out.println("length of
a2 is " + a2.length); Output
}System.out.println("length of a3 is " + length of a1 is 10
length of a2 is 8
} a3.length); length of a3 is 4
PROGRAMMING EXERCISES

◾ Matrix Manipulation
◾ Addition of two matrices

◾ Subtraction of two matrices

◾ Multiplication of two matrices


(Hard)

◾ Displaying 2D Arrays

◾ Column First order

◾ Row first order

You might also like