Array/Arraylist Review

Array

  • fixed sized
  • single or multidimensional

image

How to Create Arrays:

  1. Declare
int[] arr1;
int[] arr2;
  1. Initialize
//separating declare and initialize
arr1 = new int[5];
arr2 = new int[]{1,2,3};

// declare and initialize at the same time
int[] arr1 = new int[5];
int[] arr2 = {1,2,3}; // an array literal
  1. Access using []
arr2[0] // accesses the first element in the array

Arraylist

  • resizable
  • uses methods to access and modify elements
  • typically stores objects

How to Create Arraylists:

  1. Declare and Initialize
ArrayList<String> al = new ArrayList<String>();
  1. Basic Operations

Insertion using add()

al.add("apple");
al.add("orange");
al.add("banana");

Access using get()

al.get(0); 

Deletion using remove()

al.remove("orange");

Update using set()

al.set(0, "kiwi");

Warmup

Write a java program to sort an Arraylist



Checklist to Maximize Points ✅

Before Writing Code

  • Understand the method signature (what the methods do, the return types, access modifier)
  • Paying attention to input type (e.g. array vs ArrayList)

Writing Code

  • Use loops carefully (consider bounds)
  • Check for null/empty cases

Before Submitting Code

  • Correct return type
  • Check whether syntax is used for array/ArrayList


Common Mistakes to Avoid ❌

[] vs get Confusion (penalty)

[]: used to access elements in array

get: used to access elements in ArrayList

int[] arr = {1,2,3};
System.out.println(arr[0]); 

ArrayList<String> al = new ArrayList<String>();
al.add("sprite");
System.out.println(al.get(0));

.length vs .size() Confusion (no penalty)

.length: property for length of a array .size(): method for length of an Arraylist

String[] colors;
colors = new String[]{"yellow", "purple", "blue"};
System.out.println(colors.length);

ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(12);
nums.add(10); 
System.out.println(nums.size());

Traversing Arrays/ArrayLists

  • Ensure bounds are correct (applying the right comparison/logic)
  • Account for dynamic resizing of ArrayLists for .add() and .remove()
// starts at the last index (.size() - 1)
// ends at the first index
for (int i = temps.size() - 1; temps >= 0; i--) {
    // insert code
}


Scoring Criteria 📝

image