6 Questions
What is the primary characteristic of an array in Java?
Fixed-size and homogeneous
How can you obtain the length of an array in Java?
Using the length
property
What is the purpose of the Arrays.fill()
method?
To fill an array with a specified value
How do you access an element in a multidimensional array?
Using multiple indices
What is the purpose of the Arrays.equals()
method?
To compare two arrays for equality
What is the exception thrown when an array is accessed with an index that is out of bounds?
ArrayIndexOutOfBoundsException
Study Notes
Declaring and Initializing Arrays
- In Java, an array is a fixed-size, homogeneous collection of elements.
- Arrays can be declared in two ways:
-
int[] myArray;
(recommended) -
int myArray[];
-
- Arrays can be initialized in two ways:
-
int[] myArray = new int[5];
(declare and initialize simultaneously) -
int[] myArray;
myArray = new int[5];
(declare and initialize separately)
-
Array Elements
- Array elements are accessed using their index (starting from 0).
- Elements can be assigned values using the assignment operator (
=
). - Example:
myArray[0] = 10;
Array Operations
-
Length: The length of an array can be obtained using the
length
property. -
Copying: Arrays can be copied using the
Arrays.copyOf()
method. -
Sorting: Arrays can be sorted using the
Arrays.sort()
method.
Multidimensional Arrays
- Multidimensional arrays can be declared and initialized in a similar way to one-dimensional arrays.
- Example:
int[][] my2DArray = new int[3][4];
- Elements can be accessed using multiple indices. Example:
my2DArray[0][1] = 10;
Array Methods
- Arrays.toString(): Converts an array to a string representation.
- Arrays.equals(): Compares two arrays for equality.
- Arrays.fill(): Fills an array with a specified value.
Common Array Errors
- ArrayIndexOutOfBoundsException: Thrown when an array is accessed with an index that is out of bounds.
- NullPointerException: Thrown when an array is not initialized before use.
Declaring and Initializing Arrays
- In Java, an array is a fixed-size, homogeneous collection of elements.
- Arrays can be declared in two ways:
int[] myArray;
andint myArray[];
. - Arrays can be initialized in two ways:
int[] myArray = new int;
andint[] myArray;
myArray = new int;
.
Array Elements
- Array elements are accessed using their index (starting from 0).
- Elements can be assigned values using the assignment operator (=).
- Example:
myArray = 10;
.
Array Operations
Length
- The length of an array can be obtained using the
length
property.
Copying
- Arrays can be copied using the
Arrays.copyOf()
method.
Sorting
- Arrays can be sorted using the
Arrays.sort()
method.
Multidimensional Arrays
- Multidimensional arrays can be declared and initialized in a similar way to one-dimensional arrays.
- Example:
int[][] my2DArray = new int;
. - Elements can be accessed using multiple indices.
- Example:
my2DArray = 10;
.
Array Methods
Arrays.toString()
- Converts an array to a string representation.
Arrays.equals()
- Compares two arrays for equality.
Arrays.fill()
- Fills an array with a specified value.
Common Array Errors
ArrayIndexOutOfBoundsException
- Thrown when an array is accessed with an index that is out of bounds.
NullPointerException
- Thrown when an array is not initialized before use.
Learn how to declare and initialize arrays in Java, including the different ways to do so and how to access array elements.
Make Your Own Quizzes and Flashcards
Convert your notes into interactive study material.
Get started for free