Chapter 9 - Arrays Flashcards
What is a class?
It stores a group of related data, and it stores the methods that operate on that data.
What is an Array?
An Array is a limited version of a class.
- Similar to a class it stores related data, but it does not store methods.
- Also, unlike a class and Arrays data must all be the same type.
What are Arrays composed of?
Each part of an Array is called an element.
How does a class access one of its members?
Using dot notation
How does an Array access one of its elements?
Using square brackets around an index
I.E. [x]
How are index values determined in an Array?
The first index number starts at 0 and the final number is the number of elements in an array minus 1.
What command would you type to change the value of an array element?
The name of the Array = new value
I.E: phonelist[0] = 555-55-5555
(Name of the array and element number) = new Phone number
What command would you type to print out a phone number?
System.out.println(Arrayname and element number);
I.E: System.out.println(phoneList[1]);
What is an Array considered?
A Variable
How do you declare and Array as a variable?
“element-type”[] “array-variable”;
Examples:
int[] ids;
double[] workHours;
String[] names;
What are Arrays in Java?
Objects
After declaring an Array as a variable, what must be done to create the Array as an object?
You must use the “new” operator.
“array-variable” = new “element-type”[array-size];
Examples:
long[] phoneList; (this establishes the array variable)
phoneList = new long[10]; (this creates the array)
How do you combine and arrays declaration as a variable and creation?
“element-type”[] “array-name” = new “element-type”[length of array];
Example: long[] phoneList = new long[10];
How would you write a statement that declares, creates and assigns a 100-element array that stores book titles?
long[] bookTitles = new long[100];
What is an array initializer?
A single statement made up of an array declaration, creation and {} assignment.
Example: “element-type”[] “array-name” = {“element-values-list”};
String[] students = {“Hamoud”, “Lee”, “Brandon”};
*Note that the array is created without the “new” operator.
If an Array initializer is used, what is the size of the array?
The number of elements in the initialization list.
For example: {“John”, “Michael”, “Tom”}; would be an array size of 3
What does it mean when an array is instantiated?
It means the elements are assigned default values according to the array data type.
Basically, if an array is not given an assigned value instead of giving a complier error it will just assign default values to the array.
What are the default values when an array is instantiated? (They’re also the default values for instance variables and class variables)
Example:
float[] gpas = new float[1000];
String[] states = new String[50];
Default values:
This would return 0.0 1000 times
This would return null 50 times