Head First Java Chapter 06 summary

Udesh Kavinda
3 min readNov 17, 2021

ArrayList

The ArrayList class is a resizable array, which can be found in the java.util package.

The difference between a built-in array and an ArrayList in Java, is that the size of an array cannot be modified (if you want to add or remove elements to/from an array, you have to create a new one). While elements can be added and removed from an ArrayList whenever you want. The syntax is also slightly different.

Something you can do with Arraylist.

Make one

ArrayList<Egg> myList = new ArrayList<Egg>();

Put something in it.

Egg s = new Egg();

myList.add(s);

Put another thing in it.

Egg b = new Egg();

myList.add(b);

Find out how many things are in it.

int theSize = myList.size();

Find out if it contains something.

boolean isIn = myList.contain(s);

Find out where something is (i.e. its index)

boolean idx = myList.indexOf(b);

Find out if it’s empty

boolean empty = myList.isEmpty();

Remove something from it.

myList.remove(s);

Iterating over ArrayLists in Java

  1. Using for loops
  2. Using while
  3. Using for-each loop
  4. Using Iterator
  5. Using Lambda expressions (after Java8 only)
  6. Using Enumeration interface
import java.util.*;public class Program { 
public static void main (String[] args){
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);

// Iterating using for loop
for (int i = 0; i < numbers.size(); i++) System.out.print(numbers.get(i) + " ");

//Iterating using while loop
int val = 0;
while (numbers.size() > val) { System.out.println(numbers.get(val));
val++ ; }

// Iterating using For Each Loop
for (Integer i : numbers) System.out.print(i + " ");

//Iterating using Iterator
Iterator it = numbers.iterator();
while (it.hasNext()) System.out.print(it.next() + " ");

// Iterating using lambda expressions
numbers.forEach(number->System.out.println(number));

//Iterating using Enumeration interface
Enumeration<Integer> e = Collections.enumeration(numbers);
while (e.hasMoreElements()) System.out.println(e.nextElement());
}
}

ArrayList Vs Array.

  • A plain old array has to know its size at the time it’s created.
  • To put an object in a regular array, you must assign it to a specific location.
  • Arrays use array syntax that’s not used anywhere else in java.
  • ArrayList in Java 5.0 are parameterized.

Boolean Expressions.

Here are the Boolean Expression Used.
* ‘And’ and ‘Or’ Operators (&&, ||)
* Not equals (!= and !)
* Short Circuit Operators (&&, ||).
* In case of && if the JVM sees that the left side of a && expression is false, it stops right there.
* Similarly with ||, the expression will be true if either side is true,

Bullet Points

  • ArrayList is a class in the Java Api.
  • To Put something into an ArrayList, use add().
  • To remove something from an ArrayList use remove().
  • To find out where something is in an ArrayList, use indexOf().
  • To find out if an ArrayList is empty, use isEmpty().
  • To get the size in an ArrayList, use the size() method.
  • To get the length in a regular old array, you have to use the length variable.
  • An ArrayList resizes dynamically to what-ever size is needed. It grows when objects are added, and it shrinks when objects are removed.
  • You declare the type of the array using a type parameter, which is a type name in angle brackets.
  • Although an ArrayList holds objects and not primitives, the compiler will automatically wrap a primitive into an object.
  • Classes are grouped into packages.
  • A class has a full name, which is a combination of the package name and the class name.
  • To use class in a package other that java.lang, you must tell Java the full name of the class.
  • You use either an import statement at the top of your source code, or you can type the full name every place you use the class in your code.

--

--