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

Java Array

Java arrays allow storing multiple elements of the same type and accessing them using indexes. Arrays have fixed size and store elements contiguously in memory. Single dimensional arrays store elements in one dimension while multidimensional arrays store them in multiple dimensions like rows and columns.

Uploaded by

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

Java Array

Java arrays allow storing multiple elements of the same type and accessing them using indexes. Arrays have fixed size and store elements contiguously in memory. Single dimensional arrays store elements in one dimension while multidimensional arrays store them in multiple dimensions like rows and columns.

Uploaded by

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

ARRAY

Java Arrays
Normally, an array is a collection
of similar type of elements which
has contiguous memory location.

Java array is an object which


contains elements of a similar
data type. Additionally, The
elements of an array are stored in
a contiguous memory location. It
is a data structure where we store
similar elements. We can store
only a fixed set of elements in a
Java array.
Array in Java is index-based, the
first element of the array is stored
at the 0th index, 2nd element is
stored on 1st index and so on.
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.

Types of Array in java


There are two types of array.

Single Dimensional Array


Multidimensional Array
Single Dimensional Array in Java
Syntax to Declare an Array in Java
dataType[] arr; (or)
dataType []arr; (or)
dataType arr[];
Example of Java Array
Let's see the simple example of java array, where we are going to declare,
instantiate, initialize and traverse an array.
//Java Program to illustrate how to declare, instantiate, initialize
//and traverse the Java array.
class Testarray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}
Let us see the example of print the elements of Java array
using the for-each loop.

//Java Program to print the array elements using for-each loop

class Testarray1{
public static void main(String args[]){
int arr[]={33,3,4,5};
//printing array using for-each loop
for(int i:arr)
System.out.println(i);
}}
Change an Array Element
To change the value of a specific element, refer to the index number:
Example:

cars[0] = "Opel";

Example
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
cars[0] = "Opel";
System.out.println(cars[0]); // Now outputs Opel instead of Volvo
Array Length
To find out how many elements an array has, use
the length property:

Example

String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};


System.out.println(cars.length);

// Outputs 4
Multidimensional Array in Java
In such case, data is stored in row and column based index (also known as
matrix form).

Syntax to Declare Multidimensional Array in Java:

dataType[][] arrayRefVar; (or)


dataType [][]arrayRefVar; (or)
dataType arrayRefVar[][]; (or)
dataType []arrayRefVar[];

Example to instantiate Multidimensional Array in Java:

int[][] arr=new int[3][3];//3 row and 3 column


Example of Multidimensional Java Array

Let's see the simple example to declare, instantiate, initialize and print the
2Dimensional array.

//Java Program to illustrate the use of multidimensional array


class Testarray3{
public static void main(String args[]){
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
Let's create a program that takes a single-dimensional array as input.
ArrayInputExample1.java
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: ");
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]);
}
}
}
Let's create a Java program that takes a two-dimensional array as input.
ArrayInputExample2.java

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: ");
//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++)
{ Output
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();
}
}
}

You might also like