KEMBAR78
AP CSA Array List | PDF | Integer (Computer Science) | Software Development
0% found this document useful (0 votes)
3 views3 pages

AP CSA Array List

AP CSA

Uploaded by

Meltem Taskin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

AP CSA Array List

AP CSA

Uploaded by

Meltem Taskin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ArrayList

ArrayLists & Lists


A huge limitation of arrays is that it has a fixed length, and can only store, one specific
type of data.
Ex: You have a group of action figures, an array would require that all the
action figures are of the same type. So they would all need to have the same
features, ex: flying or protective body armor. However, the number of action
figures is represented using an array set to a fixed number. Extra spaces will
simply be ignored, and once you reach one it will result in a
NullPointerException.
An ArrayList is a solution for this issue.
An ArrayList object is dynamically sized, it can become smaller or larger as elements
are added or removed.
It can store multiple types of data without specific limits.
It’s best to choose between an Array and ArrayList based on your needs and the
functionality of the array.
ArrayLists are very adaptable, so if you need to make an ArrayList set to a specific
type, you set it up as typed ArrayList.
Ex: Say you want to make an ArrayList called lunchBag.

ArrayList lunchBag = new ArrayList();

Unlike establishing an array, the number of objects or length isn’t set.


The way you access data within an ArrayList is different from that of Arrays.
Note: Bracket notation ==can’t== be used, unlike in Array’s.
For example, say you want to return the second object, an Apple Object,
from the ArrayList and store it in a variable.

Apple red = lunchBag.get(1);

Other useful methods include add, set, remove, and size.


Arraylists are unique because only objects can be strained in them. The primitive data
types of int, and double can’t be stored in ArrayLists.
Instead, you have to use the Integer or Double wrapper classes. Integer and Double
objects can be created with integers and doubles, as parameters.
Ex:
Integer n = new Integer (5);
Double x = new Double(6.1);

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?

ArrayList list = new ArrayList();

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));

You might also like