Home FRQ 1 - ArrayLists
Post
Cancel

FRQ 1 - ArrayLists

ArraysList Overview

Important Vocabulary

Array is a data structure used to implement a list of primitive or object data Element is a single value within the array The index of an element is the position of the element within the array - For Java, the first element is at index 0 The length of an array is the number of elements in the array - length is a public final data member of an array - Since length is public, we can access it in any class - Since length is final, we cannot change an array’s length after it has been created

In Java, the last element of an array list is at index list.length - 1

2017 FRQ #1

View FRQ here

Syntax to remember

  • This part importantly declares an array and references the ‘.add’ function

Remember the syntax:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// declare the array list
[arrayListName] = new ArrayList<Integer>();

// add to the list
[arrayListName].add([position], [value])
// ---OR---
[arrayListName].add([value])

// get from the list at a position
[arrayListName].get([position]);

// modify a list at a specific position
[arrayListName].set([position], [value]);

// get the size of an array
[arrayListName].size();

// not pertaining to arrays but still good to know
// the .intValue() method is used to convert an object of a wrapper class for primitive types (such as Integer, Double, Float, etc.) into its corresponding primitive int value.
Integer integerValue = new Integer(10); // Creating an Integer object with value 10
int intValue = integerValue.intValue(); // Extracting the int value from the Integer object
// This is needed because java arrayLists store wrapper objects for 'int' primitive values
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class Digits
{
    private ArrayList<Integer> digitList;

    // Part (a)
    public Digits(int num)
    {
        digitList = new ArrayList<Integer>();
        if (num == 0)
        {
            digitList.add(new Integer(0));
        }
        while (num > 0)
        {
            digitList.add(0, new Integer(num % 10));
            num /= 10;
        }
    }

    // Part (b)
    public boolean isStrictlyIncreasing()
    {
        for (int i = 0; i < digitList.size()-1; i++)
        {
            if (digitList.get(i).intValue() >= digitList.get(i+1).intValue())
            {
                return false;
            }
        }
        return true;
    } 
} 

Common Mistakes

  1. Failure to use the intValue() method.
    • This is important because Java by default stores data in arrayLists using wrapper classes liek Interger. In this case the wrapper class needs to be converted into a primitive int datatype to perform operations on it.
  2. Failure to use num % 10 or num /= 10.
    • These lines are meant to target the last digit to either add to the arrayList or to remove from the integer
This post is licensed under CC BY 4.0 by the author.

Methods & Control Structures (Math Class)

Data Types and Control Structures Lesson