1/33
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
Array objects:
Data structures consisting of related data items of the same type. Make it convenient to process related groups of values. Remain the same length once they are created.
Variable-length argument lists:
Can create methods are with varying numbers of arguments.
ArrayList collection:
Similar to arrays. Dynamic resizing; resize as necessary to accommodate more or fewer elements.
Array:
Group of variables (called elements) containing values of the same type. Arrays are objects so they are reference types. Elements can be either primitive or reference types.
Refer to a particular element in an array:
Use the element’s index. Array-access expression, the name of the array followed by the index of the particular element in square brackets, [].
The first element in every array has an
index of zero.
The highest index in an array is
one less than the number of elements in the array.
Array names follow the same conventions as
other variable names.
An index must be a
nonnegative integer. Can use an expression as an index.
An indexed array name is an
array-access expression. Can be used on the left side of an assignment to place a new value into an array element.
Every array object knows its own
length and stores it in a length instance variable. length cannot be changed because it’s a final variable.
an index must be an
int value or a value that can be promoted to an int.
Array object creation:
Created with keyword new. You specify the element type and the number of elements in an array-creation expression, which returns a reference that can be stored in an array variable.
Declaration and array-creation expression for an array of 12 int elements
int[] c = new int[12];
Can be performed in two steps as follows:
int[] c; // declare the array variablec = new int[12]; // creates the array
In an array declaration, square brackets following a type indicate that
a variable will refer to an array (i.e., store an array reference).
When an array is created, each element of the array receives a
default value; Zero for the numeric primitive-type elements, false for boolean elements and null for references.
When the element type and the square brackets are combined at the beginning of the declaration,
all the identifiers in the declaration are array variables. For readability, declare only one variable per declaration.
Every element of a primitive-type array contains a value of the
array’s declared element type. Every element of an int array is an int value.
Every element of a reference-type array is a reference to an
object of the array’s declared element type. Every element of a String array is a reference to a String object.
Array initializer:
A comma-separated list of expressions (called an initializer list) enclosed in braces. Used to create an array and initialize its elements. Array length is determined by the number of elements in the initializer list. int[] n = {10, 20, 30, 40, 50}; Creates a five-element array with index values 0–4. Compiler counts the number of initializers in the list to determine the size of the array.
final Keyword:
used to restrict changes and make code more secure and predictable. It can be applied to variables, methods, and classes to prevent modification, overriding, or inheritance. This helps in creating constant values, stable methods, and immutable classes.
final variables must be initialized before they are used and cannot be
modified thereafter. An attempt to modify a final variable after it’s initialized causes a compilation error. An attempt to access the value of a final variable before it’s initialized causes a compilation error.
constant variables are also called
named constants. they often make programs more readable.
Many programs present data to users in a
graphical manner. Numeric values are often displayed as bars in a bar chart. Longer bars represent proportionally larger numeric values.
A simple way to display numeric data is with
a bar chart that shows each numeric value as a bar of asterisks (*).
Using the Elements of an Array as Counters:
Sometimes, programs use counter variables to summarize data, such as the results of a survey.
Array frequency must be large enough to store six counters.
We use a seven-element array in which we ignore frequency[0]. More logical to have the face value 1 increment frequency[1] than frequency[0].
If a piece of data in the responses array is an invalid value, such as 14, the program attempts to add 1 to frequency[14], which is outside the bounds of the array. Java doesn’t
allow this. JVM checks array indices to ensure that they are greater than or equal to 0 and less than the length of the array—this is called bounds checking. If a program uses an invalid index, Java generates a so-called exception to indicate that an error occurred in the program at execution time.
An exception indicates a problem that occurs while a program executes. The name “exception” suggests that the problem occurs
infrequently, if the “rule” is that a statement normally executes correctly, then the problem represents the “exception to the rule.” Exception handling helps you create fault-tolerant programs that can resolve (or handle) exceptions. When the JVM or a method detects a problem, such as an invalid array index or an invalid method argument, it throws an exception, that is, an exception occurs.
To handle an exception, place any code that might throw an exception in a
try statement. The try block contains the code that might throw an exception.
The catch block contains the code that
handles the exception if one occurs. You can have many catch blocks to handle different types of exceptions that might be thrown in the corresponding try block.
When the program encounters the invalid value 14 in the responses array, it attempts to add 1 to frequency[14], which is outside the bounds of the array, the frequency array has only six elements (with indexes 0–5). Because array bounds checking is performed at execution time, the JVM generates an
exception—specifically an ArrayIndexOutOfBoundsException to notify the program of this problem. At this point the try block terminates and the catch block begins executing—if you declared any local variables in the try block, they’re now out of scope.
The catch block declares an exception parameter (e) of type (IndexOutOfRangeException). Inside the catch block, you can use
the parameter’s identifier to interact with a caught exception object.
The exception object’s toString method returns the error message that’s implicitly stored in the
exception object. The exception is considered handled when program control reaches the closing right brace of the catch block.