Array

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

1/57

encourage image

There's no tags or description

Looks like no tags are added yet.

58 Terms

1
New cards

What are the essential properties of a Java array?

Ordered, zero-indexed collection of one type, fixed physical size after creation, can store primitives or object references, valid indexes from 0 to arr.length-1.

2
New cards

What is the length and valid indexes of 'int[] arr = new int[5];'?

Length = 5; valid indexes = 0-4.

3
New cards

What default values are assigned in a newly created int array?

Every int starts with 0.

4
New cards

What is the difference between 'int[] a, b;' and 'int a[], b;'?

'int[] a, b;' makes both variables arrays, while 'int a[], b;' makes 'a' an array and 'b' a normal int.

5
New cards

What does an array initializer list do?

'int[] coins = {1,5,10,25};' creates and initializes the array with length determined by the initializer.

6
New cards

Why can array values change even though its size cannot?

The array size is fixed, but the values in the positions can be reassigned.

7
New cards

What is array aliasing?

'int[] b = a;' copies the reference, both variables point to the same array object.

8
New cards

What is the output of 'System.out.println(a[0]);' after 'int[] a={1,2,3}; int[] b=a; b[0]=9;'?

'9', as 'a' and 'b' refer to the same array.

9
New cards

What does traversing an array mean?

Visiting every element using a loop.

10
New cards

Why does 'for (int x : arr) { x *= 2; }' not modify the original array?

x is a copy, reassigning it does not change arr.

11
New cards

What is the standard pattern for summing values in an array?

Initialize sum = 0, traverse, add each value to sum.

12
New cards

How do you count occurrences of a target in an array?

Set count = 0, traverse array, increment count when element matches the target.

13
New cards

How do you check if a target is present in an array?

Search until found; if not found by the end, report not found.

14
New cards

What is the method to find the last occurrence of a target in an array?

Search backwards from the end of the array.

15
New cards

Why must a loop comparing 'arr[i]' with 'arr[i+1]' use 'i < arr.length-1'?

To avoid out of bounds access with 'arr[i+1]'.

16
New cards

What does 'if (arr[i] > arr[i+1]) count++;' count?

Counts adjacent descents in the array.

17
New cards

What is the final array after 'arr[i] = arr[i] + arr[i-1]' for '{3,8,1,9,4}'?

Final array: '{3,11,12,21,25}'.

18
New cards

Why is 'int max = 0;' unsafe for finding max in an array?

If all values are negative, max will incorrectly remain 0.

19
New cards

What is the correct pattern for checking if every element satisfies a condition?

Return false immediately if one element violates condition, true after checking all.

20
New cards

What is the integer-division trap when calculating the array average?

Using int division truncates the fractional part. Cast to double to preserve it.

21
New cards

What is the physical size of an array?

The number of allocated positions, given by 'arr.length'.

22
New cards

What are physical and logical sizes of an array with length 1000 but 50 used?

Physical size = 1000; logical size = 50.

23
New cards

How do you safely add an element to an array using logical size?

Use 'if (lsize < arr.length) { arr[lsize] = value; lsize++; }'.

24
New cards

What does logical removal of the last element from an array involve?

Decrement the logical size, e.g., 'lsize--;'. The physical size remains unchanged.

25
New cards

How do you remove an element from the middle of an array?

Shift later elements left, decrement logical size.

26
New cards

What are the main differences between an array and an ArrayList?

Array: fixed size, can store primitives; ArrayList: resizable, stores objects.

27
New cards

Where does ArrayList fit in Java Collections?

ArrayList is a type of List within Java Collections.

28
New cards

Why is 'ArrayList' invalid but 'ArrayList' valid?

ArrayList stores objects, not primitives; Integer is the wrapper class.

29
New cards

What is autoboxing in Java?

Java converts a primitive into its wrapper object automatically.

30
New cards

What are the three size expressions to know?

Array: arr.length; String: str.length(); ArrayList: list.size().

31
New cards

What does 'list.add(value)' do?

Appends value to the end and increases size by 1.

32
New cards

What does 'list.add(index,value)' do?

Inserts value at index and shifts elements right.

33
New cards

What does 'list.set(index,value)' do?

Replaces the element at index without changing size.

34
New cards

What does 'list.get(index)' do?

Returns the element at index without changing the ArrayList.

35
New cards

What does 'list.remove(index)' do?

Removes and returns element at index, shifting later elements left.

36
New cards

What is the final state after 'add(0,

add(1,'D'); Final list: '[C,D,A,B]'. First insertion makes '[C,A,B]', then inserting D shifts A and B right.

37
New cards

What happens when 'int x=list.remove(1); int y=list.set(1,5);'?

x=20, y=30, final list = '[10,5,40]'. Remove returns 20, then 30 is replaced with 5.

38
New cards

What is the output after 'list.add(0, list.remove(list.size()-1));'?

'[Howard, Sheldon, Mickey, Raj]'. Remove last, insert at index 0.

39
New cards

Why is 'remove(1)' tricky in 'ArrayList'?

Index refers to a position; it may remove a different element than expected.

40
New cards

What is the major gotcha when removing from an ArrayList while traversing forward?

Removal shifts later elements left, may skip the next element.

41
New cards

What are three safe strategies for removing multiple matching ArrayList elements?

1) Forward for-loop with decremented index. 2) While-loop that increments only if no removal occurs. 3) Traverse backward.

42
New cards

Why is backward traversal good for repeated ArrayList removal?

No unprocessed elements are skipped since higher indexes have been processed.

43
New cards

What’s the danger in storing original ArrayList size before removal?

If the size decreases, using the old size can lead to index errors.

44
New cards

What happens when you modify an ArrayList inside an enhanced for loop?

Throws a ConcurrentModificationException.

45
New cards

How should String contents be compared in array/ArrayList algorithms?

Use '.equals()' for comparing contents.

46
New cards

What should 'countMatches(ArrayList words, String target)' do?

Count matched strings using '.equals()'.

47
New cards

How should 'removeShort(ArrayList words, int minLen)' be safely implemented?

Traverse backward, removing short strings without skipping.

48
New cards

What does 'isSorted(int[] arr)' do?

Checks if the array is sorted by comparing adjacent pairs.

49
New cards

Why is returning true inside the loop wrong for 'isSorted'?

One correct pair does not prove the entire array is sorted.

50
New cards

What is the method to create a reversed copy of an array?

Create a new array and assign values from the original in reverse.

51
New cards

Why is an ArrayList a good return type for a method collecting scores above average?

It can grow dynamically, accommodating an unknown varying count.

52
New cards

How should 'aboveAverage(int[] scores)' conceptually work?

Calculate average, create a list, add scores greater than average, return.

53
New cards

What is the result of computing consecutive differences in an array?

The last element minus the first element, canceling interior values.

54
New cards

What exceptions to recognize for invalid indexes?

'ArrayIndexOutOfBoundsException' for arrays; 'IndexOutOfBoundsException' for ArrayLists.

55
New cards

What happens during indexed ArrayList add/remove?

'add(index,x)' shifts elements right; 'remove(index)' shifts elements left.

56
New cards

What is the first step when tracing complicated ArrayList code?

Rewrite the list after every change and recalculate indexes and size.

57
New cards

What should you do first when tracing complicated array code?

Check loop bounds and whether the array is being modified.

58
New cards

What are the highest-value gotchas to remember?

Last index = length-1, aliasing, enhanced-for copies, mutation effects, add vs set, removal shifts, skipping elements, String equality, Integer vs int, integer division issues.