0% found this document useful (0 votes)
79 views13 pages

Topics: Arrays in Java

The document discusses arrays in Java. It covers: - Arrays can store homogeneous data elements and are fixed-length. - Arrays can be 1D, 2D, or multidimensional with a maximum of 255 dimensions. - Arrays are objects that are automatically initialized to default values. - Syntax for declaring and initializing 1D and 2D arrays is shown along with examples. - Referencing and displaying elements of 1D and 2D arrays using for loops is demonstrated.

Uploaded by

Shubhankar Singh
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)
79 views13 pages

Topics: Arrays in Java

The document discusses arrays in Java. It covers: - Arrays can store homogeneous data elements and are fixed-length. - Arrays can be 1D, 2D, or multidimensional with a maximum of 255 dimensions. - Arrays are objects that are automatically initialized to default values. - Syntax for declaring and initializing 1D and 2D arrays is shown along with examples. - Referencing and displaying elements of 1D and 2D arrays using for loops is demonstrated.

Uploaded by

Shubhankar Singh
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/ 13

Topics

• Arrays in Java

1 Object-Oriented Programming Using Java


Arrays Introduction

• Array  Collection of Homogeneous Data Elements


• Arrays are objects and implements only static (fixed-length) arrays
• Java follows strict bound checking for referencing array elements. If an
attempt is made to reference the elements outside the bounds then
“ArrayIndexOutOfBoundsException” will be thrown at run time.
• Arrays can be 1-dimensional (1-D), 2-dimensional (2-D) or
multidimensional.
• Maximum Dimensions an Array can have = 255
• Arrays are Objects in Java. So, every element at each index is
automatically initialized to some default value depending upon the type
of array. [byte, short, int and long  0, float, double  0.0, booelan 
false, char  ‘’ (whitespace) and any class type array  null]
• <<length>> is the attribute of each array which can be referenced by
<array-reference-name> . <length>

2 Object-Oriented Programming Using Java


1-dimensional Arrays
Syntax : One-Dimensional Arrays :
type[ ] arrayname = new type[Size];
or
type arrayname[ ] = new type[Size];
Examples :
1. int[ ] marks = new int[10]; // Each element is initialized to 0
marks is an int type array, marks.length = 10, LB index =0 , UB index = 9
2. float[ ] values = new float[20]; // Each element is initialized to 0.0
values is an float type array, values.length = 20, LB index =0 , UB index =19
3. double cgpa[ ] = new double[5]; // Each element is initialized to 0.0
cgpa is double type array, cgpa.length = 5, LB index =0 , UB index = 4
4. Box[ ] boxes = new Box[20]; // Each element is initialized to ‘null’ value
boxes is a Box type array, boxes.length = 20, LB index =0 , UB index = 19
5. Point points[ ] = new Point[20]; // Each element is initialized to ‘null’ value
points is a Point type array, points.length = 20, LB index =0 , UB index =19
6. int[] marks = {10, 8, 6, 34, 0, 34};
marks is a int type array, marks.length = 6, LB index = 0, UB index = 5

3 Object-Oriented Programming Using Java


2-dimensional Arrays

Syntax : Two-Dimensional Arrays :


type[ ][ ] arrayname = new type[Row_Size][Col_Size];
or
type arrayname[ ][ ] = new type[Row_Size][Col_Size];
Row index varies from 0 to Row_Size – 1
Column index varies from 0 to Col_Size – 1
Examples :
1. int[ ][ ] data = new int[3][3]; // Each element is initialized to 0
data is 2-D int array, capacity = 9, row index 0 to 2 col index 0 to 2
2. float values[][] = new float[10][4]; // Each element is initialized to 0.0
values is 2-D float array, capacity = 40, row index 0 to 9 col index 0 to 3
3. int table[][] = {{ 0,0,0},{1,1,1}}; // initializes first row to 0 & second to 1

4 Object-Oriented Programming Using Java


2-dimensional Arrays ….

• Java treats a 2-D array as a collection of 1-D arrays.


• In two-dimensional arrays, individual rows can have variable number of
elements <Variable Size Array>
• Example
int[][] data = new int[4][]; // Variable-Size Array, column-size not mentioned
data is a 2-D array having 4 rows (row-index varies from 0 to 3)
data[0] = new int[10]; // Row-0 has 10 elements, column-index varies from 0 to 9
data[1] = new int[5]; // Row-1 has 5 elements, column-index varies from 0 to 4
data[2] = new int[6]; // Row-2 has 6 elements, column-index varies from 0 to 5
data[3] = new int[10]; // Row-3 has 10 elements, column-index varies from 0 to 9

data[1][6]  Results in ArrayIndexOutofBounds Exception

data[2][6]  Results in ArrayIndexOutofBounds Exception

5 Object-Oriented Programming Using Java


Referring Array Elements

• 1-D Arrays, Syntax  array-name[<index>], where <index>


varies from 0 (LB) to array-name.length-1(UB)
• 2-D Arrays, Syntax  array-name[<row-index>][<col-index],
where <row-index> varies from 0 to no-of-rows -1, and <col-
index> varies from 0 to no-of-cols -1.
• Example
1. int[] data = new int[5] ; data[0] = 0; ….., data[4] = 0;
2. boolean[] flags = new boolean[10]; flag[0] = false, ….., flags[9] = false
3. String[] names = new String[5]; names[0]=null, …..., names[4] = null
4. double[] values = {10.5, 5.6, 7.5, 4.5};
values[0] = 10.5, values[1] = 5.6, values[2] = 7.5, values[3] = 4.5
5. int [][] marks = {{10,40,20},{25,56,57},{10,89,94}}
marks[0][0] = 10, marks[0][1] = 40, marks[0][2] = 20,
marks[1][0] = 25, marks[1][1] = 56, marks[1][2] = 57,
marks[2][0] = 10, marks[2][1] = 89, marks[2][2] = 94

6 Object-Oriented Programming Using Java


Displaying 1-D Array Elements

• Two Methods
 Method 1: Using for { .. } loop
Variable is
Example : automatically
int data[] = {10,6,8,9,-4,5}; considered as of
for(int i=0; i<data.length; i++) ‘int’ type The same
variable is to be
System.out.println(data[i]); used inside the loop
 Method 2: Using for each loop

Syntax : for(<type-of-array> <variable> : <array-name>)

Example :
int data[] = {10,6,8,9,-4,5};
for(int i : data)
System.out.println(i);
7 Object-Oriented Programming Using Java
Displaying 1-D Array Elements
: Example 1
// File Name : Demo.java
class Demo
<<OUTPUT>>
{
public static void main(String[] args)
Output by Method 1
{
false
boolean[] flags = new boolean[5];
false
false
// Method-1 : Using for loop
false
System.out.println(“Output by Method 1”);
false
for(int i =0; i < flags.length; i++)
Output by Method 2
System.out.println(flags[i]);
false
false
// Method-2 : Using for each loop
false
System.out.println(“Output by Method 2”);
false
for(boolean k : flags)
false
System.out.println(k);
}// End of Method
}// End of class Demo
8 Object-Oriented Programming Using Java
Displaying 1-D Array Elements
: Example 2
// File Name : Demo.java
class Demo
{
public static void main(String[] args) F:\>java Demo
{ Output by Method 1
String[] names = new String[5]; null
null
// Method-1 : Using for loop null
System.out.println("Output by Method 1"); null
for(int i =0; i < names.length; i++) null
System.out.println(names[i]); Output by Method 2
null
// Method-2 : Using for each loop null
System.out.println("Output by Method 2"); null
for(String k : names) null
System.out.println(k); null
}// End of Method
}// End of class Demo

9 Object-Oriented Programming Using Java


Displaying 2-D Array Elements

• Two Methods
 Method 1: Using nested for { .. } loop
Example :
int data[][] = new int[5][5];
System.out.println(data.length); // Displays 5 not 25
for(int i=0; i<data.length; i++) // data.length = no of rows
for(int j=0; j<data[i].length; j++) // each jth column under ith row
System.out.println(data[i][j]);
 Method 2: Using nested for each loop
Syntax : for(<type-of-array> [ ] <variable-i> : <array-name>)
for(<type-of-array> <variable-j> : <variable-i>

10 Object-Oriented Programming Using Java


Displaying 2-D Array Elements
: Example
// File Name : Demo.java
class Demo
{
public static void main(String[] args)
{
int[][] data = new int[5][5];

System.out.println(data.length); // Displays 5

// Method 1
System.out.println("Method 1 : Using Nested for Loop");
for(int i = 0; i < data.length; i++)
for(int j = 0; j < data[i].length; j++)
System.out.println(data[i][j]);

// Method 2
System.out.println("Method 2 : Using Nested for-each Loop");
for(int[] i : data)
for(int j : i)
System.out.println(j);
}// End of Method
}// End of class Demo

11 Object-Oriented Programming Using Java


Primitive Type vs Object Type
Array
• int[] data = { 10, 20, 30, 7, 34 }; • Box[] boxes = new Box[5];

• String[] names = { “David”, “Ram”, “Mike”};


String[] :String
names[0] David
:String
names[1] Ram
:String
names[2]
Mike
12 Object-Oriented Programming Using Java
Thank You

13 Object-Oriented Programming Using Java

You might also like