0% found this document useful (0 votes)
67 views4 pages

Array in Java (F)

Java provides arrays, which are fixed-size collections of elements of the same type. An array stores multiple variables of the same type sequentially in memory. Instead of declaring individual variables like number0, number1, etc., an array declaration like int numbers[] allows accessing multiple elements like numbers[0], numbers[1] using indexes. Arrays are an example of a non-primitive derived data type in Java and elements are stored adjacently in memory. Single dimensional arrays have a single index to access each element.

Uploaded by

Brajesh Rajawat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
67 views4 pages

Array in Java (F)

Java provides arrays, which are fixed-size collections of elements of the same type. An array stores multiple variables of the same type sequentially in memory. Instead of declaring individual variables like number0, number1, etc., an array declaration like int numbers[] allows accessing multiple elements like numbers[0], numbers[1] using indexes. Arrays are an example of a non-primitive derived data type in Java and elements are stored adjacently in memory. Single dimensional arrays have a single index to access each element.

Uploaded by

Brajesh Rajawat
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

Array in Java

java provides a data structure, the array, which stores a fixed-size sequential collection of
elements of the same type. An array is used to store a collection of data, but it is often more
useful to think of an array as a collection of variables of the same type.

Instead of declaring individual variables, such as number0, number1, ..., and number99, you
declare one array variable such as numbers and use numbers[0], numbers[1], and ...,
numbers[99] to represent individual variables.

Definition of Array

 An array is a group of similar (like-typed) variables that are referred to by a common name. A
specific element in an array is accessed by its index (subscript).

 Array is an example of non-primitive derived data type.

IMP. NOTES

 In Java array is considered as reference type variable.

 To declare an array we need primitive data type keywords (int, char, double …. )

 Arrays of any type can be created & may have one or more dimensions.

 In computer’s memory array elements are stored adjacently.

 It is an example of linear list

Sda(Single Dimensional Array)

 This is simplest form of an array & It is also known as one dimensional array.

 In SDA elements are specified by a single subscript / index number.

 In SDA index numbering starts with 0(Zero).

 length keyword is used to find size/length of a single dimensional array(SDA).

Example :

int arr[ ] = new int[10];

Size of the array


Data type of array Name of the array keyword
(No off cells)

or we can define as int [] arr=new int[10];


new is a key word used to occupy memory space for array and to set reference

int arr[ ] = new int[5] ;

 By above statement an array called arr of integer type is created to store 5


integer values

 It demands 20 bytes in memory (4 bytes x 5 ) .

 Length of above array is 5.

Arr[0] Arr[1] Arr[2] Arr[3] Arr[4]

Index no -0 Index no -1 Index no -2 Index no -3 Index no -4


Cellno 0 Cellno 1 Cellno 2 Cellno 3 Cellno 4

MORE EXAMPLES
char p[ ] =new char [10 ] ;
By this a character array called p is created / declared with 10 cells
String s[ ] = new String [15];
By this a String array called s is created / declared to store 15 strings
Find the output
class array
{
Public void main()
{
int id ,x =10;

for ( id=0 ; id<=4;id++)


{
n[ id ] =x ;
x++;
}
for ( id=0 ; id<=4;id++)
{
System.out.println(n[id]);
}
}}

Initialization of array

//Java Program to illustrate how to declare, instantiate, initialize


//and traverse the Java array.
class Testarray{
public static void main(){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<4;i++)//or a.length length is the property of array
System.out.println(a[i]);
}}

Another way(important)

Declaration, Instantiation and Initialization of Java Array


We can declare, instantiate and initialize the java array together by:

 int n[ ] = {24,46,10,15,100 }; // array is initialized with 5 integer values


 int a[]={33,3,4,5};//declaration, instantiation and initialization
 /Java Program to illustrate the use of declaration, instantiation
 //and initialization of Java array in a single line
class Testarray1{
public static void main(){
int a[]={33,3,4,5};//declaration, instantiation and initialization
//printing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}
a.length will find the length of array here length =4
n.length  length os 5

Advantages of arrays:

 We can access any element randomly by using indexes provided by arrays.


 Primitive type to wrapper classes object conversion will not happen so it is fast.
 Array can store many number of elements at a time.
 Easy to manipulate and store large data
Disadvantages of array in java

 We need to mention the size of the array. Fixed length.


 So there is a chance of memory wastage.
 To delete an element in an array we need to traverse throughout the array so this will
reduce performance

You might also like