Amity School of Engineering & Technology
Array
Amity School of Engineering & Technology
• 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.
Amity School of Engineering & Technology
Advantages
• Code Optimization: It makes the code optimized, we
can retrieve or sort the data efficiently.
• Random access: We can get any data located at an
index position.
Disadvantages
• Size Limit: We can store only the fixed size of elements
in the array.
• It doesn't grow its size at runtime.
• To solve this problem, collection framework is used in
Java which grows automatically.
Amity School of Engineering & Technology
How to declare an array in Java?
Syntax: 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;
But, how many elements can array this hold?
Amity School of Engineering & Technology
• To define the number of elements that an array can hold,
we have to allocate memory for the array in Java.
• For example,
// declare an array
double[] data;
// allocate memory
data = new double[10];
Amity School of Engineering & Technology
• In Java, we can declare and allocate the
memory of an array in one single
statement. For example,
double[] data = new double[10];
Amity School of Engineering & Technology
Initialize Arrays in Java
we can initialize arrays during declaration. For example,
//declare and initialize and array
int[] age = {12, 4, 5, 2, 5};
Amity School of Engineering & Technology
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;
..
Amity School of Engineering & Technology
Access Elements of an Array in Java
We can access the element of an array using the index
number.
// access array elements
array[index]
Amity School of Engineering & Technology
class Main {
public static void main(String[] args) {
// create an array
int[] age = {12, 4, 5, 2, 5};
// access each array elements
System.out.println("Accessing Elements of Array:");
System.out.println("First Element: " + age[0]);
System.out.println("Second Element: " + age[1]);
System.out.println("Third Element: " + age[2]);
System.out.println("Fourth Element: " + age[3]);
System.out.println("Fifth Element: " + age[4]);
}
}
Amity School of Engineering & Technology
Looping Through Array Elements
class Main {
public static void main(String[] args) {
// create an array
int[] age = {12, 4, 5};
// loop through the array using for loop
System.out.println("Using for Loop:");
for(int i = 0; i < age.length; i++) {
System.out.println(age[i]);
}
}
}
Amity School of Engineering & Technology
Using the for-each Loop
class Main {
public static void main(String[] args) {
// create an array
int[] age = {12, 4, 5};
// loop through the array using for loop
System.out.println("Using for-each Loop:");
for(int a : age) {
System.out.println(a);
}
}
}
Amity School of Engineering & Technology
Taking array through user
• 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.
• To take input of an array, we must ask the user about the
length of the array.
Amity School of Engineering & Technology
One-dimensional array input
import java.util.Scanner;
public class ArrayInputExample1
{
public static void main(String[] args)
{
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of elements you want to store: ");
//reading the number of elements from the that we want to enter
n=sc.nextInt();
//creates an array in the memory of length 10
int[] array = new int[10];
System.out.println("Enter the elements of the array: ");
Amity School of Engineering & Technology
for(int i=0; i<n; i++)
{
//reading array elements from the user
array[i]=sc.nextInt();
}
System.out.println("Array elements are: ");
// accessing array elements using the for loop
for (int i=0; i<n; i++)
{
System.out.println(array[i]);
}
}
}
Amity School of Engineering & Technology
Java Multidimensional Arrays
• A multidimensional array is an array of arrays.
• Each element of a multidimensional array is an array
itself.
For example,
int[][] a = new int[3][4];
Amity School of Engineering & Technology
How to initialize a 2d array in Java?
Here is how we can initialize a 2-dimensional array in Java.
int[][] a = {
{1, 2, 3},
{4, 5, 6, 9},
{7},
};
Amity School of Engineering & Technology
Example: 2-dimensional Array
class MultidimensionalArray {
public static void main(String[] args) {
// create a 2d array
int[][] a = {
{1, 2, 3},
{4, 5, 6, 9},
{7},
};
// calculate the length of each row
System.out.println("Length of row 1: " + a[0].length);
System.out.println("Length of row 2: " + a[1].length);
System.out.println("Length of row 3: " + a[2].length);
}
}
Amity School of Engineering & Technology
Printing all elements of 2d array Using Loop
class MultidimensionalArray {
public static void main(String[] args) {
int[][] a = {
{1, -2, 3},
{-4, -5, 6, 9},
{7},
};
for (int i = 0; i < a.length; ++i) {
for(int j = 0; j < a[i].length; ++j) {
System.out.println(a[i][j]);
}
}
}
}
Amity School of Engineering & Technology
for...each loop to access elements
class MultidimensionalArray {
public static void main(String[] args) {
// create a 2d array
int[][] a = {
{1, -2, 3},
{-4, -5, 6, 9},
{7},
};
// first for...each loop access the individual array inside the 2d array
for (int[] innerArray: a) {
// second for...each loop access each element inside the row
for(int data: innerArray) {
System.out.println(data);
}
}
}
}
Amity School of Engineering & Technology
Two-dimensional array input
• A two-dimensional array is an array that contains elements in the
form of rows and columns.
• It means we need both row and column to populate a two-
dimensional array. Matrix is the best example of a 2D array.
• We can declare a two-dimensional array by using the following
statement.
datatype arrayName[][] = new datatype[m][n];
Amity School of Engineering & Technology
import java.util.Scanner;
public class ArrayInputExample2
{
public static void main(String args[])
{
int m, n, i, j;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number of rows: ");
//taking row as input
m = sc.nextInt();
System.out.print("Enter the number of columns: ");
//taking column as input
n = sc.nextInt();
// Declaring the two-dimensional matrix
int array[][] = new int[m][n];
// Read the matrix values
System.out.println("Enter the elements of the array: ");
Amity School of Engineering & Technology
//loop for row
for (i = 0; i < m; i++)
//inner for loop for column
for (j = 0; j < n; j++)
array[i][j] = sc.nextInt();
//accessing array elements
System.out.println("Elements of the array are: ");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
//prints the array elements
System.out.print(array[i][j] + " ");
//throws the cursor to the next line
System.out.println();
}
}
Amity School of Engineering & Technology
String Array
import java.util.Scanner;
public class StringArrayInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of the array");
int n = sc.nextInt();
String[] arr = new String[n];
System.out.println("Enter the elements of the array");
for (int i = 0; i < n; i++) {
arr[i] = sc.next();
}
System.out.println("The elements of the array are");
for (int i = 0; i < n; i++) {
System.out.println(arr[i]);
}
}
}
Amity School of Engineering & Technology
An Array Of Characters From User Input
import java.util.Scanner;
public class TakeCharArrayInputfor{
public static void main(String args[]){
//scanner class to read input from the user
Scanner sc=new Scanner(System.in);
//Declaring and creating character array
char[] arr=new char[5];
//initializing value to the array
System.out.println("******Initializing array******");
System.out.println("Enter character values");
arr=sc.next().toCharArray();
//Take character input
//displaying the array elements
System.out.println("n******displaying array of characters******");
System.out.println("Entered Characters are");
for(int i=0; i<arr.length; i++){
System.out.print(arr[i]+"t");
}
}
}
Amity School of Engineering & Technology
Thanks

SessionPlans_f3efa1.6 Array in java.pptx

  • 1.
    Amity School ofEngineering & Technology Array
  • 2.
    Amity School ofEngineering & Technology • 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.
  • 3.
    Amity School ofEngineering & Technology Advantages • Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently. • Random access: We can get any data located at an index position. Disadvantages • Size Limit: We can store only the fixed size of elements in the array. • It doesn't grow its size at runtime. • To solve this problem, collection framework is used in Java which grows automatically.
  • 4.
    Amity School ofEngineering & Technology How to declare an array in Java? Syntax: 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; But, how many elements can array this hold?
  • 5.
    Amity School ofEngineering & Technology • To define the number of elements that an array can hold, we have to allocate memory for the array in Java. • For example, // declare an array double[] data; // allocate memory data = new double[10];
  • 6.
    Amity School ofEngineering & Technology • In Java, we can declare and allocate the memory of an array in one single statement. For example, double[] data = new double[10];
  • 7.
    Amity School ofEngineering & Technology Initialize Arrays in Java we can initialize arrays during declaration. For example, //declare and initialize and array int[] age = {12, 4, 5, 2, 5};
  • 8.
    Amity School ofEngineering & Technology 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; ..
  • 9.
    Amity School ofEngineering & Technology Access Elements of an Array in Java We can access the element of an array using the index number. // access array elements array[index]
  • 10.
    Amity School ofEngineering & Technology class Main { public static void main(String[] args) { // create an array int[] age = {12, 4, 5, 2, 5}; // access each array elements System.out.println("Accessing Elements of Array:"); System.out.println("First Element: " + age[0]); System.out.println("Second Element: " + age[1]); System.out.println("Third Element: " + age[2]); System.out.println("Fourth Element: " + age[3]); System.out.println("Fifth Element: " + age[4]); } }
  • 11.
    Amity School ofEngineering & Technology Looping Through Array Elements class Main { public static void main(String[] args) { // create an array int[] age = {12, 4, 5}; // loop through the array using for loop System.out.println("Using for Loop:"); for(int i = 0; i < age.length; i++) { System.out.println(age[i]); } } }
  • 12.
    Amity School ofEngineering & Technology Using the for-each Loop class Main { public static void main(String[] args) { // create an array int[] age = {12, 4, 5}; // loop through the array using for loop System.out.println("Using for-each Loop:"); for(int a : age) { System.out.println(a); } } }
  • 13.
    Amity School ofEngineering & Technology Taking array through user • 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. • To take input of an array, we must ask the user about the length of the array.
  • 14.
    Amity School ofEngineering & Technology One-dimensional array input import java.util.Scanner; public class ArrayInputExample1 { public static void main(String[] args) { int n; Scanner sc=new Scanner(System.in); System.out.print("Enter the number of elements you want to store: "); //reading the number of elements from the that we want to enter n=sc.nextInt(); //creates an array in the memory of length 10 int[] array = new int[10]; System.out.println("Enter the elements of the array: ");
  • 15.
    Amity School ofEngineering & Technology for(int i=0; i<n; i++) { //reading array elements from the user array[i]=sc.nextInt(); } System.out.println("Array elements are: "); // accessing array elements using the for loop for (int i=0; i<n; i++) { System.out.println(array[i]); } } }
  • 16.
    Amity School ofEngineering & Technology Java Multidimensional Arrays • A multidimensional array is an array of arrays. • Each element of a multidimensional array is an array itself. For example, int[][] a = new int[3][4];
  • 17.
    Amity School ofEngineering & Technology How to initialize a 2d array in Java? Here is how we can initialize a 2-dimensional array in Java. int[][] a = { {1, 2, 3}, {4, 5, 6, 9}, {7}, };
  • 18.
    Amity School ofEngineering & Technology Example: 2-dimensional Array class MultidimensionalArray { public static void main(String[] args) { // create a 2d array int[][] a = { {1, 2, 3}, {4, 5, 6, 9}, {7}, }; // calculate the length of each row System.out.println("Length of row 1: " + a[0].length); System.out.println("Length of row 2: " + a[1].length); System.out.println("Length of row 3: " + a[2].length); } }
  • 19.
    Amity School ofEngineering & Technology Printing all elements of 2d array Using Loop class MultidimensionalArray { public static void main(String[] args) { int[][] a = { {1, -2, 3}, {-4, -5, 6, 9}, {7}, }; for (int i = 0; i < a.length; ++i) { for(int j = 0; j < a[i].length; ++j) { System.out.println(a[i][j]); } } } }
  • 20.
    Amity School ofEngineering & Technology for...each loop to access elements class MultidimensionalArray { public static void main(String[] args) { // create a 2d array int[][] a = { {1, -2, 3}, {-4, -5, 6, 9}, {7}, }; // first for...each loop access the individual array inside the 2d array for (int[] innerArray: a) { // second for...each loop access each element inside the row for(int data: innerArray) { System.out.println(data); } } } }
  • 21.
    Amity School ofEngineering & Technology Two-dimensional array input • A two-dimensional array is an array that contains elements in the form of rows and columns. • It means we need both row and column to populate a two- dimensional array. Matrix is the best example of a 2D array. • We can declare a two-dimensional array by using the following statement. datatype arrayName[][] = new datatype[m][n];
  • 22.
    Amity School ofEngineering & Technology import java.util.Scanner; public class ArrayInputExample2 { public static void main(String args[]) { int m, n, i, j; Scanner sc=new Scanner(System.in); System.out.print("Enter the number of rows: "); //taking row as input m = sc.nextInt(); System.out.print("Enter the number of columns: "); //taking column as input n = sc.nextInt(); // Declaring the two-dimensional matrix int array[][] = new int[m][n]; // Read the matrix values System.out.println("Enter the elements of the array: ");
  • 23.
    Amity School ofEngineering & Technology //loop for row for (i = 0; i < m; i++) //inner for loop for column for (j = 0; j < n; j++) array[i][j] = sc.nextInt(); //accessing array elements System.out.println("Elements of the array are: "); for (i = 0; i < m; i++) { for (j = 0; j < n; j++) //prints the array elements System.out.print(array[i][j] + " "); //throws the cursor to the next line System.out.println(); } }
  • 24.
    Amity School ofEngineering & Technology String Array import java.util.Scanner; public class StringArrayInput { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter the size of the array"); int n = sc.nextInt(); String[] arr = new String[n]; System.out.println("Enter the elements of the array"); for (int i = 0; i < n; i++) { arr[i] = sc.next(); } System.out.println("The elements of the array are"); for (int i = 0; i < n; i++) { System.out.println(arr[i]); } } }
  • 25.
    Amity School ofEngineering & Technology An Array Of Characters From User Input import java.util.Scanner; public class TakeCharArrayInputfor{ public static void main(String args[]){ //scanner class to read input from the user Scanner sc=new Scanner(System.in); //Declaring and creating character array char[] arr=new char[5]; //initializing value to the array System.out.println("******Initializing array******"); System.out.println("Enter character values"); arr=sc.next().toCharArray(); //Take character input //displaying the array elements System.out.println("n******displaying array of characters******"); System.out.println("Entered Characters are"); for(int i=0; i<arr.length; i++){ System.out.print(arr[i]+"t"); } } }
  • 26.
    Amity School ofEngineering & Technology Thanks

Editor's Notes

  • #7 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).
  • #8 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.
  • #11  arrays are objects in Java, we can find their length using the object property length.
  • #12 For each loop can not be used to take input by user.
  • #16 Remember, Java uses zero-based indexing, that is, indexing of arrays in Java starts with 0 and not 1.
  • #18 Output: Length of row 1: 3 Length of row 2: 4 Length of row 3: 1
  • #19 for (int i = 0; i < a.length; ++i) { for(int j = 0; j < a[i].length; ++j) { System.out.print(a[i][j]); System.out.print(" "); } System.out.println(" ");
  • #24 Enter the size of the array 3 Enter the elements of the array ram shyam geeta The elements of the array are ram shyam geeta
  • #25 ******Initializing array****** Enter character values ASDFGH ******displaying array of characters****** Entered characters are A     S     D     F     G     H