0% found this document useful (0 votes)
7 views8 pages

Data Structure Notes

This document provides comprehensive notes on arrays in Java, including definitions, declarations, initializations, and default values. It covers various array operations such as finding length, copying, reversing, sorting, comparing, searching, and iterating through arrays. The document also explains binary search as an efficient method for searching in sorted arrays.
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)
7 views8 pages

Data Structure Notes

This document provides comprehensive notes on arrays in Java, including definitions, declarations, initializations, and default values. It covers various array operations such as finding length, copying, reversing, sorting, comparing, searching, and iterating through arrays. The document also explains binary search as an efficient method for searching in sorted arrays.
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
You are on page 1/ 8

Data Structure Notes

sic Concepts

1. What is an array in Java?

Answer: An array is a container object that holds a fixed number of values of a single type.

Example:

int[] arr = {1, 2, 3, 4}

2. What is an array in Java?

Definition: An array is a container object that holds a fixed number of values of a single type.
Example:
int[] arr = {1, 2, 3, 4};

How do you declare an array in Java?


Answer:
int[] arr; // Preferred
int arr[]; // Also valid

How do you initialize an array in Java?


Answer:
int[] arr = new int[5]; // Initialized with default values

What is the default value of array elements in


Java?
Answer:
int: 0, float: 0.0, boolean: false, Object: null

Can you change the size of an array after


initialization?
Answer: No. Arrays are fixed in size once initialized.

Array Operations

How to find the length of an array?


Answer: Using .length property.
Example: int len = arr.length;
How to copy an array in Java?
Answer:
int[] copy = Arrays.copyOf(original, original.length);

How to reverse an array in Java?


Answer:
for (int i = 0; i < arr.length / 2; i++) {
int temp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = temp;
}

How to sort an array in Java?


Answer:
Arrays.sort(arr);

How to compare two arrays?


Answer:
Arrays.equals(arr1, arr2);

Searching and Iteration

How to search an element in an array?


Answer:
for (int i = 0; i < arr.length; i++) {
if (arr[i] == key) {
// found
}
}

What is binary search?


Definition: Efficient search on a sorted array with O(log n) time.
Example:
Arrays.binarySearch(arr, key);

How to iterate over an array?


Answer:
for (int num : arr) {
System.out.println(num);
}

You might also like