Chapter 5 but shorter Flashcards
What is an array in Java?
A collection of variables of the same type stored in a fixed-size sequential order.
Why use arrays?
✅ Easier management of multiple values
✅ Efficient storage and access
✅ Helps organize large data sets
Example of declaring an array?
```java
int[] scores = new int[50];
~~~
✅ Creates an array named scores
that holds 50 integers.
How are array elements accessed?
Using an index, which starts from 0.
Example of accessing an element?
```java
int[] scores = {79, 87, 94, 82};
System.out.println(scores[2]); // Output: 94
~~~
✅ scores[2]
refers to the third element (index 2).
Different ways to declare an array?
✅ Declare only: int[] scores;
✅ Declare & initialize: int[] scores = new int[10];
✅ Using an initializer list: int[] scores = {79, 87, 94};
What happens if an array is not initialized?
It contains default values:
- 0
for numeric types
- false
for boolean
- null
for objects
Where are arrays stored in memory?
Arrays are objects stored in heap memory.
Example of storing an array?
```java
int[] numbers = new int[5]; // Stores integers in contiguous memory locations
~~~
What is a 2D array?
An array with rows and columns (like a table).
Example of declaring a 2D array?
```java
int[][] matrix = new int[2][3];
~~~
✅ Creates 2 rows and 3 columns.
Example of initializing a 2D array?
```java
int[][] values = { {1, 2, 3}, {4, 5, 6} };
System.out.println(values[1][2]); // Output: 6
~~~
✅ values[1][2]
refers to row 1, column 2.
What happens if an index is out of bounds?
ArrayIndexOutOfBoundsException
is thrown.
Example of an out-of-bounds error?
```java
int[] numbers = {1, 2, 3};
System.out.println(numbers[5]); // Error: Index 5 is out of bounds!
~~~
✅ Java prevents access beyond valid indices.
How to loop through an array using a for loop?
```java
for (int i = 0; i < scores.length; i++) {
System.out.println(scores[i]);
}
~~~
✅ Prints all elements from first to last.
How to use an enhanced for loop?
```java
for (int score : scores) {
System.out.println(score);
}
~~~
✅ Easier way to iterate, but cannot modify elements.
Does array1 = array2;
copy an array?
No! It only copies the reference, not the values.
Three correct ways to copy an array?
- Using a loop
```java
for (int i = 0; i < sourceArray.length; i++) {
targetArray[i] = sourceArray[i];
}
~~~
- Using
arraycopy()
```java
System.arraycopy(sourceArray, 0, targetArray, 0, sourceArray.length);
~~~
- Using
clone()
```java
int[] targetArray = sourceArray.clone();
~~~
✅ Creates a new array with the same values.
- Where:
sourceArray → The array from which elements are copied.
srcPos (0) → The starting position (index) in sourceArray from where copying begins.
targetArray → The array into which elements are copied.
destPos (0) → The starting position (index) in targetArray where elements will be copied.
length (sourceArray.length) → The number of elements to copy.
How are arrays passed to methods?
By reference, meaning changes inside the method affect the original array.
Example of passing an array?
```java
public static void printArray(int[] arr) {
for (int num : arr) {
System.out.println(num);
}
}
printArray(new int[] {1, 2, 3, 4});
~~~
✅ Prints 1 2 3 4
.
How can a method return an array?
```java
public static int[] getNumbers() {
return new int[] {10, 20, 30};
}
int[] nums = getNumbers();
~~~
✅ The method returns an array reference.
What is a variable-length argument list (varargs
)?
Allows passing any number of arguments to a method.
Example of varargs
?
```java
public static void printMax(double… numbers) {
double max = numbers[0];
for (double num : numbers) {
if (num > max) max = num;
}
System.out.println(“Max: “ + max);
}
printMax(3.5, 7.2, 9.8, 1.4); // Output: Max: 9.8
~~~
✅ Accepts any number of double
values.
How does Java receive command-line arguments?
Through the args
parameter in main()
.
Example of reading arguments?
```java
public class ArgTest {
public static void main(String[] args) {
System.out.println(“First argument: “ + args[0]);
}
}
✅ Running `java ArgTest Hello` prints:
First argument: Hello
~~~
Example of finding the maximum number in an array?
```java
int max = userVals[0];
for (int i = 1; i < userVals.length; i++) {
if (userVals[i] > max) max = userVals[i];
}
System.out.println(“Max: “ + max);
~~~
✅ Loops through the array and finds the largest number.