1.
Define Array in Java
Array: An array in Java is a container object that holds a fixed number of values of a single
type. It is used to store multiple values in a single variable, instead of declaring separate
variables for each value. Arrays in Java are zero-indexed, meaning the first element is at
index 0.
2. Types of Arrays
One-dimensional Array: A list of items that can be accessed via a single index. Example: int[ ]
numbers = {1, 2, 3};
Multi-dimensional Array: Arrays of arrays, where each element is itself an array. Common
types are:
Two-dimensional Array (like a table): int[ ][ ] matrix = new int[3][3];
Three-dimensional Array or more, for complex structures like cubes or hypercubes.
3a. Is a Class an Array? Explain
No, a class is not an array.
Class: A class in Java is a blueprint for creating objects (instances of the class). It defines
attributes (data) and methods (behaviors) encapsulated within an object.
Array: An array is a data structure that stores elements of the same type in a contiguous
block of memory. It's a collection of variables of the same type.
3b. Difference Between Class and Array
Structure: A class has methods, constructors, and can implement interfaces, while an array is
simply a collection of values.
Flexibility: Classes can inherit from other classes, have inner classes, and are more flexible in
defining the structure. Arrays are rigid in terms of type and size once instantiated.
Usage: Classes are used to model complex objects or entities in a program, whereas arrays
are primarily used for storing multiple items of the same type.
3c. How to Check if a Class is an Array
Use the instanceof operator or [Link]() method:
java
Object obj = new int[5];
1
boolean isArray = obj instanceof int[ ]; // true
boolean isArrayClass = [Link]().isArray(); // true
3d. Declare an Array in Java
Declaration and Initialization:
java
// Declaration
int[ ] myIntArray;
String[ ] myStringArray;
// Initialization
myIntArray = new int[5]; // Array of size 5
myStringArray = new String[ ] {"a", "b", "c"}; // Array with predefined elements
4a. Explain ArrayList
ArrayList: An implementation of the List interface backed by an array. It's part of the Java
Collections Framework, providing dynamic arrays that can grow as needed. Unlike arrays,
ArrayList can increase or decrease in size.
4b. Difference Between Array and ArrayList
Size: Array's size is fixed at creation; ArrayList can dynamically resize.
Methods: ArrayList provides many utility methods like add(), remove(), contains(), which
aren't directly available with arrays.
Type: Array holds elements of a single type; ArrayList can hold objects of any type but with
the same wrapper (e.g., all must be Integer or String).
4c. How to Print an ArrayList in Java
Use [Link]() with toString() method implicitly called:
java
ArrayList<String> list = new ArrayList<>();
[Link]("apple");
[Link]("banana");
[Link](list); // [apple, banana]
2
4d. How to Print an ArrayList as an Array in Java
Convert ArrayList to array for printing:
java
ArrayList<String> list = new ArrayList<>();
[Link]("apple");
[Link]("banana");
// Convert ArrayList to array
String[] array = [Link](new String[0]);
// Print each element of the array
for (String s : array) {
[Link](s + " ");
// Output: apple banana