Array - Single & Multidimensional

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 13

Arrays In Java

Dr.Archana B Saxena
archanabsaxena@gmail.com
Archanab.saxena@jimsindia.org

11/01/23 Arrays 1
ARRAYS
Array is a finite ordered and collection of homogeneous data
elements.
•An array is a structure that holds multiple values of the same
type .
•Arrays can be one dimensional or more.
•The length of an array is established when the array is created
(at runtime).
•After creation, an array is a fixed-length structure.
•An array element is one of the values within an array and is
accessed by its position within the array.
•Arrays can hold reference types as well as primitive types
[Array_Single.java]
11/01/23 Arrays 2
Creating of ARRAY
1. Declare the Array
2. Create memory location
3. Put values into the memory Location
• Declare the Array:
An array declaration has two components: the array's type and the array's name.
datatype arrayname[];
datatype[] arrayname;
ex : int marks[]; int[] marks;
In java it is required to allocate memory to an array using new operator ,.
Till declaration just reference variable is available, no physical memory.
For the physical allocation of memory it is required to use new operator or directly
assign values to array location.
After initialization all the elements of an array will be automatically initialized to
0.
Remember we do not enetr the size of arrays in the declaration.
11/01/23 Arrays 3
• Creating Memory location:
You create an array explicitly using Java's new operator.
Arrayname = new type[];
int [] marks = new int[10]; // create an array of integers
It is possible to combine declaration and creation in one step like :
int number = new int[5];
• Initialization of Arrays:
An array initializer is a list of comma separated expressions
surrounded by curly braces.
marks.
int marks[5];
Arrayname [subscript] = value;
Marks[i] =100;
int marks[] = {70,80,56,78}; [array can be declared, initialize and
memory
11/01/23
can be saved in a single
Arrays
statement.] 4
Multidimensional Array
In java multidimensional arrays are actually
arrays of arrays. To declare a
multidimensional array variable, specify
each additional index using another set of
square brackets.
Int two_array [] [] = new int [4][5]
- In above declared two dimensional array,
left index determines row and right index
determines column.

11/01/23 Arrays 5
• Multidimensional array:
Arrays can contain arrays.
Ex : int marks[][] = new int[4][5]
• When we allocate memory, we need only specify the
memory for the first(leftmost) dimension.
• We can allocate remaining dimensions separately.
• It is possible to initialize multidimensional arrays.
int marks[][]={{1,2},{4,5}}

11/01/23 Arrays 6
Uneven/Irregular Multidimensional Array
When you allocate memory for a multidimensional array
, you need to specify only the memory for the first
(leftmost) dimension. You can allocate the remaining
dimension separately.
Example int two_array [][] =new int [4][];
two_array [0] = new int[1];
two_array [1] = new int[2];
two_array [2] = new int[3];
two_array [3] = new int[4];
[Dynamic_Two_D_Array.java]
11/01/23 Arrays 7
Alternative array Declaration Syntax
-Datatype name_of_array[];
-Datatype [] name_of_aray;
- int a1[] = new int[3];
- int [] a1 = new int[3];
- int a1[][] = new int [4][5];
- int [][]a1 = new int [3][4];
- Int [] a1,a2,a3;
- Int a1[], a2[],a3[];

11/01/23 Arrays 8
Reference Data-type Array
• Java allows you to create array of reference
data-type.
[Reference_Array.java]

11/01/23 Arrays 9
String Arrays
• we can define a String Array as an array holding a
fixed number of strings or string values. String
array is one structure that is most commonly used
in Java. If you remember, even the argument of
the ‘main’ function in Java is a String Array.
• String array is an array of objects. This is because
each element is a String and you know that in
Java, String is an object. You can do all the
operations on String array like sorting, adding an
element, joining, splitting, searching, etc.

11/01/23 Arrays 10
Declaring a String array
• String myarr[] = new String[];
• String[] numarray = {“one”, “two”,
“three”};
• String[] strArray = new String[] {“one”,
“two”, “three”, “four”};
• String[] strArray = new String[3];
strArray[0] = “one”;
strArray[1] = “two”;
strArray[2] = “three”;
11/01/23 Arrays 11
String Array Operations

• Size of String array (length)


• Traversing a String array [For each loop]
• Searching a String Array (equals)
• Sorting a String array (sort)
• Converting a string array:
– toString()
– toBytes()

11/01/23 Arrays 12
Why Arrays are dynamic in Java Script

• https://linuxhint.com/create-dynamic-array-
in-javascript/

11/01/23 Arrays 13

You might also like