Git Product home page Git Product logo

cs-implementing-an-arraylist-lab's Introduction

cs-implementing-an-arraylist-lab

Objectives

  1. Write an implementation of an ArrayList.

Implementing an Array-backed List

For this lesson we provide a partial implementation of an ArrayList that uses a Java array to store the elements. We left four of the methods incomplete; your job is to fill them in. We provide JUnit tests you can use to check your work.

Instructions

When you check out the repository for this lab, you should find a file structure similar to what you saw in the previous lab. The top level directory contains CONTRIBUTING.md, LICENSE.md, README.md, and the directory that contains the code for this lab, javacs-lab02.

In the subdirectory javacs-lab02/src/com/flatironschool/javacs you'll find the source files you need for this lab:

*  `MyArrayList.java` contains a partial implementation of the `List` interface using a Java array to store the elements.

*  `MyArrayListTest.java` contains JUnit tests for `MyArrayList`.

In javacs-lab02, you'll find the Ant build file build.xml. If you are in this directory, you should be able to run ant MyArrayList to run MyArrayList.java, which contains a few simple tests. (Make sure you run ant build before trying to run the individual classes!) Or you can run ant MyArrayListTest to run the JUnit test.

When you run the tests, several of them should fail. If you examine the source code, you'll find four TODO comments indicating which methods you will fill in.

MyArrayList code

Before you start filling in the missing methods, let's walk through some of the code. Here are the instance variables and the constructor.

public class MyArrayList<E> implements List<E> {
	int size;                    // keeps track of the number of elements
	private E[] array;           // stores the elements
	
	public MyArrayList() {
		array = (E[]) new Object[10];
		size = 0;
	}
}

As the comments indicate, size keeps track of how many elements are in MyArrayList, and array is the array that actually contains the elements.

The constructor creates an array of 10 elements, which are initially null, and sets size to 0. Most of the time, the length of the array is bigger than size, so there are unused slots in the array.

One detail about Java: You can't instantiate an array of T[], so you have to instantiate an array of Object and then typecast it. You can read more about this issue here.

Next we'll look at the method that adds elements to the list. Here's our implementation of add:

	public boolean add(E element) {
		if (size >= array.length) {
			// make a bigger array and copy over the elements
			E[] bigger = (E[]) new Object[array.length * 2];
			System.arraycopy(array, 0, bigger, 0, array.length);
			array = bigger;
		} 
		array[size] = element;
		size++;
		return true;
	}

If there are no unused spaces in the array, we have to create a bigger array and copy over the elements. Then we can store the element in the array and increment size.

It might not be obvious why this method returns a boolean, since it seems like it always returns true. As always, you can find the answer in the documentation.

It's also not obvious how to analyze the performance of this method. In the normal case, it's constant time, but if we have to resize the array, it's linear. In the next README we'll explain how we can handle this.

We'll look at get next, and then you can fill in set. Actually, get is pretty simple:

	public T get(int index) {
		if (index < 0 || index >= size) {
			throw new IndexOutOfBoundsException();
		}
		return array[index];
	}

If the index is out of bounds, it throws an exception; otherwise it reads and returns an element of the array. Notice that it checks whether the index is less than size, not array.length, so it's not possible to access the unused elements of the array.

Instructions

  • In MyArrayList.java, you'll find a stub for set that looks like this:
	public T set(int index, T element) {
		// TODO: fill in this method.
		return null;
	}

Read the documentation of set, then fill in the body of this method. If you run MyArrayListTest again, testSet should pass.

HINT: Try to avoid repeating the index-checking code.

  • Your next mission is to fill in indexOf. As usual, you should read the documentation so you know what it's supposed to do. In particular, notice how it is supposed to handle null.

    To make things a little easier, we've provided a helper method called equals that compares an element from the array to a target value and returns true if they are equal (and it handles null correctly). Notice that this method is private because it is used inside this class but it is not part of the List interface.

When you are done, run MyArrayListTest again; testIndexOf should pass now, as well as the other tests that depend on it.

  • Only two more methods to go, and you'll be done with this lab. The next one is an overloaded version of add that takes an index and stores the new value at the given index, shifting the other elements to make room, if necessary.

Again, read the documentation, write an implementation, and run the tests for confirmation.

HINT: Avoid repeating the code that makes the array bigger.

Once you have your implementation working, compare it to mine, which you can find by checking out the solutions branch of the repo, or you can read it on GitHub.

View Implementing an Array List on Learn.co and start learning to code for free.

cs-implementing-an-arraylist-lab's People

Contributors

allendowney avatar annjohn avatar pletcher avatar pwhitter1 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

pwhitter1

cs-implementing-an-arraylist-lab's Issues

Possible Incorrect Use of @Override and Possibly Buggy removeAll test

There are a couple of possible issues:

  1. The master branch comes with a large number of incorrect uses of @OverRide such that Eclipse won't build and run the project until they have been removed. The principle here is that you don't override interface methods. The lab cs-implementing-a-linkedlist-lab also has this issue.

  2. The assertion in the test for removeAll (see line 229 of MyArrayListTest.java) fails despite my remove method being substantially identical to the solution offered in the subsequent lesson. You may analyze my solution here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.