Key Concepts

Review core concepts you need to learn to master this subject

Java arrays

// create an array of 5 int elements int[] marks = {10, 20, 30, 40, 50}; // print out the first element of the marks array at index = 0 System.out.println(marks[0]);

In Java, an array is used to store a list of elements of the same datatype.

Arrays are a fixed size, their elements are ordered, and the elements can be accessed using an index, which starts with 0.

Array creation in Java

// create an array of 5 int elements int[] marks = {10, 20, 30, 40, 50}; // print out the first element of the marks array at index = 0 System.out.println(marks[0]);

In Java, an array can be created in the following ways:

  • Using the {} notation, by adding each element all at once.
  • Using the new keyword, and assigning each position of the array individually.

Accessing elements in a Java array

// create an array of 5 int elements int[] marks = {10, 20, 30, 40, 50}; // print out the first element of the marks array at index = 0 System.out.println(marks[0]);

In Java, we can use an index to access the associated element in an array.

The index of an array starts from 0 and goes up to one less than the total length of the array.

Java ArrayList

// create an array of 5 int elements int[] marks = {10, 20, 30, 40, 50}; // print out the first element of the marks array at index = 0 System.out.println(marks[0]);

In Java, an ArrayList is used to represent a dynamic list.

While Java arrays are fixed in size (the size cannot be modified), an ArrayList allows flexibility by being able to both add and remove elements.

Modifying ArrayLists in Java

// create an array of 5 int elements int[] marks = {10, 20, 30, 40, 50}; // print out the first element of the marks array at index = 0 System.out.println(marks[0]);

An ArrayList can easily be modified using built in methods.

To add elements to an ArrayList, you use the add() method. The element that you want to add goes inside of the ().

To remove elements from an ArrayList, you use the remove() method. Inside the () you can specify the index of the element that you want to remove. Alternatively, you can specify directly the element that you want to remove.

Chevron Left Icon
Learn Java: Arrays
Lesson 1 of 2
Chevron Right Icon
  1. 1
    We have seen how to store single pieces of data in variables. What happens when we need to store a group of data? What if we have a list of students in a classroom? Or a ranking of the top 10 horse…
  2. 2
    Imagine that we’re using a program to keep track of the prices of different clothing items we want to buy. We would want a list of the prices and a list of the items they correspond to. To create a…
  3. 3
    When we printed out the array we created in the last exercise, we saw a memory address that did not help us understand what was contained in the array. If we want to have a more descriptive printo…
  4. 4
    Now that we have an array declared and initialized, we want to be able to get values out of it. We use square brackets, [ and ], to access data at a certain index: double[] prices = {13.1, 15.87,…
  5. 5
    We can also create empty arrays and then fill the items one by one. Empty arrays have to be initialized with a fixed size: String[] menuItems = new String[5]; Once you declare this size, it cann…
  6. 6
    What if we have an array storing all the usernames for our program, and we want to quickly see how many users we have? To get the length of an array, we can access the length field of the array obj…
  7. 7
    When we write main() methods for our programs, we use the parameter String[] args. Now that we know about array syntax, we can start to parse what this means. A String[] is an array made up of St…
  8. 8
    We have now seen how to store a list of values in arrays. We can use this knowledge to make organized programs with more complex variables. Throughout the lesson, we have learned about: - Creatin…
  1. 1
    When we work with arrays in Java, we’ve been limited by the fact that once an array is created, it has a fixed size. We can’t add or remove elements. But what if we needed to add to the book list…
  2. 2
    To create an ArrayList, we need to declare the type of objects it will hold, just as we do with arrays: ArrayList babyNames; We use angle brackets to declare the type of the ArrayList. These …
  3. 3
    Now we have an empty ArrayList, but how do we get it to store values? ArrayList comes with an add() method that takes an argument to add to the end of the ArrayList: ArrayList sudokuRow1 = new A…
  4. 4
    Let’s say we have an ArrayList that stores items in a user’s online shopping cart. As the user navigates through the site and adds items, their cart grows bigger and bigger. If we wanted to displa…
  5. 5
    With arrays, we can use bracket notation to access a value at a particular index: double[] ratings = {3.2, 2.5, 1.7}; System.out.println(ratings[1]); This code prints 2.5, the value at index 1 o…
  6. 6
    When we were using arrays, we could rewrite entries by using bracket notation to reassign values: String[] shoppingCart = {“Trench Coat”, “Tweed Houndstooth Hat”, “Magnifying Glass”}; shoppingCar…
  7. 7
    What if we wanted to get rid of an entry altogether? For arrays, we would have to make a completely new array without the value. Luckily, ArrayLists allow us to remove an item by specifying the in…
  8. 8
    What if we had a really large list and wanted to know the position of a certain element in it? For instance, what if we had an ArrayList detectives with the names of fictional detectives in chronol…
  9. 9
    Nice work! You now know the basics of ArrayLists including: - Creating an ArrayList. - Adding a new ArrayList item using add(). - Accessing the size of an ArrayList using size(). - Finding an item …

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo