Array Flashcards

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/63

encourage image

There's no tags or description

Looks like no tags are added yet.

64 Terms

1
New cards

What is an array?

Ordered list of items of a given data type

2
New cards

What is an element?

Each item in an array

3
New cards

What is an array index?

Each element’s location number

4
New cards

What do array indexes start with?

0

5
New cards

How to assign an element to an array index?

arrayName[#] = value

6
New cards

What is the last element’s index?

NumElements - 1

7
New cards

How does array reference variable declaration and array allocation look like?

dataType[] arrayName = new dataType[numElements];

8
New cards

What does the new keyword do?

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

9
New cards

How to declare array reference first and then allocate array?

dataType[] arrayName;

arrayName = new dataType[numElements];

10
New cards

What is the default value all int array elements are initialized to?

0

11
New cards

How to find a certain element in a list? (for example the 7th element)?

arrayName[n - 1]

ex. arrayName[7 -1]

12
New cards

What function accesses the amount of elements in an array?

arrayName.length

13
New cards

What is the default value for int and floating-point data type arrays?

0

14
New cards

What is the default value for boolean data type arrays?

False

15
New cards

How to set non-default values to an array?

dataType[] arrayName = {value, value, value};

16
New cards

What is a common loop structure when iterating through arrays?

for(i = 0; i < arrayName.length; ++i)

17
New cards

When using a loop to determine max value in array, how to set up max variable?

max variable initialized before loop, initialize to one of the elements of the loop

18
New cards

What happens if you try to access an array with index that is out of the array’s index range?

Results in runtime error that causes program to terminate

19
New cards

How to pass array reference to a method?

public static void methodName(arrayDataType[] arrayReference)

20
New cards

How is an array stored in a method?

Elements of the array stored indirectly in the heap and the reference to the array is stored in the stack frame

21
New cards

What will happen when you change an array in a method and why?

It will also change the array. This happens because Java passes a copy of the reference to the method. It has the same reference to the same array so altering it would change the array just as a regular reference would.

22
New cards

Can you change an array reference inside a method?

No

23
New cards

What can be changed for an array inside a method and what cannot be changed?

The elements can be changed.

The array length cannot be changed.

24
New cards

Can you swap array references in a method?

No

25
New cards

What happens when you assign array parameter in method?

It only updates local reference (cnages what it is referencing to, so it is not referencing the array anymore) and doesn’t change the array reference

26
New cards

What are related arrays?

Element at an index of one array corresponds to the other array element at the same index

27
New cards

What is a common way to swap two variables values?

Use temporary variable that briefly stores a value

temp = x

x = y

y = temp

28
New cards

Can you use a loop to modify elements of an array?

Yes!

29
New cards

What would be a basic loop to copy the elements of one array to another array?

for(int i = 0; i < arrayName.length; ++i){

copiedArray[i] = arrayName[i];

}

30
New cards

How many items can a method return?

Only one item

31
New cards

How to modify an array’s size in a method?

  1. Create new array with the modified size

  2. Copy existing array elements to new array

  3. return new constructed array

  4. In main, assign original array reference to the new array returned by method

    1. ex. arrayName = methodName(arrayName);


32
New cards

What are three common purposes of a method that uses arrays?

  1. Calculate a value based on array, leave array unchanged

  2. Change array’s contents but not size

  3. Create new array or change arrays size


33
New cards

When creating a method that changes array’s contents but not the size, what is the return type?

Void return type

34
New cards

When creating a method that creates a new array or changes array’s size, what is the return type? How would the method header look like?

return array reference

ex. public static dataType[] methodName()

35
New cards

In a method, when you need an array to calculate something but you want to avoid modifying the array what can you do?

Make a copy of the array and perform the calculation using the copy

36
New cards

What is an Arrays method that copies an array? What would be the syntax to successfully copy to another array reference?

dataType[] arrayCopy = Arrays.copyOf(arrayName, arrayName.length);

37
New cards

What would be the strategy to reversing an array?

Make a series of swaps. Swap first element with last element, second item with second to last item, etc.

38
New cards

What would a standard reversal array code look like?

for (i = 0; i < (userVals.length / 2); ++i) {
         tempVal = userVals[i];                           
         userVals[i] = userVals[userVals.length - 1 - i];
         userVals[userVals.length - 1 - i] = tempVal;     
      }


39
New cards

How to declare a two-dimensional array?

int[][] arrayName = new int[R][C]

R = # of rows

C = # of columns

40
New cards

How to find total elements of a two-dimensional array?

R * C

41
New cards

What does this code access and assign value to? (row and column) -

numVals[4][2] = 99;

Assigned to the 5th row and 3rd column

(assuming the actual row number starts at 1)

42
New cards

How to initialize a two-dimensional array during declaration?

int[] [] arrayName = {

{value, value, value}, //notice the comma!!

{value, value, value}

};

43
New cards

What is the standard code for using a loop to iterate through 2-dimensional array?

for (i = 0; i < arrayName.length; i++){

for (j = 0; j < arrayName[i].length; j++){

arrayName[i][j] = scnr.nextInt();

}

}

44
New cards

What is an enhanced for loop/ for each loop?

A for loop that iterates through each element in an array

45
New cards

What does an enhanced for loop declare?

It declares a new loop variable

46
New cards

How is an enhanced for loop formatted?

for(dataType loopVariable : arrayName) {

47
New cards

What is the the loop variable assigned to in an enhanced for loop?

Loop variable assigned with each successive element of an array

i.e. The first iteration will assign loopVariable with the first element of arrayName, second iteration will assign loopVariable with second element, etc.

48
New cards

How are elements accessed in for each loop?

Only accessed forward from first element to last element

49
New cards

When you use an enhanced for loop, is the array modified?

No, can’t modify array with loop variable

50
New cards

What is a perfect size array?

Array where number of elements is exactly equal to the memory allocated

51
New cards

How is a perfect size array created?

Created when you declare and initialize an array at the same time

52
New cards

When is it appropriate to use perfect size arrays?

When the size of the array is constant and won’t change

53
New cards

What is an oversize array?

Array where the number of elements used is less than or equal to the memory allocated

54
New cards

What variable do you need for an overssize array?

Need variable for how many array elements are currently used, the current size

55
New cards

When is an oversize array appropriate?

When size of the array is not known in advance or it varies over time

56
New cards

What are the three variables typically used for oversize array?

Array reference, current size, and constant variable for the array capacity (which is put into array reference size)

57
New cards

When using methods for oversized arrays, what must be used as parameters?

array reference and current size

58
New cards

If a method returns an array reference, what type of array must it be an why?

Must be perfect size array because an oversize array must return the current size of array (and method can only return one thing)

59
New cards

If a method has no parameter for the size of the array, what array must it be and why?

Must be perfect size array. Oversize arrays need this parameter because it must be updated in the method

60
New cards

For printing an oversize array, what is the expression of the for loop?

i < arraySize

(use the variable for size not .length)

61
New cards

When updating an oversize array in a method, what should you first check? What would the code look like?

First check if unused element is available in array reference

if(currentSize == arrayRef.length) → return currentSize

62
New cards

When adding array element to an oversize array, what would the assigning process look like?

arrayRef[currentSize] = element;

++currentSize

63
New cards

When adjusting the size in a method, what should you do to the size variable in main?

Assign it to the method!!

ex. currentSize = methodName(…)

64
New cards

For an oversize array, what is the return type in a method?

Int

(because it returns the array size)