Java and Back Again

A beginner tale…part 3

Robert M Ricci
Mar 28 · 3 min read
Photo by Nathan Dumlao on Unsplash

Welcome to the third part of my series on my journey through learning Java. In the first part, I talked a little about the history and some basic principles. With the second part, I went into state and instances, with some time explaining a little about object-oriented programming. In this part, we will get into Arrays and ArrayLists. We will talk about the differences between the two, and I will give examples of both.

Now unlike other programming languages, that I am familiar with, arrays in java are only able to hold a collection of similar datatypes. So you couldn’t hold a string and an int in the same array. Like most other languages an element's position is called an index. Indexes start at zero and go until the last element. This means that the last element's index will be one less than the actual number of elements. For example, if you have four elements in an array, the last element's index would be three. One more thing I want to point out is that Java arrays are a fixed size. Once they are declared their size can not be changed.

Below are examples of how to create arrays, there are two different ways. I will also show examples of reference indexes so that you can add or change an element in an array.

String[] words = {"The", "One", "Ring", "to", "Rule", "Them","All"};
//You can use bracket notation and add the all.
String[] precious = new String[2] //The two represents how large the
array is. This array only has two
elements.
precious[0] = "My";
precious[1] = "Precious";
//Or you can use the new keyword and assign them seperatly
//Notice that when I assigned the first string I placed it at zero, //which is the first index.
//If you would wan to change an elemnt you can also use bracket notation. precious[1] = "Fishes"
precious = {"My", "Fishes"};

ArrayList’s are used when you want to hold a collection of different data types. Unlike arrays, ArrayLists size can be adjusted, elements can be both added and removed. ArrayLists aren’t native to java, so you have to import a package into your java file in order to use them. Another difference between arrays and ArrayList is that you can not declare ArrayLists with values.

import java.util.ArrayList;       //Import ArrayList packageArrayList<String> fellowship = new ArrayList<String>();
//create an ArrayList called fellowship

There are a few different methods you can use to modify and ArrayList, depending on what you want to accomplish.

import java.util.ArrayList;      public class Fellowship {
public static void main(String[] args) {
ArrayList<String> fellowship = new ArrayList<String>(); fellowhip.add("Frodo"); //add memebers(elements) to the
fellowhip.add("Sam"); fellowship.
fellowhip.add("Mary");
fellowhip.add("Pippin");
fellowhip.add("Aragorn");
fellowhip.add("Boromir");
fellowhip.add("Legolas");
fellowhip.add("Gimli");
fellowhip.add("Gandalf");
fellowship.remove(0); //remove Frodo, Sam, and Boromir
fellowship.remove("Sam");
fellowship.remove(3); //Notice that after I removed Frodo
and Sam, Boromir's index changed
to three from five. Keep that in
mind when removing elements using
their index.
}}

As you can see arrays and ArrayLists are a little different not only from other languages but from themselves as well. Array’s are only for elements of similar data types, whereas ArrayLists are for dynamic groups. I hope you found this helpful. Keep an eye out for the next part.

Geek Culture

Proud to geek out.

By Geek Culture

Subscribe to receive top 10 most read stories of Geek Culture — delivered straight into your inbox, once a week. 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.

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Robert M Ricci

Written by

Full Stack Developer Ruby and Javascript. Recent grad of the Flatiron School.

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

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