unit 7.2

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall with Kai
GameKnowt Play
New
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/17

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

18 Terms

1
New cards

The following are the ArrayList methods that you need to know for the AP CSA exam. These are included on the AP CSA Java Quick Reference Sheet that you will receive during the exam so you do not need to memorize them. The E in the method headers below stands for the type of the element in the ArrayList; this type E can be any Object type. We will look at how these methods work below.

2
New cards
  • int size() returns the number of elements in the list

  • boolean add(E obj) appends obj to the end of the list and returns true

  • E remove(int index) removes the item at the index and shifts remaining items to the left (to a lower index)

  • void add(int index, E obj) moves any current objects at index or beyond to the right (to a higher index) and inserts obj at the index

  • E get(int index) returns the item in the list at the index

  • E set(int index, E obj) replaces the item at index with obj

3
New cards

As we saw in the last lesson, you can get the number of items in a ArrayList using its size() method. The ArrayList starts out empty with a size of 0.

ArrayList<String> list = **new** ArrayList<String>(); System.out.println( list.size() );

4
New cards

With arrays, you use the length field to get the number of items in the array. But, with an ArrayList you use the size() method to get the number of items in the ArrayList. You will not be penalized if you mix up length and size() in the CSA exam. The number of items in an empty ArrayList is 0.

5
New cards

You can add values to an ArrayList using the method add(obj) which will add the object to the end of the list, just like you would join the end of the line to board a bus.

6
New cards

When adding Integer objects to the list, you can use the Integer constructor like add(new Integer(5)) in Java version 7 which is used on the exam (although this is deprecated and no longer used in Java version 9) or you can just add the int value directly like add(5) in any Java version and it will be changed into an Integer object automatically. This is called autoboxing. When you pull an int value out of a list of Integers that is called unboxing.

ArrayList<Integer> list = **new** ArrayList<Integer>(); list.add(**new** Integer(5));

*// this will only work in Java 7*list.add(5);

*// this will work in all Java versions*

7
New cards

You can put any kind of objects into an ArrayList. Even instances of a class that you wrote. For example, here is an ArrayList of Students.

8
New cards

There are actually two different add methods in the ArrayList class. The add(obj) method adds the passed object to the end of the list. The add(index,obj) method adds the passed object at the passed index, but first moves over any existing values to higher indices to make room for the new object.

9
New cards

ArrayLists like arrays start numbering their elements from 0.

10
New cards

You can also remove values from an ArrayList using the remove(index) method. It removes and returns the item at the given index. This will move all the other items over in the underlying array and decrease the size of the ArrayList by 1.

11
New cards

The remove(int index) method will remove the object at the given index and shift left any values to the right of that index. It doesn’t remove the object that matches the integer value given.

12
New cards

You can get the object at an index using obj = listName.get(index) and set the object at an index using listName.set(index,obj). Both methods require that the index argument refer to an existing element of the list, i.e. the index must be greater than or equal to 0 and less than the size() of the list.

13
New cards

Notice that ArrayLists use get and set methods instead of the index operator that we use with arrays: array[index]. This is because ArrayList is a class with methods, not a built in type with special support in the language like arrays.

14
New cards

When do you use arrays and when do you use ArrayLists? Use an array when you want to store several items of the same type and you know how many items will be in the array and the items in the array won’t change in order or number. Use an ArrayList when you want to store several items of the same type and you don’t know how many items you will need in the list or when you want to remove items from the list or add items to the list while the program is running.

// arrays must specify a size!

int[] highScores = new int[5];

String[] names = new String[5];

// ArrayLists are empty to start with ArrayList<Integer> highScoreList = new ArrayList<Integer>();

ArrayList<String> nameList = new ArrayList<String>();

15
New cards

Here is a comparison of how to access and change elements in arrays and **ArrayList**s. Note that ArrayLists have a method size() instead of a length property, and ArrayLists use get/set methods instead of the index operator ([]).

Operation

array

ArrayList

length/size

array.length

list.size()

Access

value = array[index];

value = list.get(index);

Modify

array[index] = value;

list.set(index,value);

16
New cards

Note that the ArrayList methods add and remove do not have a simple equivalent in arrays because they change the number of elements in the list and may shift the positions of other elements.

17
New cards

Here is a comparison handout of the basic operations to access 1-dimensional and 2-dimensional arrays (which we will see in the next unit), ArrayLists, and Strings made by AP CSA teacher Sam Procopio of Bishop Blanchet High School.

18
New cards
  • The following ArrayList methods, including what they do and when they are used, are part of the Java Quick Reference:

    • int size() : Returns the number of elements in the list

    • boolean add(E obj) : Appends obj to end of list; returns true

    • void add(int index, E obj) : Inserts obj at position index (0 <= index <= size), moving elements at position index and higher to the right (adds 1 to their indices) and adds 1 to size

    • remove(int index) — Removes element from position index, moving elements at position index + 1 and higher to the left (subtracts 1 from their indices) and subtracts 1 from size; returns the element formerly at position index

    • E get(int index) : Returns the element at position index in the list

    • E set(int index, E obj) : Replaces the element at position index with obj; returns the element formerly at position index