0% found this document useful (0 votes)
2 views5 pages

Arrays in Java

In Java, an array is an object that holds a fixed number of elements of the same data type, allowing for efficient storage and access of multiple values under a single variable name. Arrays are index-based, have a fixed size, and store homogeneous elements, making them versatile for various data types. However, they come with disadvantages such as fixed size, inability to store different types, and costly insertion or deletion operations.

Uploaded by

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

Arrays in Java

In Java, an array is an object that holds a fixed number of elements of the same data type, allowing for efficient storage and access of multiple values under a single variable name. Arrays are index-based, have a fixed size, and store homogeneous elements, making them versatile for various data types. However, they come with disadvantages such as fixed size, inability to store different types, and costly insertion or deletion operations.

Uploaded by

Anushka Singh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Arrays in Java

 In Java, array is an object that stores a fixed number of elements of the same data type.
 It stores multiple elements under a single variable name.
 Syntax :

int[] arr = {88, 74, 91, 82, 68, 94};

 Why we need arrays ?


o Suppose we have 6 students and we want to store their marks:
 int marks1 = 88;
int marks2 = 74;
int marks3 = 91;
int marks4 = 82;
int marks5 = 68;
int marks6 = 94;
o This works fine for a few students, but imagine we have 100 or 1000 students. Creating so many
variables would be difficult and messy.
o Solution: Arrays allow us to store all student's marks in a single variable:
 int[] marks = {88, 74, 91, 82, 68, 94};
o Now, we can access any student's mark using an index, loop through the marks, and perform
calculations easily.

Key Features of Java Arrays:


1. Index Based:
o Arrays elements are accessed using indices starting from 0.
o Example:

[Link](numbers[0]); // Output: 10
[Link](numbers[2]); // Output: 30

2. Fixed Size :
o After creating an array, the size cannot be changed.
o Example:

int[] numbers = new int[5]; // Size = 5


// numbers[5] = 10; // ❌ Error, index out of bounds
3. Stores Same Type of Elements:
o Arrays always stored homogeneous elements i.e. same type of data.
o Example:

int[] numbers = {10, 20, 30}; // Only int values allowed


// numbers[0] = 5.5; // ❌ Error, cannot store double

4. Contiguous Memory:
o Array elements are stored in continuous memory locations, which allows fast access.
o Diagram:

Advantages of Arrays
1. Efficient way to store multiple values:
o Arrays allow storing multiple values of the same type in a single variable.
o Example:

int[] marks = {88, 74, 91, 82, 68, 94};


String[] names = {"Amit", "Bhupinder", "Deepak", "Kamal", "Rahul",
"Ravi"};

2. Versatile:
o Arrays can store different types of data like numbers, characters or even objects.
o Example:

int[] marks = {88, 74, 91, 82, 68, 94};


String[] names = {"Amit", "Bhupinder", "Deepak", "Kamal", "Rahul",
"Ravi"};
Student[] std = {new Student(), new Student()};

3. Easy to access elements using loop:


o We can easily access all elements using loops like for or for-each.
o Example using for loop:

for (int i = 0; i < [Link]; i++)


{
[Link](marks[i]);
}

o Example using for-each loop:

for (String name : names)


{
[Link](name);
}

4. Supports random access:


o We can directly access any element using its index.
o Example:

[Link](marks[2]); // Output: 91
names[1] = "Harpreet"; // Changes "Bhupinder" to "Harpreet"

5. Helps to Build Other Data Structures:


o Arrays are the basic building blocks for many data structures like ArrayList, LinkedList, Stack, Queue,
etc.
o They provide a way to store and access elements efficiently, which these structures use internally.
6. No Object Conversion / No Type Casting Needed:
o When an array stores objects, you can directly access them without type casting.
o This makes the code simpler and safer.
o Example:

Person[] people = {new Person("Deepak"), new Person("Rahul")};

// Directly access object methods without casting


[Link](people[0].getName()); // Output: Deepak
Disadvantages of Arrays
1. Fixed size - Less flexibility:
o Once arrays are created, the size of an array cannot be changed.
o Example:
 int[] numbers = new int[5];
 // You cannot add a 6th element; need to create a new array
2. Cannot store elements of different types:
o All elements must be of the same data type.
o Example:
 int[] numbers = {10, 20, 30}; // Cannot add a string here
3. Insertion or deletion in the middle is costly:
o To insert or delete an element, other elements may need to be shifted.
o Example (deleting an element):
 int[] numbers = {10, 20, 30, 40};
 // To delete 20, shift 30 and 40 left
 numbers[1] = numbers[2]; // numbers becomes {10, 30, 30, 40}
 numbers[2] = numbers[3]; // numbers becomes {10, 30, 40, 40}
4. Memory Wastage:
o If we create an array larger than needed, the extra space is wasted because arrays have a fixed size.
o Example:
 int[] numbers = new int[10]; // Array can hold 10 elements
 numbers[0] = 5;
 numbers[1] = 10;
 // Only 2 elements are used, but memory for 10 is reserved
o This can waste memory, especially if the array size is much larger than the number of elements you
actually store.
5. No Built-in Methods:
o Arrays in Java do not have built-in methods for operations like adding, removing, or searching
elements.
o We have to write our own code to perform these operations.

Types of Arrays

 There are mainly 2 types of Arrays :


o Single-Dimensional Array
 Stores elements in a single row.
 Example :
 1-D Array int[] numbers = {10, 20, 30};
o Multi-Dimensional Arrays
 Arrays with more than one dimension (rows and columns).
 Example:
 2-D Array (most common)
 Represents data in a table (rows × columns).
 int[][] matrix = {{1,2}, {3,4}};
 3-D Array
 An array of 2-D arrays.
 int[][][] cube = new int[2][3][4];
 Higher-Dimensional Arrays (4-D, 5-D …)
 Rarely used, but possible in Java.
 Jagged Array
 A 2-D array where each row can have a different number of columns.
 int[][] jagged = {{1,2,3}, {4,5}, {6}};
 Matrix Array
 Usually refers to 2-D arrays used for mathematical matrices.

Points to Remember:

1. Index starts from 0


o The first element of an array is at index 0.
o int[] marks = {88, 74, 91, 82, 68, 94};
o [Link](marks[0]); // Output: 88
2. No of elements in an array = [Link]
o We can calculate the number of elements in an array using length property.
o int[] marks = {88, 74, 91, 82, 68, 94};
o [Link]([Link]); // Output: 6
3. Last index = [Link] - 1
o The last element’s index is always one less than the array size.
o int[] marks = {88, 74, 91, 82, 68, 94};
o [Link]("Last index position : "+[Link] - 1); //
Output: 5
o [Link]("Last Element : "+marks[[Link] - 1]); //
Output: 94
4. Can store primitives or objects
o Arrays can hold primitive types like int, char, etc., or objects like String or custom classes.
o char[] letters = {'A', 'B', 'C'};

o String[] names = {"Deepak", "Rahul", "Kamal"};


o Person[] people = {new Person(), new Person()};

You might also like