Fiveable
Fiveable

Array

Definition

An array is a fixed-size collection of elements of the same type stored in contiguous memory locations. It allows efficient access to individual elements using an index.

Analogy

Imagine an array as rows of lockers in your school hallway, where each locker has its own number. You can easily find any item by knowing its locker number without searching through every single locker.

Related terms

Indexing: Indexing refers to accessing individual elements within an array by specifying their position using an index value.

Length/Size: The length or size represents the total number of elements present in an array.

Multidimensional Array: A multidimensional array is an array with multiple dimensions (rows and columns) that allows storing data in tabular form.

"Array" appears in:

Practice Questions (20+)

  • What does it mean to traverse an array?
  • If an array has eight elements and we used the for loop header `for (int i = 7; i >= 0; i++)` to traverse it in reverse order, what is the result?
  • If we want to traverse from the fifth to the eleventh element of an array, inclusive, what should the for loop header be?
  • How would you traverse through an array in reverse order?
  • How would you traverse through an array, starting from the fourth element and all the way to the end? Assume that the array has more than four elements.
  • What happens if you access an index that is outside of the bounds of the array during traversal?
  • Suppose the array `arr` has a length of 7. If we want to traverse through its first 4 elements, which for loop header should be used?
  • If we want to traverse from the second to the seventh element of an array, inclusive, what should the for loop header be?
  • If an array has four elements and we used the for loop header `for (int i = 0; i <= 4; i++)` to traverse it, what is the result?
  • Suppose the array `arr` has 5 elements. If we want to traverse through its first 2 elements, which for loop header should be used?
  • What happens when we attempt to modify the elements of an array using the enhanced for loop?
  • How can we rewrite the following for loop as an enhanced for loop? int[] array = {5, 4, 10, 23, 15} for (int i = 0; i < array.length; i++) { System.out.println(array[i]); }
  • What does the following code do? public static int[] codeBlock(int[] array) { int firstItem = array[0] for (int i = 0; i < array.length - 1; i++) { array[i] = array[i+1]; } array[array.length - 1] = firstItem; return array; }
  • What is returned by the following code if we pass in the array [65, 78, 29, 71]? public static int codeBlock(int[] array) { int a = 0; int c = 0; for (int i = 0; i < array.length - 1; i++) { int b = 1; for (int j = i + 1; j < array.length; j++) { if (array[j] == array[i]) { b++; } } if (b > c) { a = array[i]; c = b; } } return a; }
  • What is returned by the following code if [3, 1, 10, 8] is passed in as the argument? public static int maximum(int[] array) { int maxValue = array[0]; for (int number: array) { if (number < maxValue) { maxValue = number; } } return maxValue; }
  • If we call the following method on the array [10, 9, 3, 12, 5], what is returned? public static int[] codeBlock(int[] array) { int[] newArray = new int[array.length]; for (int i = 0; i < array.length; i++) { newArray[i] = array[array.length - i - 1]; } return newArray; }
  • What does the following method return for the array [16, 7, 20, 35, 36, 7]? public static boolean codeBlock(int[] array) { for (int i: array) { if (15 <= i <= 30) { return false; } } return true; }
  • After calling the following method on the array [210, 303, 198, 762, 135], what is returned? public static int codeBlock(int[] array) { int x = array.length; for (int y: array) { if (y % 2 == 0) { x--; } } return x; }
  • Which of the following correctly implements the linear search algorithm for an array? Assume each code block is written inside a method where val is a parameter taking in the value being searched for.
  • What is the return value of the linear search algorithm if the first element of the array is equal to the value being searched for?


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.

AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.