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

unit_5_array_in_java

Uploaded by

rchy83194
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)
3 views

unit_5_array_in_java

Uploaded by

rchy83194
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/ 9

Java Arrays

An array is typically a grouping of elements of the same kind that are stored in a single,
contiguous block of memory.

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

o Code Optimization: It makes the code optimized, we can retrieve or sort the data
efficiently.
o Random access: We can get any data located at an index position.

Disadvantages

o Size Limit: Arrays have a fixed size and do not grow dynamically at runtime.

Types of Array in java

There are two types of array.

o Single Dimensional Array


o Multidimensional Array
o 1) Single-Dimensional Array in Java

A single-dimensional array in Java is a linear collection of elements of the same data type. It
is declared and instantiated using the following syntax:

1. dataType arr [];


Instantiation of an Array in Java

1. arrayvariable =new datatype[size];

Example of Java Array

simple example of java array, where we are going to declare, instantiate, initialize and
traverse an array.

Example

//Java Program to illustrate how to declare, instantiate, initialize


//and traverse the Java array.
public class Main{
public static void main(String args[]){
//declaration and instantiation of an array
int a[]=new int[5];
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]);
}
}
}

Declaration, Instantiation and Initialization of Java Array

declare, instantiate, and initialize an array in a single line, as demonstrated below:

1. int a[]={33,3,4,5};//declaration, instantiation and initialization

Example
//Java Program to illustrate the use of declaration, instantiation
//and initialization of Java array in a single line
public class Main{
public static void main(String args[]){

int a[]={33,3,4,5};
for(int i=0;i<a.length;i++) //length is the property of array
System.out.println(a[i]);
}
}
Compile and Run
Output:

33
3
4
5

For-each Loop for Java Array

We can also print the Java array using for-each loop. The Java for-each loop prints the array
elements one by one. It holds an array element in a variable, then executes the body of the
loop.

The syntax of the for-each loop is given below:

for(data_type variable: array){


//body of the loop
}

Example

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


public class Main{
public static void main(String args[]){
//declaration and initialization of an array
int arr[]={33,3,4,5};
//traversal of an array using for-each loop
for(int i:arr)
System.out.println(i);
}
}
Compile and Run
Wap to enter 10 elements into array and sort them ascending or descending order.
package sorting;
import java.util.Scanner;
public class Sorting {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int a[] = new int[10];


int i,j,temp;

System.out.println("Enter the elements of the array:");


for ( i = 0; i < 10; i++) {
a[i] = sc.nextInt();
}

for ( i = 0; i < 10 ; i++) {


for ( j = i+1 ; j < 10; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

System.out.println("Sorted Array:");
for ( i = 0; i < 10; i++) {
System.out.print(a[i] + " ");

}
}
}

Wap to enter 10 elements into array and find sum of them


package sorting;
import java.util.Scanner;
public class Sorting {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int a[] = new int[10];


int i,j;
int s=0;
System.out.println("Enter the elements of the array:");
for ( i = 0; i < 10; i++) {
a[i] = sc.nextInt();
}

for ( i = 0; i < 10 ; i++) {


s=s+a[i];
}
System.out.println(s +"");
}
}

2) Multidimensional Array in Java

A multidimensional array in Java is an array of arrays where each element can be an array
itself. It is useful for storing data in row and column format.

Syntax to Declare Multidimensional Array in Java

1. dataType array[][];

Example to instantiate Multidimensional Array in Java

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


Example to initialize Multidimensional Array in Java

arr[0][0]=1;
arr[0][1]=2;
arr[0][2]=3;
arr[1][0]=4;
arr[1][1]=5;
arr[1][2]=6;
arr[2][0]=7;
arr[2][1]=8;
arr[2][2]=9;
Example of Multidimensional Java Array

simple example to declare, instantiate, initialize and print the 2Dimensional array.

Example

public class Main {


public static void main(String args[]) {
int arr[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; // 3x3 matrix
// Printing the 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();
}
}
}
Compile and Run
Compile and RunArrayAddition.java
//Java Program to demonstrate the addition of two matrices in Java
class matrixadd{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,3,4},{3,4,5}};
int b[][]={{1,3,4},{3,4,5}};

//creating another matrix to store the sum of two matrices


int c[][]=new int[2][3];

//adding and printing addition of 2 matrices


for(int i=0;i<2;i++){
for(int j=0;j<3;j++){
c[i][j]=a[i][j]+b[i][j];
System.out.print(c[i][j]+" ");
}
System.out.println(); //new line
}
}}
Multiplication of Two Matrices in Java

MatrixMultiplicationExample.java

//Write a program to assign two matrix elements and calculate matrix multiplication.
public class Matrixmult{
public static void main(String args[])
{
int a[][]={{1,1,1},{2,2,2},{3,3,3}};
int b[][]={{1,1,1},{2,2,2},{3,3,3}};

//creating another matrix to store the multiplication of two matrices


int c[][]=new int[3][3];

//multiplying and printing multiplication of 2 matrices


for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
c[i][j]=0;
for(int k=0;k<3;k++)
{
c[i][j]+=a[i][k]*b[k][j];
}
System.out.print(c[i][j]+" "); //printing matrix element
}
System.out.println();//new line
}
}}
o Wap to enter 2x2 two matrix and calculate addition of matrix using scanner class in java
package matrixaddition;
import java.util.Scanner;

public class Matrixaddition {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

int a[][] = new int[2][2];


int b[][]= new int[2][2];
int c[][] = new int[2][2];
int i,j;

System.out.println("Enter elements of first matrix:");


for ( i = 0; i < 2; i++) {
for(j=0;j<2;j++)
{
a[i][j] = sc.nextInt();
}}

System.out.println("Enter e elements of second matrix:");


for ( i = 0; i < 2; i++) {
for(j=0;j<2;j++)
{
b[i][j] = sc.nextInt();
}}

// matric addition
for ( i = 0; i < 2; i++) {
for(j=0;j<2;j++)
{

c[i][j] = a[i][j]+b[i][j];
}}
System.out.println("Addition of matrix:");
for ( i = 0; i < 2; i++) {
for(j=0;j<2;j++)
{
System.out.println(c[i][j]+"");

}}}}
WAP to enter 3x3 two matrixes and calculate matrix multiplication using scanner class.
package matrixaddition;
import java.util.Scanner;

public class Matrixaddition {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

int a[][] = new int[3][3];


int b[][]=new int[3][3];
int c[][] = new int[3][3];
int i,j,k;

System.out.println("Enter the elements of the array:");


for ( i = 0; i < 3; i++) {
for(j=0;j<3;j++)
{

a[i][j] = sc.nextInt();
}}

System.out.println("Enter e elements of the array:");


for ( i = 0; i < 3; i++) {
for(j=0;j<3;j++)
{
b[i][j] = sc.nextInt();
}}
// matric addition
for ( i = 0; i < 3; i++) {
for(j=0;j<3;j++)
{ c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j] = c[i][j]+a[i][k]*b[k][j];
}}}
System.out.println("Addition of matrix:");
for ( i = 0; i < 3; i++) {
for(j=0;j<3;j++)
{
System.out.println(c[i][j]+"");
}
System.out.println();
}}}

You might also like