Chapter 8 Flashcards
What are Arrays? What’s the difference with ArrayLists?
An Array is a programming language construct used to organize a list of objects and values of a primitive data type.
The are two important differences with ArrayLists:
- An ArrayList can only store objects. An Array can store primitive data types
- An Array is not dynamic, it doesn’t shrink when elements are removed.
An Array element has a special syntax. What are some of its commands?
int[] scores = new int[10]
declares an Array called scores of type int and length 10 (with index going from 0 to 9).
scores[2]
returns the value at index 2 in the array
How do you initialize an array? What must be done carefully?
Example:
for (int index = 0; index < 15; index++) {
list[index] = index * 2;
}
It’s important that the loop doesn’t try to add an element to an index that is not defined in the array. In other words, we must not go over the size of the array.
What is the name of the constant that hold the size of an array?
this.length stores the size of the array. Not the last index of the array.
What does the following code do?
…
count[ i - 3 ]++;
…
This code increments by one the value stored inside the array “count” at index “i-3”.
What are the two formats with which an array is declared? Which one is preferred?
double[] prices;
double prices[];
The first is usually preferred as more readable.
What is an initializer list?
It’s a way of declaring the values present inside an array (without the need of even writing the size of it)
->
the initializer list can only be used in the declaration.
int[] units = {147, 323, 89, 933, 540, …}
Can elements of an array hold object references?
Yes, they can. Since an array is already an object, this means that an array is holding a reference to a space in memory that then stores other references to other objects.
This means that for each object reference inside an array. The array indicates an address in the memory that then indicates another address.
[] array V [][][][][][] elements of the array V [][][][][][] objects pointed by each of the elements.
What does the signature of the main method indicates?
The main method (main(String[] args) indicates that it takes an Array of String as parameters.
These values come from command-line arguments that are provided when the interpreter is invoked.
With the command:
(File name is NameTag): public static void main(String[] args) { System.out.println(); System.out.println(args[0]); System.out.println("My name is " + args[1]); }
> is command line
Output:
> java NameTag Howdy John
Howdy
My name is John
What is the syntax for declaring methods with a variable length parameter list?
Can it accept other parameters?
Other variable length parameters?
Example:
public double average (int … list)
int = element type
… = indicates that the number of parameters is variable
list = name of the array in which the values will be stored in.
Yes, the definite parameters must come before the one with a variable length.
No, only one parameter with variable length is acceptable.
How many dimensions can an array have?
What is a ragged array?
An array can have infinitely dimensions: int[] 1 dimension int[][] 2 dimensions int[][][] 3 dimensions int[][][][][][][][][][][]...
A ragged array is a specific multidimensional array that inside a dimension contains arrays with different lengths.
How do you create a polygon in JavaFX?
Example: double[] hullPoints = {200, 25, 240, 23, 153, ...}; Polygon name = new Polygon(hullPoints);