AP Computer Science in Java
  • Introduction
  • Introduction to Programming in Java with Karel the Dog
    • Introduction to Programming with Karel
    • More Basic Karel
    • Java Programs and the Run Method
    • Karel Can't Turn Right
    • Methods in Karel
    • Top Down Design and Decomposition in Karel
    • Commenting Your Code
    • SuperKarel
    • For Loops
    • While Loops in Karel
    • If Statements
    • If/Else Statements
    • Control Structures Example
    • How To Indent Your Code
  • Basic Java
    • Printing in Java
    • Variables and Types
    • User Input
    • Arithmetic Expressions
    • Casting
    • Booleans
    • Logical Operators
    • Comparison Operators
    • For Loops
    • While Loops
    • If Statements
    • Loop-and-a-Half
    • Short-Circuit Evaluation
    • De Morgan's Laws
    • Strings
  • Methods
    • Java Methods
    • Methods and Parameters
    • Methods and Return Values
    • Javadoc and More Methods
    • Strings Methods
    • Strings and Characters
    • Exceptions
    • String Processing
  • Classes and Object-Oriented Programming
    • Introduction To Classes and Objects
    • Classes vs. Objects
    • Using a Class as a Client
    • Writing Classes
    • Writing Classes and Instance Methods
    • Getter and Setter Methods
    • Class Methods and Class Variables
    • Method Overloading
    • Local Variables and Scope
    • Key Terms for Classes
    • Objects vs Primitives
    • Inheritance
    • Class Design and Abstract Classes
    • Polymorphism
    • Interfaces
  • Data Structures
    • What Are Data Structures?
    • Introduction to Arrays
    • Using Arrays
    • ArrayList Methods
    • Arrays vs ArrayLists
    • 2D Arrays (Matrices or Grids)
    • Hashmaps
  • Algorithms and Recursion
    • What is an Algorithm?
    • Pseudocode
    • Linear Search
    • Binary Search
    • Selection Sort
    • Insertion Sort
    • Advanced: Recursion
    • Mergesort
Powered by GitBook
On this page
  • Differences Between Arrays and ArrayLists
  • Getting the Size or Length:
  • Setting Values at an Index:
  • Getting Values at an Index:
  • Creating New Instances:
  • Extra Helper Methods:
  • Types:

Was this helpful?

  1. Data Structures

Arrays vs ArrayLists

Both Arrays and ArrayLists are incredibly useful to programmers, but how do we know when to use them? While both Arrays and ArrayLists perform similar operations, they are used differently.

Differences Between Arrays and ArrayLists

One the biggest differences between an Array and ArrayList is expandability. While the size of an ArrayList can change, an Array is a set size. Other differences include handling types and getting the size/length.

Getting the Size or Length:

How we retrieve the size or length of an Array or ArrayList varies between the two.

When dealing with an Array we use arr.length to access its length.

With ArrayLists we would use list.size().

Setting Values at an Index:

Settings values at a given index varies as well.

To set the value of a given index with an Array we use arr[i] = x;.

To set the value of a given index with an ArrayList we use list.set(i, x);.

Getting Values at an Index:

To retrieve values at a given index is as follows:

To get a value at a given index with an Array we use int x = arr[i];.

To get a value at a given index with an ArrayList we use int x = list.get(i);.

Creating New Instances:

To create new instances of an Array or ArrayList you use:

To create a new instance of an Array we use int[] arr = new int[5];.

To create a new instance of an ArrayList we use


ArrayList<Integer> list = new ArrayList<Integer>();

Extra Helper Methods:

Only ArrayLists have extra helper methods. These helper methods include: remove, add at index, clear, and isEmpty.

Types:

Arrays can hold Primitives or Objects.

ArrayLists can only hold Objects, and handles autoboxing and unboxing.

PreviousArrayList MethodsNext2D Arrays (Matrices or Grids)

Last updated 5 years ago

Was this helpful?