0% found this document useful (0 votes)
44 views

Arrays

Arrays allow us to store multiple values of the same type in a single variable. In Java, arrays are objects that hold a fixed number of values of a single type. There are two types of arrays: single dimensional arrays, which hold values in one dimension, and multidimensional arrays like 2D and 3D arrays which hold values across multiple dimensions. Arrays provide benefits like random access and code optimization but are limited in size. The Collections framework can be used when a dynamically sized data structure is needed.

Uploaded by

REAL world
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Arrays

Arrays allow us to store multiple values of the same type in a single variable. In Java, arrays are objects that hold a fixed number of values of a single type. There are two types of arrays: single dimensional arrays, which hold values in one dimension, and multidimensional arrays like 2D and 3D arrays which hold values across multiple dimensions. Arrays provide benefits like random access and code optimization but are limited in size. The Collections framework can be used when a dynamically sized data structure is needed.

Uploaded by

REAL world
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

PROGRAMMING IN JAVA

1
ARRAY
 Array is a collection of similar type of elements
that have contiguous memory location.
 In java, array is an object the contains elements
of similar data type.
 It is a data structure where we store similar
elements. We can store only fixed elements in an
array.
 Array is index based, first element of the array is
stored at 0 index.

2
Advantage of Array
Code Optimization: It makes the code optimized, we
can retrieve or sort the data easily.
Random access: We can get any data located at any
index position.
Disadvantage of Array
Size Limit: We can store only 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.

3
Types of Array: There are two types of array.
 Single Dimensional Array
 Multidimensional Array-
 2D array
 3D array

 Jagged array

4
SINGLE DIMENSIONAL ARRAY
 The syntax for declaring and instantiating an
array:
There are two ways to declare an array,
type[] arrayName;
type arrayName[];

5
 How to instantiate an array
arrayName = new type[length];
 How to declare and instantiate an array in one
statement
type[] arrayName = new type[length];

6
EXAMPLES
 Array of integers
int[] num = new int[5];
 Array of Strings

String[] nameList = new String[5];


nameList[0] = "Amanda Green";
nameList[1] = "Vijay Arora";
nameList[2] = "Sheila Mann";
nameList[3] = "Rohit Sharma";
nameList[4] = "Mandy Johnson";
7
ARRAY LENGTH

 The syntax for getting the length of an array


arrayName.length
e.g-
int[] values = new int[10];
for (int i = 0; i < values.length; i++)
{
values[i] = i;
}

8
THE ARRAYS CLASS

 Arrays.sort
e.g-
int[] numbers = {2,6,4,1,8,5,9,3,7,0};
Arrays.sort(numbers);
for (int num : numbers)
{
System.out.print(num + " ");
}

9
SOME OTHER METHODS:
 equals( arrayName1, arrayName2 )
 copyOf( arrayName, length )

 binarySearch( arrayName, value )

10
TWO-DIMENSIONAL ARRAYS

The syntax for creating a rectangular array-


type[][] arrayName = new
type[rowCount][columnCount];

 A statement that creates a 3x2 array


int[][] numbers = new int[3][2];
 3x2 array and initializes it in one statement

int[][] numbers = { { 1, 2 }, { 3, 4 }, { 5, 6 } };

11
JAGGED ARRAY
type[][] arrayName = new type[rowCount][];
e.g:-int num[][]=new int[4][];
num[0]=new int[1];
num[1]=new int[2];
num[2]=new int[3];
num[3]=new int[4];

12
ENHANCED FOR LOOP FOR 2D ARRAY
for (int[] num: arr)
{
for(int data: num)
{
System.out.println(data);
}
}

13
ANONYMOUS ARRAY:
An array without any name is called
anonymous array.
Anonymous array is passed as an argument of
method
Syntax: new arrayType[]{ array elements};
// anonymous int array
new int[] { 1, 2, 3, 4};
// anonymous char array
new char[] {'x', 'y', 'z‘};
// anonymous String array
new String[] {“hello", “hi", “bye"};
// anonymous multidimensional array 14

new int[][] { {10, 20}, {30, 40, 50} };


3 D ARRAY:
Syntax:
array_type[][][] array_name = new
array_type[x][y][z];
Ex:
int[][][] num=new int[2][3][4];
Here, num[i][j][k] where ‘i’ is the array number,
‘j’ is the row number and ‘k’ is the column
number.

15
16
4 D ARRAY
Array of 3 D Array
int [][][][] num=new int[2][2][2][2];

Multi dimensional array means arrya of arrays.

17

You might also like