[go: up one dir, main page]

0% found this document useful (0 votes)
4 views14 pages

Java Arrays

Uploaded by

laxmaiah.cse
Copyright
© © All Rights Reserved
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% found this document useful (0 votes)
4 views14 pages

Java Arrays

Uploaded by

laxmaiah.cse
Copyright
© © All Rights Reserved
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/ 14

JAVA ARRAYS

AGENDA
 Introduction
 Types of Arrays
 One Dimensional Array
 Multi Dimensional Array
3
ARRAY
An array is a collection of similar
data values with a single name. An
array can also be defined as, a
special type of variable that holds
multiple values of the same data
type at a time.
TYPES OF ARRAYS 4

In java, arrays are objects and they are


created dynamically using new operator.
Every array in java is organized using
index values. The index value of an
array starts with '0' and ends with ‘size-
1'. We use the index value to access
individual elements of an array.
There are two types of arrays :
 One-Dimensional Array
 Multidimensional Array
CREATING AN ARRAY 5

In the java programming language, an array


must be created using new operator and
with a specific size. The size must be an
integer value but not a byte, short, or long.

Syntax :

data_type array_name[ ] = new


data_type[size];
(or)
data_type[ ] array_name = new
data_type[size];
EXAMPLE
6
public class ArrayExample {
public static void main(String[] args) {
int list[] = new int[5];
list[0] = 10;
System.out.println("Value at index 0 - " +
list[0]);
System.out.println("Length of the array - " +
list.length);
}
}
In java, an array can also be
7

initialized at the time of its


declaration. When an array is
initialized at the time of its
declaration, it need not specify the
size of the array and use of the new
operator. Here, the size is
automatically decided based on the
number of values that are
initialized.
ARRAYINDEXOUTOFBOUNDS
EXCEPTION WITH ARRAYS 8

In java, the JVM (Java Virtual Machine) throws


ArrayIndexOutOfBoundsException when an array is
trying to access with an index value of negative
value, value equal to array size, or value more than
the array size.
public class ArrayExample {
public static void main(String[] args) {
short list[] = {10, 20, 30};
list[4] = 10;
System.out.println("Value at index 0 - " + list[0]);
}}
ONE DIMENSIONAL ARRAY 9

In the java programming language, an array


must be created using new operator and with
a specific size. The size must be an integer
value but not a byte, short, or long. We use
the following syntax to create an array.
Syntax:
data_type array_name[ ] = new
data_type[size];
(or)
data_type[ ] array_name = new
data_type[size];
import java.util.Scanner;
public class ArrayExample { 10

public static void main(String[] args) {


Scanner read = new Scanner(System.in);
int size, sum = 0;
System.out.print("Enter the size of the list: ");
size = read.nextInt();
short list[] = new short[size];
System.out.println("Enter any " + size + " numbers: ");
for(int i = 0; i < size; i++) // Simple for statement
list[i] = read.nextShort();
for(int i : list) // for-each statement
sum = sum + i;
System.out.println("Sum of all elements: " + sum);
} }
MULTIDIMENSIONAL ARRAY 11
In java, we can create an array with multiple dimensions.
We can create 2 dimensional, 3-dimensional, or any
dimensional array. In Java, multidimensional arrays are
arrays of arrays. To create a multidimensional array
variable, specify each additional index using another set
of square brackets. We use the following syntax to create
two-dimensional array.
Syntax:

data_type array_name[][]= new data_type[rows][columns];

(or)
data_type[ ][ ] array_name = new data_type[rows][columns];

data_type array_name[ ][ ] = {{value1, value2},


{value3, value4}, {value5, value6},...};
public class Example {
public static void main(String args[]) { 12
int n = 3;
int[][] a = { {5, 2, 3}, {2, 6, 3}, {6, 9, 1} };
int[][] b = { {2, 7, 5}, {1, 4, 3}, {1, 2, 1} };
int[][] c = new int[n][n];
System.out.println("Matrix A:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println("Matrix B:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(b[i][j] + " ");
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++){ 13

for (int k = 0; k < n; k++) {


c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
System.out.println("The product of two matrices is:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
} }
THANK YOU

You might also like