AP CSA Array List
AP CSA Array List
To call these values from the example above you can use the intValue(), and
doubleValue() methods.
Ex:
int a = n.intValue();
int y = x.doubleValue();
Other variables that the AP Computer Science Java Subset includes are the static
variables of MINVALUE, and MAXVALUE found in the Integer class.
These static variables store the minimum and maximum values of an integer.
Ex: Consider the following code segment. What is printed as a result of
executing the code segment?
list.add(“A”);
list.add(“B”);
list.add(0,”C”);
list.add(“D”);
list.set(2,”E”);
list.remove(1);
System.out.println(list);
Answer: It should print out “C E D”. This is because our list at first will be A B. Since we ask to
add C to the index of 0 the array will look like this- C A B. Then D gets added to become C A B D.
The B gets replaced with E to become C A E D. Then we remove A, because it’s at index 1. It
becomes C E D.
Differences between Array’s and ArrayLists:
Array ArrayList
Arrays have a fixed length. ArrayLists can resize when new elements are added
to it.
You don’t need to have an import You have to have an important statement,
statement to use an array. The only java.util.ArrayList, or the full name of the package
case to use an import statement in when you use the ArrayList.
an array would be when the array
has specific elements that require
import statements.
Elements can be accessed with Different methods are used to access the ArrayList.
index notation. Ex: Ex: myList.get(2), myList.add(“Bob”)
LibraryArray[3];
Arrays can contain primitive data ArrayLists can only be used to hold object references.
types(int, float, double, boolean, and
char), and/or object data types.
They can only hold one specific type Has the ability to hold a collection of
of element. Ex: If the array is said to objects.**However, this isn’t recommended** Ex:
hold only integers, then it stores only ArrayList list = new
integers, not other types such as ArrayList();list.add(new
Strings. String(“Hello”));*list.add(new
Integer(5));