knowt ap exam guide logo

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 MIN_VALUE, and MAX_VALUE 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 statement to use an array. The only case to use an import statement in an array would be when the array has specific elements that require import statements.

You have to have an important statement, java.util.ArrayList, or the full name of the package when you use the ArrayList.

Elements can be accessed with index notation. Ex: LibraryArray[3];

Different methods are used to access the ArrayList. Ex: myList.get(2), myList.add(“Bob”)

Arrays can contain primitive data types(int, float, double, boolean, and char), and/or object data types.

ArrayLists can only be used to hold object references.

They can only hold one specific type of element. Ex: If the array is said to hold only integers, then it stores only integers, not other types such as Strings.

Has the ability to hold a collection of objects.**However, this isn’t recommended** Ex: ArrayList list = new ArrayList();list.add(new String(“Hello”));*list.add(new Integer(5));

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 MIN_VALUE, and MAX_VALUE 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 statement to use an array. The only case to use an import statement in an array would be when the array has specific elements that require import statements.

You have to have an important statement, java.util.ArrayList, or the full name of the package when you use the ArrayList.

Elements can be accessed with index notation. Ex: LibraryArray[3];

Different methods are used to access the ArrayList. Ex: myList.get(2), myList.add(“Bob”)

Arrays can contain primitive data types(int, float, double, boolean, and char), and/or object data types.

ArrayLists can only be used to hold object references.

They can only hold one specific type of element. Ex: If the array is said to hold only integers, then it stores only integers, not other types such as Strings.

Has the ability to hold a collection of objects.**However, this isn’t recommended** Ex: ArrayList list = new ArrayList();list.add(new String(“Hello”));*list.add(new Integer(5));

robot