9. Java array index
int data[]=new int[5];
Last element
First element
data[0] data[1] data[2] data[3] data[4]
Index Array data of length 5
1 2 3 4 5
0 1 2 3 4
10. • boolean : false
• int : 0
• double : 0.0
• String : null
• User Defined Type : null
Default array values
11. public class ArrayDemo
{
public static void main(String[] args){
String str[] = new String[5];
for (String s : str)
System.out.print(s + " ");
System.out.println();
int num[] = new int[5];
for (int val : num)
System.out.print(val + " ");
System.out.println();
double dnum[] = new double[5];
for (double val : dnum)
System.out.print(val + " ");
System.out.println();
boolean bnum[] = new boolean[5];
for (boolean val : bnum)
System.out.print(val + " ");
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
15. How to access elements from an array?
First element - marks[0]
Second element – marks[1]
Third element – marks[2]
Fourth element – marks[3]
Fifth element – marks[4]
marks[ ] = { };
size = 5
5 15, 24, 45, 34, 60
To access any nth element,
marks[n-1]
0, 1, 2, 3, 4 - index
16. // Predict the output
import java.util.*;
public class Main
{
public static void main(String[] args)
{
int[] arr = {12, 4, 5, 2, 5};
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " ");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
17. // Predict the output
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int n = s.nextInt();
int arr[]=new int[n];
for(int i = 0; i < arr.length; i++)
{
arr[i] = s.nextInt();
}
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + " " );
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
18. //Java Array of Objects
class Std {
String name;
public Std(String name){
this.name = name;
}
}
public class Main {
public static void main(String args[]) throws Exception {
Std myArray[] = new Std[3];
myArray [0] = new Std("Java");
myArray [1] = new Std("Object");
myArray [2] = new Std("Array");
for(Std str:myArray)
{
System.out.println(str.name);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
19. Storage of Arrays
Stack area
myArray[]
Heap Memory
obj1 obj2 obj3
new Std(“Java”)
new
Std(“Object”)
new Std(“Array”)
20. Anonymous Array
• An array in Java without any name is called anonymous array.
• It is just for instant use (i.e. one time usage) .
• Anonymous array is passed as an argument of method.
Syntax:
new Datatype[] {val1, val2..valn};
21. import java.util.*;
public class MyClass {
public static void main(String args[])
{
int s1 = sum(new int[]{1,2,3,4,5});
System.out.print(s1);
}
public static int sum(int a[])
{
int total = 0;
for(int i:a)
{
total = total+i;
}
return total;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Description:
Ramesh is creating twelve variable called mark1, mark2… mark12 to store marks of 12 students.
It is more difficult if there 60 students in his class.
Description:
He can create a single variable name called mark. He can divide the mark into 12 columns so that he can store the marks of all 12 students in single record called mark.
Here comes the array to fulfill our needs
Description:
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.
1D Array
In this array which can 1 row and many columns i.e.) row = 1, column = n.
Example:
A deck of cards.
2D Array
In this array which can multiple rows and columns i.e.) row = n, column = m.
Example:
Collection egg in an egg tray.
Description:
dataType can be a primitive data type like: int, char, Double, byte etc. or an object (will be discussed in later chapters).
arrayName is an identifier. For example put a array name as our wish .
Example:
int name[];
char data[];
Double size[];
data =new int[10], 10 is an array length.
Static Array Initilization:
int arr[]={1,2,3,4,5};
(or)
int arr[] = new int[5];
Dynamic Array Initilization:
int arr[]=new int[n];
Description:
If we don’t assign values to array elements, and try to access them, compiler does not produce error as in case of simple variables. Instead it assigns values which aren’t garbage.
Output
Description:
int is a primitive datatype. We know that the java program will load in stack memory. Since array is a reference. So the reference are stored in heap memory. Since it is a primitive datatype it is stored in adjacent memory location.
Title + Text
Note1:
arr.length is used to return the number of elements in an array.
In here, there are five elements in array “arr[]”. So it return’s value 5.
Output:
12 4 5 2 5
Description:
In this program array initialization is static. Because here we are initializing the array values before compiling the program. Here array length is 5 and i value start form 0. i<5 time loop has been excuted so output is
12 4 5 2 5.
Note2:
ArrayIndexOutOfBounds Exception
It is a Runtime Exception thrown only at runtime.
This exception occurs when we try to access the negative index or index greater than array size.
Input:
5
1 2 3 4 5
Output:
1 2 3 4 5
Description:
In this program array initilization is dynamic . i.e.) were creating the size of array during run time, so it is called Dynamic memory allocation.
int ‘n’ is an array size and array elements are 1 2 3 4 5 so output is 1 2 3 4 5.
Output:
15
Description:
It is a program for anonymous array.
I am creating an anonymous array. I want to find the sum of the array.
While creating an anonymous array you should pass the that array to a method so that you can perform the desired function.
Since there is no reference for anonymous array you can’t able to perform the operation in main method itself.
On passing the anonymous array to a method. Now only temporary reference is created i.e.) int a[].Now the array values are fetched in array a[] ={1,2,3,4,5}.
In this sum method the values in the array a[] are get summed and stored in total. This method returns the total i.e)15 to main method now the value is stored
in s1.