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

Arrays

An array in Java is a collection of elements of the same type stored in contiguous memory locations, allowing multiple values to be stored under a single variable name. Key operations include accessing elements, sorting, copying, and converting between arrays and lists. Various methods for traversing, reversing, finding minimum/maximum values, and performing binary search on arrays are also discussed.

Uploaded by

sandhyakontham05
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 views4 pages

Arrays

An array in Java is a collection of elements of the same type stored in contiguous memory locations, allowing multiple values to be stored under a single variable name. Key operations include accessing elements, sorting, copying, and converting between arrays and lists. Various methods for traversing, reversing, finding minimum/maximum values, and performing binary search on arrays are also discussed.

Uploaded by

sandhyakontham05
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

🔹 What is an Array in Java?

An array in Java is a collection of elements of the same type, stored in


contiguous memory locations.
It allows you to store multiple values under a single variable name instead of
declaring separate variables for each value.

Syntax
datatype[] arrayName = new datatype[size];
datatype arrayName[] = new datatype[size];

// Printing array elements


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

Accessing Array Elements: arrayName[index];


Array Length: [Link];
Sort: [Link](arr);
Copy array: int[] copy=
[Link](arr, [Link]);
Convert to String [Link](arr);
Array to List List<Integer> list =
[Link](arr).boxed().toList();
List to Array int[] nums = list. stream().mapToInt
(Integer::intValue) .toArray();

Traversing an Array
1. Using for loop:
for (int i = 0; i < [Link]; i++) {
[Link](marks[i]);
}
2. Using for-each loop:
for (int m : marks) {
[Link](m);
}

Reverse an Array:
for (int i = 0, j = [Link] - 1; i < j; i++, j--) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}

Finding Minimum / Maximum


int max = Integer.MIN_VALUE;
for (int num : arr) {
if (num > max) max = num;
}

Prefix Sum / Cumulative Sum


int[] prefix = new int[[Link] + 1];
for (int i = 0; i < [Link]; i++) {
prefix[i + 1] = prefix[i] + arr[i];
}
Using HashSet / HashMap with Arrays
import [Link];
HashSet<Integer> set = new HashSet<>();
for (int num : arr) {
if ([Link](num)) {
[Link]("Duplicate found: " + num);
}
[Link](num);
}
Binary Search (on sorted arrays)
int left = 0, right = [Link] - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) return mid;
else if (arr[mid] < target) left = mid + 1;
else right = mid - 1;
}
Merging Arrays
int[] merged = new int[[Link] + [Link]];
[Link](arr1, 0, merged, 0, [Link]);
[Link](arr2, 0, merged, [Link], [Link]);

You might also like