The document provides an overview of handling arrays in Java, including declaration, creation, and initialization of both primitive and object arrays. It explains the syntax for single and multidimensional arrays, as well as compile-time initialization and the use of the length property. Additionally, it covers the steps to create arrays of objects and includes examples for clarity.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
13 views26 pages
6 Handling Arrays in Java
The document provides an overview of handling arrays in Java, including declaration, creation, and initialization of both primitive and object arrays. It explains the syntax for single and multidimensional arrays, as well as compile-time initialization and the use of the length property. Additionally, it covers the steps to create arrays of objects and includes examples for clarity.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26
Handling Arrays in Java
Mr. M.R. Solanki Sr. Lecturer, Information Technology, [email protected] SBMP Learning Outcomes
Students will be able to:
• Declare and create array of primitive types • Declare and create array of primitive types Array of primitive types Array of primitive types An array is a group of like-typed variables that are referred to by a common name.
Step 1: Declaration of an array (create the
reference) Syntax: type var-name[ ]; Example int a[];
Step 2: Creation of an array
Syntax: array-var = new type [size]; Example a = new int[10]; “ # We can combine 2 steps as follows: int a[] = new int[10]; # we can take size from the user : int a[] = new int[size]; #The elements in the array allocated by new will automatically be initialized to zero (for numeric types), false (for boolean), or null (for reference types) Compile time initialization We can initialize arrays at compile time as shown below:
Syntax: type var-name[ val1,val2,val3, … ,
valn]; Example: int a[] = {1,2,3,4,5}; Java provides an in-built length variable for arrays to get the length of an array. Syntax: arr.length System.out.println(a.length); Use: for(i=0;i<a.length;i++) { System.out.println(a[i]); } Array of primitive types Array of primitive types Multidimensional Arrays Declaration of 2-D Array:int twoD[][] = new int[4] [5]; Multidimensional Arrays Multidimensional Arrays Flexible 2-D Array: When you allocate memory for a multidimensional array, you need only specify the memory for the first (leftmost) dimension.
You can allocate the remaining dimensions
separately.
int twoD[][] = new int[4][];
twoD[0] = new int[1]; twoD[1] = new int[2]; twoD[2] = new int[3]; twoD[3] = new int[4]; Multidimensional Arrays Multidimensional Arrays Array of Objects Creating array of objects Step1: Declaring array of objects Syntax: Class_Name ref[] = new Class_Name[size]; Example: Employee e[] = new Employee[10];
Note: Step 1 does not create array of objects
physically, it is just declaration
Step2: Creating array of objects Physically
Syntax: ref[index] = new Class_Name(); Example: e[0] = new Employee(); // employee 0 obj. is created Creating array of objects Creating array of objects Creating array of objects input input input input input Thanks! Any questions? You can find me at: [email protected] Credits Java The Complete Reference, Ninth E dition, Herbert Schildt