JAVA 2

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/35

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

36 Terms

1
New cards

loop

a program construct that repeatedly executes the loop's statements (known as the loop body) while the loop's expression is true; when the expression is false, execution proceeds past the loop.

2
New cards

iteration

Each time through a loop's statements

3
New cards

while loop

a program construct that repeatedly executes a list of sub-statements (known as the loop body) while the loop's expression evaluates to true. Once entering the loop body, execution continues to the body's end, even if the expression would become false midway through.

4
New cards

for loop

is a loop with three parts at the top: a loop variable initialization, a loop expression, and a loop variable update. A for loop describes iterating a specific number of times more naturally than a while loop.

5
New cards

for loop structure

for (initialExpression; conditionExpression; updateExpression) {
  // Loop body
}
// Statements after the loop

6
New cards

increment operator, statement i = i + 1

++i

7
New cards

decrement operator, means i = i - 1

--i

8
New cards

for

Number of iterations is computable before the loop, like iterating N times.

9
New cards

while

Number of iterations is not (easily) computable before the loop, like iterating until the input is 'q'.

10
New cards

Iterating through a string with a for loop

A programmer commonly iterates through a string, examining each character.

11
New cards

Array

s an ordered list of items of a given data type. A special variable having one name, but storing a list of data items, with each item being directly accessible. Some languages use a construct similar to an array called a vector. Each item in an array is known as an element.

12
New cards

element

each item in an array.

13
New cards

Array declaration and allocation

dataType[] arrayName = new dataType[numElements];

the declaration uses [] symbols after the data type to indicate that the variable is an array reference. 

14
New cards

array reference

can refer to arrays of various sizes.

15
New cards

new keyword

creates space in memory to store the array with the specific number of elements.

16
New cards

array reference variable

is assigned to refer to that newly allocated array. Ex: int[] gameScores = new int[4]; declares an array reference variable gameScores, allocates an array of four integers, and assigns gameScores to refer to the allocated array.

17
New cards

array reference declaration and array allocation

A programmer can declare an array reference variable without allocating the array at that time and later assign the variable with an allocated array.

18
New cards
19
New cards

example of array reference declaration without allocation

int[] gameScores;

20
New cards

good practice

If the size of an array is known, good practice is to combine the array reference variable declaration with the array allocation.

21
New cards

the index of an array is

an expression

22
New cards

An array’s index must be

an integer type

23
New cards

the array index can not be

a floating type.

24
New cards

Example: Declare and initialize an array named myVals that stores 10 elements of type int with default values.

int[] myVals = new int[10];

25
New cards

loops and arrays

are a key advantage, for example, store the values and then print them.

26
New cards

array’s length property

accessed by appending .length after the array's name, indicates the number of array elements.

27
New cards

example of an array’s length

userVals.length

28
New cards

array initialization

An array's elements are automatically initialized to default values when using the new keyword to initialize the array reference.

29
New cards

array initalization details

The default value for elements of integer and floating-point data types is zero, and the default value for boolean elements is false.

30
New cards

array specific initialization

non-default values by specifying the initial values in braces {} separated by commas. Ex: int[] myArray = {5, 7, 11}; creates an array of three integer elements with values 5, 7, and 11. Does not require the keyword new.

31
New cards

Iterating through an array using loops

commonplace and is an important programming skill to master. 

32
New cards

Iterating through an array using loops example

// Iterating through myArray
for (i = 0; i < myArray.length; ++i) {
   // Loop body accessing myArray[i]
}

Note that index variable i is initialized to 0, and the loop expression is i < myArray.length rather than i <= myArray.length. If N were 5, the loop's iterations would set i to 0, 1, 2, 3, and 4, for a total of 5 iterations. The benefit of the loop structure is that each array element is accessed as myArray[i] rather than the more complex myArray[i - 1].

33
New cards

common error

try to access an array with an index that is out of the array's index range. Ex: Trying to access highScores[8] when highScores's valid indices are 0-7.

34
New cards

Swapping two variables

means to assign y's value to x, and x's value to y. If x is 33 and y is 55, then after swapping x is 55 and y is 33.

35
New cards

common swapping method

to use a temporary variable

36
New cards

temporary variable

is a variable used briefly to store a value. To understand the intuition of such temporary storage, consider a person holding a book in one hand and a phone in the other, wishing to swap the items. The person can temporarily place the phone on a table, move the book to the other hand, then pick up the phone.