How Data Structures Make Your Code Faster
Before the concept of data structure data was often managed using simple , linear storage method like .
- Flat files : Data was stored in plain text files or binary files without any structured format .
- Arrays : Simple arrays were used to store data but the lacked the flexibility and efficiency of more advanced data structure .
- Manual Management : Programmer had to manually manage memory and data organization which was error prone and inefficient .
What is Data Structure .
A Data structure is specialized format for organizing , processing , retrieving and storing data . it defines the relationship between data and the operations. that can be performed of the data common data structures include arrays , linked lists ,stacks , queues , tree and graph .
Benefits of data structures
- Efficiency : Data structures allow for efficient data manipulation and retrieval . for example searching for an element in a balanced binary search tree is faster than searching in an unsorted array .
- Reusability : will defined data structures can be reused across different programs and applications .
- Abstraction : they provide a way to manage large amount of data by abstraction the details of data storage and manipulation .
- Optimization : Data Structure help in optimizing the performance of algorithms by providing the most suitable way to store and access data .
How the variable can store this line when compiler compiles this line .
int a=10
When jvm reads int a it reserve space in stack memory for variable a .
- JVM checks the data type (int)
- int requires 4 bytes of memory .
- Memory space is allocated immediately
When jvm executes a=10;
- the value 10 is stored directedly in the allocated stack location.
- primitive data types store actual values , not references .
Primitive data type memory allocation

Example :
int a=10 // 4 bytes
8*4 =32 bits

- Address always in hexadecimal form .
- int a=5 ; in this variable we can store only one variable . int a=16 it will store 5 by 16 .
- Suppose you want to store multiple value at a time its not possible with int a= 5,10 … . that’s why array comes into the picture .
Syntax :
int a[20] // In bracket we have to define consonant value
- Array is a collection of element , element data type should be same .
- All element stored in array is continuous location one after another and index stored from zero .
Array Initialization:
- Compile time
- Run Time
1. Compile time Initialization
int a[5] ={4,2,1,3,5} // we can not extend more than five length.
int a= 4 Bytes
a[5] = 5*4 = 20 Bytes
In Array data will store in consecutive way

Array follows random access method , suppose we have define an array and length of the array is 100 . but we are inserting only 10 character in the array , that means we are wasting of memory . this is the draw back of static array .
public class StaticArrayExample {
public static void main(String[] args) {
// Static array initialization
int[] numbers = {10, 20, 30, 40, 50}
// Printing array elements
for(int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
Output :
10
20
30
40
50
Drawbacks of Static Array in Java
- Fixed Size : Once created, size cannot be changed. If data increases → you must create a new array.
- Memory Wastage : If array size is large but data is small.
- Insertion is Costly : Adding element in middle requires shifting all elements.
- Cannot Grow Dynamically : Unlike
ArrayList, array cannot expand automatically.
2. Runtime array Initialization
A dynamic array is an array-like structure whose size can grow or shrink automatically at runtime depending on the data.
1.Dynamic Size Using User Input
import java.util.Scanner;
public class Demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter array size:");
int size = sc.nextInt(); // dynamic size
int[] arr = new int[size];
for(int i = 0; i < size; i++) {
arr[i] = sc.nextInt(); // dynamic values
}
}
}
Size and values both are decided at runtime in above code .
2.Dynamic Initialization Using Loop Logic
int[] arr = new int[5];
for(int i = 0; i < arr.length; i++) {
arr[i] = i * i; // dynamic value generation
}
3.Dynamic Initialization from Method Return
public static int[] createArray(int n) {
int[] arr = new int[n];
for(int i = 0; i < n; i++) {
arr[i] = i + 10;
}
return arr;
}
Operations on array in Data Structure
- Traversal
- Insertion
- Deletion
- Searching
- Sorting
1.Traversal of elements
Traversal means visiting each and every elements at least once in sequentially .
Example
public class ArrayTraversal {
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
// Traversing array using for loop
for(int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
}
}
2. Insert operation
2.1 Inserting Data in Array Based on Index
an array has fixed size, so when we insert an element at a specific index, we must make space for the new element. if we insert any element directedly from index 0 to 4 we can loose the original data . for resolve the above issues we have to shift the element in array .

Now suppose we want to insert 25 at index 2. If we directly put 25 at index 2 , value 30 is lost .
Example:
public class InsertArrayExample {
public static void main(String[] args) {
int[] arr = new int[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
int size = 4; // current elements
int index = 2; // insertion position
int value = 25;
// shifting elements to right
for(int i = size; i > index; i--) {
arr[i] = arr[i - 1];
}
// insert new value
arr[index] = value;
size++;
// print array
for(int i = 0; i < size; i++) {
System.out.println(arr[i]);
}
}
}
Output
10
20
25
30
40
2.2 How to insert the beginning of array
When inserting an element at the beginning (index 0) of an array, all existing elements must be shifted one position to the right. Because arrays have fixed positions, we must create space at index 0.
Example : Before Insertion

Insert 5 at beginning .
After Insertion

Example :
public class InsertAtBeginning {
public static void main(String[] args) {
int[] arr = new int[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
int size = 4; // current number of elements
int value = 5; // value to insert
// Step 1: Shift elements to right
for(int i = size; i > 0; i--) {
arr[i] = arr[i - 1];
}
// Step 2: Insert at index 0
arr[0] = value;
size++;
// Step 3: Print array
for(int i = 0; i < size; i++) {
System.out.println(arr[i]);
}
}
}
Output
5
10
20
30
40
2.3 How to insert the element and of array
Inserting an element at the end of an array no shifting is required .We just place the new value at the next available index.
public class InsertAtEnd {
public static void main(String[] args) {
int[] arr = new int[5];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
int size = 4; // current number of elements
int value = 50; // value to insert
// Insert at end
arr[size] = value;
size++;
// Print array
for(int i = 0; i < size; i++) {
System.out.println(arr[i]);
}
}
}
3. How to delete the element from Array
Arrays have fixed size, so we cannot actually remove memory. Instead, we delete an element by shifting remaining elements to the left and reducing the logical size.
Deletion = Remove value + Left shifting.
Example : Before Deletion

Delete element at index 2 (30) .
After Deletion

public class DeleteArrayExample {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int size = 5;
int index = 2; // position to delete
// Shift elements to left
for(int i = index; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--; // reduce logical size
// Print updated array
for(int i = 0; i < size; i++) {
System.out.println(arr[i]);
}
}
}
Output
10
20
40
50
time complexity depends on the which elements you want to delete the data . in unsorted order it will take o(1) . but in sorted order it will take constant time , depends on the position of the array .