Array basics, multidimensional arrays, copying, sorting, and searching.
Question 1: What is an array in Java?
Ans
An array is a fixed-size container that holds multiple values of the same type in contiguous memory, with each value accessed using a numeric index starting at 0. Once created, an array's length can never change.
Example
java
int[] marks = {80, 90, 75};
System.out.println(marks[1]); // 90
Important Point
Accessing an index outside the valid range (0 to length-1) throws ArrayIndexOutOfBoundsException.
Question 2: What is the difference between an array and an ArrayList?
Ans
An array has a fixed length set at creation time and can directly store primitives. An ArrayList is a resizable List implementation that grows and shrinks automatically as elements are added or removed, but it stores objects, so primitives are automatically boxed into their wrapper types.
Example
java
int[] a = new int[5]; // fixed size, primitives directly
ArrayList<Integer> list = new ArrayList<>(); // resizable, boxes int to Integer
Important Point
Choose an array when the size is fixed and performance/memory matters; choose ArrayList when the collection needs to grow or shrink.