KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Implementing a Map : 04/26/2024

  • Streams : 04/25/2024

  • Generics : 04/24/2024

  • Hashing : 04/23/2024

  • Binary Search : 04/22/2024

  • MP3: Course Ratings : 04/19/2024

  • Quicksort : 04/18/2024

  • Merge Sort : 04/17/2024

  • Sorting Algorithms : 04/16/2024

  • MP Debugging Part 1 : 04/15/2024

  • MP2: Course Activity : 04/12/2024

  • Practice with Recursion : 04/11/2024

  • MP Debugging Part 0 : 04/10/2024

  • MP2: API Client : 04/09/2024

  • MP2: API Server : 04/08/2024

  • Trees and Recursion : 04/05/2024

  • Trees : 04/04/2024

  • Recursion : 04/03/2024

  • MP1: Filtering and Search : 04/02/2024

  • MP1: Loading and Sorting : 04/01/2024

  • Lists Review and Performance : 03/29/2024

  • Linked Lists : 03/28/2024

  • Algorithms and Lists : 03/27/2024

  • Continuing MP0 : 03/26/2024

  • Getting Started with MP0 : 03/25/2024

  • Lambda Expressions : 03/22/2024

  • Anonymous Classes : 03/21/2024

  • Practice with Interfaces : 03/20/2024

  • Implementing Interfaces : 03/19/2024

  • Using Interfaces : 03/18/2024

  • Working with Exceptions : 03/08/2024

  • Throwing Exceptions : 03/07/2024

  • Catching Exceptions : 03/06/2024

  • References and Polymorphism : 03/05/2024

  • References : 03/04/2024

  • Data Modeling 2 : 03/01/2024

  • Equality and Object Copying : 02/29/2024

  • Polymorphism : 02/28/2024

  • Inheritance : 02/27/2024

  • Data Modeling 1 : 02/26/2024

  • Static : 02/23/2024

  • Encapsulation : 02/22/2024

  • Constructors : 02/21/2024

  • Objects, Continued : 02/20/2024

  • Introduction to Objects : 02/19/2024

  • Compilation and Type Inference : 02/16/2024

  • Practice with Collections : 02/15/2024

  • Maps and Sets : 02/14/2024

  • Lists and Type Parameters : 02/13/2024

  • Imports and Libraries : 02/12/2024

  • Multidimensional Arrays : 02/09/2024

  • Practice with Strings : 02/08/2024

  • null : 02/07/2024

  • Algorithms and Strings : 02/06/2024

  • Strings : 02/05/2024

  • Functions and Algorithms : 02/02/2024

  • Practice with Functions : 02/01/2024

  • More About Functions : 01/31/2024

  • Errors and Debugging : 01/30/2024

  • Functions : 01/29/2024

  • Practice with Loops and Algorithms : 01/26/2024

  • Algorithms : 01/25/2024

  • Loops : 01/24/2024

  • Arrays : 01/23/2024

  • Compound Conditionals : 01/22/2024

  • Conditional Expressions and Statements : 01/19/2024

  • Operations on Variables : 01/18/2024

  • Variables and Types : 01/17/2024

  • Welcome to CS 124 : 01/16/2024

Algorithms and Lists

import java.util.Arrays;
public interface SimpleList {
Object get(int index);
void set(int index, Object value);
Object remove(int index);
void add(int index, Object value);
}
public class SimpleArrayList implements SimpleList {
private Object[] values;
public SimpleArrayList(Object[] setValues) {
values = setValues;
}
public Object get(int index) {
assert index >= 0 && index < values.length;
return values[index];

Welcome back! The rest of the semester is incredibly exciting material. As you begin work on the machine project, we begin building and analyzing new data structures and algorithms. We’ll also introduce new bits of Java syntax along the way.

Algorithms and Data Structures
Algorithms and Data Structures

Algorithms and data structures comprise the core conceptual concerns of computer science. Algorithms are how we do things. Data structures are how we represent things.

The two topics are intertwined. We will implement data structures to support certain algorithms. And we will design algorithms that utilize specific data structure capabilties.

Algorithm Analysis
Algorithm Analysis

As we proceed, we will spend more time talking about how long certain algorithms take and why—or performing algorithm analysis. To do this we use something called Big-O notation to describe the behavior of algorithms. Let’s define those terms:

Big-O notation is a mathematical notation that describes the limiting behavior of a function when the argument tends towards a particular value or infinity.

Complexity Categories
Complexity Categories

We’ll take a very high-level view of Big-O as we get started with algorithm analysis. Let’s provide an overview of the different complexity categories that we’ll learn to identify, and some of the code features that are associated with them.

Array Lists
Array Lists

To get some practice with algorithm analysis, over the next few lessons we’ll be implementing a data structure known as a list. You’ve already been working with Java’s built-in Lists, so this will give you a peek at how they are actually implemented.

Lists store a sequence of elements. We already know how to do that using arrays, and we can build an implementation of lists on top of an array. Let’s see how!

// Initial SimpleArrayList

Remove
Remove

OK, this is good start. But so far all we have is a wrapper around an array! That’s not particularly interesting.

Indeed, the key difference between a Java array and list is that the size of the list can change. But doing this using a list that maintains are array internally requires more work. Let’s see how, starting with the remove operation. (You get to implement add as this lesson’s homework.)

// SimpleArrayList remove

Practice: SimpleArrayList get and set

Created By: Geoffrey Challen
/ Version: 2020.10.0

Let's begin building a simple list implementation that uses arrays to store the values. Create a class SimpleArrayList with a public constructor that initializes the list using a passed array of Object references. assert that the passed array is not null.

Next, implement:

  1. Object get(int), which takes an int index and returns the Object at that index
  2. void set(int, Object), which takes an int index and an Object reference and sets that value at the index to the passed reference.

Both your get and set method should assert that the index passed is valid for that SimpleArrayList. Here's an example of how your SimpleArrayList should work:

Don't overthink this! Both get and set should be two lines of code (including one for the assert).

List Method Algorithm Analysis
List Method Algorithm Analysis

Next, let’s take a look at our core list functions and see how they perform. We’re going to use our new big-O vocabulary and try to understand the performance of get, set, and remove.

// SimpleArrayList performance

Homework: SimpleArrayList add

Created By: Geoffrey Challen
/ Version: 2020.10.0

Let's write the add method for our SimpleArrayList. First, create a SimpleArrayList class with a single public constructor that initializes the list with a passed array of Object references. You should assert that the passed array is not null. Next, implement a getter getValues() which returns the Object array that the list uses to store values. (This is purely for testing.) Also implement size() which returns the size of this list as an int.

Now write the add method, which takes the position to add at as an int as its first parameter and the Object reference to add as its second. add should add the element to the list, increasing the size by one and shifting elements after the add position backward. You should assert that the passed position is valid for this list. But note that you should allow adding a new item to the end of the existing list.

When you are done, here is how your SimpleArrayList class should work:

CS People: Mark Dean
CS People: Mark Dean

Mark Dean was a pioneering Black American computer scientist, engineer, and inventor, who made important contributions to several computing technologies. He developed the ISA bus, an early computer standard allowing interconnection of hardware components. He also worked on computer graphics and the first chip to achieve a 1 GHz clock rate(1).

In recognition of his many accomplishments, Mark Dean was the first African-American to be named an IBM Fellow. Watch the following short video to learn more about Mark Dean:

More Practice

Need more practice? Head over to the practice page.