Chapter 5 Flashcards
What is an array in Java?
An array is a collection of variables of the same type, stored in a fixed-size sequential order. It allows grouping multiple variables under one name, making data management more efficient.
How do you declare and initialize an array in Java?
```java
int[] scores = new int[50]; // Declares an array of integers with 50 elements
char[] letters = new char[10]; // Declares an array of characters with 10 elements
~~~
Arrays must be instantiated before use.
How are arrays indexed in Java?
An array of size N is indexed from 0 to N-1. Each element can be accessed using an index in square brackets.
Example:
```java
int[] scores = {79, 87, 94, 82, 67, 98, 87, 81, 74, 91};
System.out.println(scores[2]); // Outputs: 94
~~~
What are array elements in Java?
Array elements are the values stored within an array. Each element has an index and must be of the same type as the array. Arrays can store primitive data types (int, char, double) or objects.
Where are arrays stored in Java memory?
Arrays are objects in Java, and Java Virtual Machine (JVM) stores them in the heap memory.
How do you assign values to an array?
```java
scores[2] = 89; // Assigns 89 to index 2
scores[first] = scores[first] + 2; // Updates value at ‘first’ index
mean = (scores[0] + scores[1]) / 2; // Uses array values in a calculation
System.out.println(“Top = “ + scores[5]); // Prints a specific element
~~~
Arrays allow direct element manipulation using their index.
How can you declare an array in Java?
An array can be declared as:int[] scores;
// just declaringint[] scores = new int[10];
// declaring and initializing
What is the type of the variable scores
when declaring it as int[] scores
?
The type of the variable scores
is int[]
, meaning it is an array of integers.
Does the array type in Java specify its size?
No, the array type does not specify its size. However, each object of that type has a specific size once it is instantiated.
What happens when you declare an array like int[] scores = new int[10];
?
The reference variable scores
is set to a new array object that can hold 10 integers.
How can the brackets of the array type be associated in Java?
The brackets of the array type can be associated either with the element type or with the name of the array.
For example:float[] prices;
float prices[];
Both are equivalent, but the first format is generally more readable and should be used.
Can you give examples of array declarations with initialization in Java?
Yes, here are some examples:float[] prices = new float[500];
boolean[] flags; flags = new boolean[20];
char[] codes = new char[50];
What is the default value for numeric primitive data type elements in an array?
The default value is zero.
What is the default value for boolean elements in an array?
The default value is false
.
What is the default value for char
elements in an array?
The default value is \u0000
.
How can a programmer initialize an array’s elements with non-default values?
A programmer can specify the initial values in braces {}
separated by commas.
For example:int[] myArray = {5, 7, 11};
char[] letterGrades = {'A', 'B', 'C', 'D', 'F'};
What are the key points when using an initializer list for an array in Java?
When an initializer list is used:
- The new
operator is not used.
- No size value is specified.
- The size of the array is determined by the number of items in the initializer list.
- It can only be used in the array declaration.
Example:int[] units = {147, 323, 89, 933, 540, 269, 97, 114, 298, 476};
How would you declare and initialize an array of Strings in Java?
Declare an array and assign values as follows:String[] names = new String[3];
names[0] = "Sophia";
names[1] = "Emma";
names[2] = "Isabella";
What happens when you declare and initialize an array of Strings, but only assign values to some elements?
Unassigned elements will have the value null
.
Example:String[] names = new String[3];
names[0] = "Sophia";
After initialization:names[0] = "Sophia";
names[1] = null;
names[2] = null;
How do you assign a value to an element of an array and then modify it in Java?
You can assign a value and modify it later. For example:String[] names = new String[3];
names[0] = "Sophi";
names[0] = names[0] + "a";
This will result in names[0] = "Sophia";
.
How does array syntax compare to non-array types in Java?
For arrays:
- Square brackets []
are used.
- Arrays require an additional statement to size the array using new
.
- Square brackets and an index are used to access elements (both to get and to set).
Can elements of an array in Java be object references?
Yes, the elements of an array can be object references.
How would you declare an array to store 5 references to String
objects in Java?
String[] words = new String[5];
This reserves space for 5 references to String
objects but does not create the String
objects themselves.
What is the initial value of each element in an array of objects when it is declared in Java?
Initially, each element in an array of objects holds a null
reference.