knowt ap exam guide logo

Arrays

Arrays Intro

  • Arrays- A data structure that makes it easier to handle similar types of data.

  • Arrays can store primitive or object data types.

  • Every separate piece of data is referred to as an element, and an index is used to identify the specific location of this element.

  • Arrays can be used to make the run-time of code a lot more efficient.

    Primitives and Objects

  • Primitives and Objects play a huge role in setting up an array. Use these steps to set up your array effectively.

  • The best way to teach this concept is by example, which is what we will walk through.

  • Decide on the types of data that will be stored in the array.

int [] <identifier> = new int [<array size>];
  • If you already know the data types being stored and the data values, you can instantiate or set up the array using an initializer list.

int [] <identifier> = {<data1>, <data2>, <datan>}
  • Note: Square brackets are used to show that an array is present. While curly brackets indicate an initializer list, just like the examples above.

    • Ex: Create an array called testGrades that stores 5 test grades.

int[] testGrades = new int [5];
  • As mentioned before each data point will be stored with its unique index.

  • To create an array with data, you need to instantiate it.

    • Ex: If you score a 95 on the first test

testGrades[0] = 95;
  • Create an array with all the 5 scores at once:

int [] testScores = {90, 80, 100, 85, 92};
  • You can also reassign a value at an index in case you make a mistake entering the numbers.

testGrades[0] = 98;
  • You can also use arrays to perform integer operations, and display the value.

  • You can also traverse through the array and change all of the values.

  • Ex: Your teacher decides to deflate your grades 🙁 so you have to subtract 2 points from each grade

    for( int index = 0; index < testGrades.length ; index++)

    testGrades[index]-=2;
  • ArrayindexOutOfBoundsException occurs when the indexes go out of bonds from what the array is actually set to.

  • Note: The AP Exam will require you to create, traverse and modify arrays. This is often what the free response questions are about!!!

  • Remember to make sure that you assign the correct values for each element found in the array. Otherwise, you may get a result that you weren’t expecting.

  • A key tool to use when going through arrays is to use an enhanced-for-loop.

    • Ex: Calculate the average of 5 test grades

int total = 0, len = testGrades.length;

double average;

for (int index = 0; index < len; index++)

total += testGrades[index];

average = (double) total/len;
  • Ex: Calculate the average of 5 test grades using an enhanced-for-loop

    int total = 0, len = testGrades.length;

    double average;

    for (int grade: testGrades)

    total += grade;

    average = (double) total/len;
  • In enhanced-for-loop specifically, a declared array or an ArrayList needs to be referenced after the colon. The loop then iterates for each element found in the list all the way from index 0 to the last index(length-1).

  • Note on enhanced for loops: The enhanced for loop provides a variable that will hold each element of an array. it doesn’t provide the index of an element. You will need to include code to keep track of indexes if it’s necessary for your code.

  • No variable is used to refer to the current index.

  • Instead, the element located at the current index is stored as the variable declared before the colon.

  • Overall, the enhanced for loop stores each element in increasing order by the index of the array testGrades as grade. It then adds grade to the total.

  • The reason why enhanced for loops are used is because the risk of getting an ArrayIndexOufOfBoundsException when it traverses the array is less, and it shortens the notation of elements.

  • Note: FRQs on the AP Exam test your ability to write code that doesn’t go out of bounds. The AP exam will require you to create, traverse and modify your arrays.

  • It’s important to pay attention to the data that you are including in your arrays. Any unassigned value will just default to 0.

  • Since each element is an object in an array, the default value for the object will be null. Any operations performed on null will result in a NullPointerException because null isn’t a valid object!

  • Arrays are useful to organize and manipulate objects. Just make sure that the array is full of objects so that it produces the desired results.

Searches

  • In many cases when you have data stored in an array you may want to search the array for specific values.

  • It makes it easier to pinpoint values in some cases instead of just using loops.

  • The most common searches are:

  • Sequential searches- These search through the data one by one in order, and take a long time to execute.

  • Binary searches- These search through the data for the desirable value by dividing it into half each time until the desired value is found.

  • The 2 searches above are referred to as search algorithms.

  • Note: On the AP Exam you will be asked to recognize these search algorithms and understand how they function.

Sorts

  • Sorting Algorithms- They take data in an array, and rearrange it into a particular order. They are very powerful because they can accomplish tasks faster, and can make your programs even more efficient.

  • Selection sort: This sort searches and swaps. Once it finds the lowest value, it will swap its position in the array with the data at index 0. The first element is now sorted. The process repeated for index 1. The rest of the array will be searched for the lowest value and is swapped with the data that is found at index 1.

  • Insertion sort: It compares the first 2 elements, and depending on the outcome of this it inserts the second value in front of the first value into index 0, which moves the first value to index 1. The first 2 elements are sorted. Then the third element is checked and this process continues until it’s entirely sorted.

  • Merge Sort: This type of sort uses recursion. An array is split into 2 pieces. The piece is sorted. The 2 sorted pieces are merged together into one sorted list. Then in order to sort each of the 2 pieces, the same method is used again until it is fully sorted.

  • **Recursion- Technique that uses a method to call itself. ***View Unit 12

Arrays

Arrays Intro

  • Arrays- A data structure that makes it easier to handle similar types of data.

  • Arrays can store primitive or object data types.

  • Every separate piece of data is referred to as an element, and an index is used to identify the specific location of this element.

  • Arrays can be used to make the run-time of code a lot more efficient.

    Primitives and Objects

  • Primitives and Objects play a huge role in setting up an array. Use these steps to set up your array effectively.

  • The best way to teach this concept is by example, which is what we will walk through.

  • Decide on the types of data that will be stored in the array.

int [] <identifier> = new int [<array size>];
  • If you already know the data types being stored and the data values, you can instantiate or set up the array using an initializer list.

int [] <identifier> = {<data1>, <data2>, <datan>}
  • Note: Square brackets are used to show that an array is present. While curly brackets indicate an initializer list, just like the examples above.

    • Ex: Create an array called testGrades that stores 5 test grades.

int[] testGrades = new int [5];
  • As mentioned before each data point will be stored with its unique index.

  • To create an array with data, you need to instantiate it.

    • Ex: If you score a 95 on the first test

testGrades[0] = 95;
  • Create an array with all the 5 scores at once:

int [] testScores = {90, 80, 100, 85, 92};
  • You can also reassign a value at an index in case you make a mistake entering the numbers.

testGrades[0] = 98;
  • You can also use arrays to perform integer operations, and display the value.

  • You can also traverse through the array and change all of the values.

  • Ex: Your teacher decides to deflate your grades 🙁 so you have to subtract 2 points from each grade

    for( int index = 0; index < testGrades.length ; index++)

    testGrades[index]-=2;
  • ArrayindexOutOfBoundsException occurs when the indexes go out of bonds from what the array is actually set to.

  • Note: The AP Exam will require you to create, traverse and modify arrays. This is often what the free response questions are about!!!

  • Remember to make sure that you assign the correct values for each element found in the array. Otherwise, you may get a result that you weren’t expecting.

  • A key tool to use when going through arrays is to use an enhanced-for-loop.

    • Ex: Calculate the average of 5 test grades

int total = 0, len = testGrades.length;

double average;

for (int index = 0; index < len; index++)

total += testGrades[index];

average = (double) total/len;
  • Ex: Calculate the average of 5 test grades using an enhanced-for-loop

    int total = 0, len = testGrades.length;

    double average;

    for (int grade: testGrades)

    total += grade;

    average = (double) total/len;
  • In enhanced-for-loop specifically, a declared array or an ArrayList needs to be referenced after the colon. The loop then iterates for each element found in the list all the way from index 0 to the last index(length-1).

  • Note on enhanced for loops: The enhanced for loop provides a variable that will hold each element of an array. it doesn’t provide the index of an element. You will need to include code to keep track of indexes if it’s necessary for your code.

  • No variable is used to refer to the current index.

  • Instead, the element located at the current index is stored as the variable declared before the colon.

  • Overall, the enhanced for loop stores each element in increasing order by the index of the array testGrades as grade. It then adds grade to the total.

  • The reason why enhanced for loops are used is because the risk of getting an ArrayIndexOufOfBoundsException when it traverses the array is less, and it shortens the notation of elements.

  • Note: FRQs on the AP Exam test your ability to write code that doesn’t go out of bounds. The AP exam will require you to create, traverse and modify your arrays.

  • It’s important to pay attention to the data that you are including in your arrays. Any unassigned value will just default to 0.

  • Since each element is an object in an array, the default value for the object will be null. Any operations performed on null will result in a NullPointerException because null isn’t a valid object!

  • Arrays are useful to organize and manipulate objects. Just make sure that the array is full of objects so that it produces the desired results.

Searches

  • In many cases when you have data stored in an array you may want to search the array for specific values.

  • It makes it easier to pinpoint values in some cases instead of just using loops.

  • The most common searches are:

  • Sequential searches- These search through the data one by one in order, and take a long time to execute.

  • Binary searches- These search through the data for the desirable value by dividing it into half each time until the desired value is found.

  • The 2 searches above are referred to as search algorithms.

  • Note: On the AP Exam you will be asked to recognize these search algorithms and understand how they function.

Sorts

  • Sorting Algorithms- They take data in an array, and rearrange it into a particular order. They are very powerful because they can accomplish tasks faster, and can make your programs even more efficient.

  • Selection sort: This sort searches and swaps. Once it finds the lowest value, it will swap its position in the array with the data at index 0. The first element is now sorted. The process repeated for index 1. The rest of the array will be searched for the lowest value and is swapped with the data that is found at index 1.

  • Insertion sort: It compares the first 2 elements, and depending on the outcome of this it inserts the second value in front of the first value into index 0, which moves the first value to index 1. The first 2 elements are sorted. Then the third element is checked and this process continues until it’s entirely sorted.

  • Merge Sort: This type of sort uses recursion. An array is split into 2 pieces. The piece is sorted. The 2 sorted pieces are merged together into one sorted list. Then in order to sort each of the 2 pieces, the same method is used again until it is fully sorted.

  • **Recursion- Technique that uses a method to call itself. ***View Unit 12

robot