CSCI 2170 Lecture 4: ArrayLists

12 views2 pages
11 Oct 2019
AP CS Name: ________________________
Notes: ArrayLists
ArrayList: (BJP Chapter 10)
list: a collection storing an ordered sequence of elements
each element is accessible by a 0-based index
a list has a size (number of elements that have been added)
elements can be added to the front, back, or elsewhere
in Java, a list can be represented as an ArrayList object
Required Library for these: (placed above the Class creation)
Required library for Arrays (placed about the class creation):
import java.util.*;
ArrayList creation and use:
ArrayList<Type> name = new ArrayList<Type>();
name.add("Your Name"); // Assigning a value to the end of an ArrayList name
public static type methodName(ArrayList<Type> name){ // ArrayList as a parameter
public static ArrayList<Type> methodName(parameters) { // returning an ArrayList
methodName(arrayListName); //calling a method with an ArrayList as its parameter
ArrayList<String> names = methodName(parameters); // ArrayList as returned value
ArrayList<String> names = new ArrayList<String>(); // an ArrayList of Strings
public static void actOnIt(ArrayList<String> input) {} // ArrayList parameter
// returning a boolean ArrayList, using “Integer” as the wrapper for int
public static ArrayList<Integer> figureItOut(int x, int y) {}
actOnIt(names); // calling a method with an array as its parameter
ArrayList<Integer> values = figureItOut(x, y); // ArrayList as a returned value
Unlock document

This preview shows half of the first page of the document.
Unlock all 2 pages and 3 million more documents.

Already have an account? Log in

Get OneClass Grade+

Unlimited access to all notes and study guides.

Grade+All Inclusive
$10 USD/m
You will be charged $120 USD upfront and auto renewed at the end of each cycle. You may cancel anytime under Payment Settings. For more information, see our Terms and Privacy.
Payments are encrypted using 256-bit SSL. Powered by Stripe.