Creating the Java ArrayList Data Structure

Dino Cajic
Aug 14, 2020 · 2 min read
Dino Cajic explaining Java ArrayList Data Structure
Dino Cajic explaining Java ArrayList Data Structure

Certain data structures in Java can be created by you (yes you). In this example, we’ll go ahead and create an ArrayList data structure that has some of the methods that the built-in ArrayList class has.

We’ll create 2 constructors:

  • The default constructor that creates an ArrayList with a default size of 10.
  • Constructor that allows an initial size to be passed to the array.

We’ll also create a number of methods:

  • void add(Object x); A method that allows you to place an Object at the end of the ArrayList.
  • void add(int index, Object x); A method that allows you to place a value at a given location.
  • Object get(int index): Allows you to retrieve a value of the arrayList array from a given location.
  • int size(); Allows you to get the number of elements currently in the Arraylist.
  • boolean isEmpty(); Tests to see if the Arraylist is empty.
  • boolean isIn(Object x); A method that sees if a particular object exist in the arrayList.
  • int find(Object x); Returns the location of first occurrence of an Object starting from location 0.
  • void remove(Object x); Removes the first occurrence of an Object starting from location 0.

I encourage you not to look at the ArrayList built-in class. See if you can figure it out on your own. The only other restriction will be to store the Objects in an array data field. Create a test class to test the ArrayList class. Name your ArrayList class ArrayList.java so that it overwrites the built in class.

Read the notes above each method. There is a precondition that states the requirements that you’ll need to abide by before the method is called. For example, if the method requires an array of integers to be passed to it, you will need to have an array of integers ready. After the method is called, there is the post-condition. This explains what the expected output of the method will be and the steps the method takes to achieve that output.

We’ll start off with the ArrayList class. The methods that were outlined above will be added to this class.

What good is the code if you’re not going to test it. We’ll create a Driver class that’s going to test the ArrayList code.

And that’s really all their is too it. With basic Java skills, you too can create your ArrayList data structure.

Dev Genius

Sign up for Best Stories

By Dev Genius

The best stories sent monthly to your email. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Dino Cajic

Written by

Author of An Illustrative Introduction to Algorithms. A Software Engineer with a B.S. in Computer Science, a minor in Biology, and a passion for learning.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Dino Cajic

Written by

Author of An Illustrative Introduction to Algorithms. A Software Engineer with a B.S. in Computer Science, a minor in Biology, and a passion for learning.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store