Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Java Arrays PDF

This document discusses Java arrays. It defines an array as a collection of similar data types, such as storing multiple names as strings. Arrays are declared with a data type and name, and initialized with "new". Elements can be accessed and looped through using indexes starting from 0. Multidimensional arrays contain arrays, such as a 2D array being an array of arrays. Examples demonstrate initializing, accessing, and summing array elements in 1D, 2D and 3D arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Java Arrays PDF

This document discusses Java arrays. It defines an array as a collection of similar data types, such as storing multiple names as strings. Arrays are declared with a data type and name, and initialized with "new". Elements can be accessed and looped through using indexes starting from 0. Multidimensional arrays contain arrays, such as a 2D array being an array of arrays. Examples demonstrate initializing, accessing, and summing array elements in 1D, 2D and 3D arrays.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Java Arrays

DHANUSHKA AKILA SAMARANAYAKE


B.SC. (IN USJP)
S E N I O R Q U A L I T Y E N G I N E E R AT W I L E Y G L O B A L T E C H N O L O G Y P V T LT D
Topics
•What is array
•How to Declare an Array in JAVA
•How to initialize Arrays in JAVA
•How to Access Elements of an Array in Java?
•Looping Through Array Elements
•Java Multidimensional Arrays
What is an Array
▪An array is a collection of similar types of data.
▪For example, if we want to store the names of 100 people then we
can create an array of the string type that can store 100 names

▪String[] array = new String[100];


▪Here, the above array cannot store more than 100 names. The number of values
in a Java array is always fixed.
How to declare an array in Java?
▪In Java, here is how we can declare an array.
▪ datatype [ ] arrayName;
▪ datatype - it can be primitive data types like int, char, double, byte, etc. or Java objects
▪ arrayName - it is an identifier

▪For example,
▪ double[ ] data;
▪ Here, data is an array that can hold values of type double.

▪In Java, we can declare and allocate the memory of an array in one single statement. For
example,
▪ double[] data = new double[10];
How to Initialize Arrays in Java?
▪ In Java, we can initialize arrays during declaration. For example,
//declare and initialize and array
▪ int[] age = {12, 4, 5, 2, 5};
▪ Note that we have not provided the size of the array. In this case, the Java compiler automatically specifies the
size by counting the number of elements in the array (i.e. 5).
▪In the Java array, each memory location is associated with a number. The number is known as an array index. We
can also initialize arrays in Java, using the index number. For example,

// declare an array
▪ int[] age = new int[5];
// initialize array
▪ age[0] = 12;
▪ age[1] = 4;
▪ age[2] = 5;
How to Access Elements of an Array in
Java?
▪Here is the syntax for accessing elements of an array,
// access array elements
▪ array[index]

Note:
◦ Array indices always start from 0. That is, the first element of an array is at index 0.
◦ If the size of an array is n, then the last element of the array will be at index n-1.
Example : Access Array Element using
indexes
Looping Through Array Elements
In Java, we can also loop through each element of the array. For example,
Example: Compute Sum and Average of
Array Elements
Multidimensional Arrays
Multidimensional Arrays
▪In this topic we will learn about the Java multidimensional array using 2-dimensional arrays and
3-dimensional arrays with the help of examples
▪A multidimensional array is an array of arrays.
Example,
int[][] a = new int[3][4];

This is how above


declare 2d array
created in visually
How to initialize a 2d array in Java?
int[][] a = {
{1, 2, 3},
{4, 5, 6, 9},
{7},
};

This is how above


initialize 2d array
created in visually
Accessing the 2D Array Elements
▪Using “.length” attribute to calculate the length of each row
Accessing the 2D Array Elements
Example: Print all elements of 2d array Using Loop
How to initialize a 3d array in Java?
Let's see how we can use a 3d array in Java. We can initialize a 3d array similar to the 2d array.
For example,
▪Basically, a 3d array is an array of 2d arrays. The rows of a 3d array can also vary in length just
like in a 2d array.
Refences
➢Arrays (Java Platform SE 8 ) (oracle.com)
➢Java Arrays (w3schools.com)
➢Java Array – Javatpoint
➢Java Array (With Examples) (programiz.com)
Thank you

You might also like