0% found this document useful (0 votes)
35 views2 pages

Array Java

An array is a collection of similar type of elements stored in contiguous memory locations that can be accessed using an index. Arrays allow storing multiple elements of the same data type. Elements are stored sequentially in memory with the first element at index 0, second at index 1 and so on. When the array is full and an attempt is made to insert an element at an invalid index, it will throw an ArrayIndexOutOfBoundsException. Examples demonstrate declaring, initializing, and accessing elements of both integer and string arrays. User input can also be taken to populate array elements.

Uploaded by

asmita dagade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views2 pages

Array Java

An array is a collection of similar type of elements stored in contiguous memory locations that can be accessed using an index. Arrays allow storing multiple elements of the same data type. Elements are stored sequentially in memory with the first element at index 0, second at index 1 and so on. When the array is full and an attempt is made to insert an element at an invalid index, it will throw an ArrayIndexOutOfBoundsException. Examples demonstrate declaring, initializing, and accessing elements of both integer and string arrays. User input can also be taken to populate array elements.

Uploaded by

asmita dagade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Array

====================================================

- Array is a collection of similar type of elements which has contiguous memory


location.
-Array is an object which contains elelmet of same data type.
-element of array is sored into contigeous memory location.
- Array is a index based where i can store my first number on 0th index, second
nuymber on 1st index,
third number on 2nd index and so on.

syntax and example->

int b[] = {8,7,12,6,9,5,33,1} ;

int arr[]= new int[8]; // array declaration


arr[0]= 8;
arr[1]=7;
arr[2]= 12;
arr[3]= 6;
arr[4]=9;
arr[5] = 5;
arr[6]= 33;
arr[7]=1;

note : when you try to insert extra element in the array when your array is
completly full
or you array index is not present then it will throw array index out of bound
exception.

Example->

int arr[]= new int[8]; // array declaration


arr[0]= 8;
arr[1]=7;
arr[2]= 12;
arr[3]= 6;
arr[4]=9;
arr[5] = 5;
arr[6]= 33;
arr[7]=1;

System.out.println(arr.length);

for(int x : arr ) { //for each loop


System.out.println(x);
}

System.out.println("==============================");
String strArr[] = new String[3];
strArr[0]= "Shital";
strArr[1]="Mayur";
for(String y : strArr) { //for each loop
System.out.println(y);
}

//when u take inpust from users


Scanner sc = new Scanner(System.in);
int p[]= new int[4];

for(int i=0; i< p.length;i++) {


System.out.println("Enter the number in "+i+" position");
p[i]= sc.nextInt();
}

System.out.println("Numbers in array which are entered by user");

for(int z :p) {
System.out.println(z);
}

You might also like