Java Spells: ArrayList

This tutorial was created with Roy Nunez. To learn the basics of ArrayList check out their tutorial HERE.

As a refresher, an ArrayList is like an Array without a set size. Unlike Arrays however, ArrayLists can only consist of Objects and cannot hold primitive data types like ints and booleans. If you want to make an ArrayList that holds integers or booleans, you’ll have to use their corresponding Wrapper Classes such as Integer an Boolean.

ArrayList<Boolean> divisByThree = new ArrayList<Boolean>();
ArrayList<Integer> modThree = new ArrayList<Integer>();

Lets take our ArrayLists we defined above and initialize them with a loop like in the Array tutorial.

for (Integer i = 0; i <= 25; i++){
    if ((i % 3) == 0){
       divisByThree.add(true);
    }else{
       divisByThree.add(false);
    }
    modThree.add(i % 3);
}

ArrayLists also support a predefined sorting function.

import java.util.Collections;
Collections.sort(modThree);  
// Sorts the values in modThree from lowest to highest

ArrayLists are slower to access than standard Arrays. This is because their undefined and variable size results in them not being store consecutively in memory. Rather an ArrayList stores each Object it contains at it’s own memory address and the ArrayList itself is stored as a consecutive series of references to those addresses.

I hope this more advanced dive on ArrayLists has been helpful to you in your programming endeavors. I can’t wait to see what you craft next!

Code Available at https://github.com/StrategicNPC/CollaberaJUMPProjects/tree/master/Code/ArrayListDemos

Working the Magic: Interview Questions

Question 1: What’s the value of “nums” after executing the following code?

int[] nums = new int[5];
for (int i = 0; i <= nums.length; i++){
     nums[i] = i;
}

Turns out this is a trick question. Arrays in Java start counting from zero so if you try to access the index of your Array’s length, you’ll try to access an index the Array doesn’t have crashing Java.

int[] nums = new int[5];
//A Simple fix is to remove the = from your operation
for (int i = 0; i < nums.length; i++){ 
     nums[i] = i;
}

Question 2: To be Added

Java Cantrips: Arrays

So you’ve picked up the building blocks of Java. You feel comfortable creating a Variable, you’ve created Loops and If statements, you may even have Instantiated an Object. Now you find that to complete your next project, you need more than one of the same Object. You could create an Instance for each Object you need, or you could create an Array:

int num;                  
//A variable holds one of an given class instance
int[] nums = new int[4];  
//An array can hold as many instances as specified when it is created
//  !!THIS CANNOT BE CHANGED LATER!!
String[] colors = {"Green", "Blue", "Red", "Purple"};   
//When you create an array, you can specify it's elements with {}

An Array is a set of Objects of any type of a size you chose at it’s creation (Be careful, you cannot change it later). You can reference a member of the Array by referring to it’s Index, or place in the Array. Arrays count starting from 0 so the first element in your Array will be found at Index 0.

int[] nums = {3,62,47,0,12};
// Prints out the length of nums (5)
System.out.println(nums.length); 
// Prints the element of nums at index 3 (0)
System.out.println(nums[3]);    

// This will break your code as the last index of an array 
// is always 1 less than the length of that array
System.out.println(nums[nums.length]);

Arrays work well with Loops, as they allow for the easy Initialization of your Arrays as well as a method to check every element.

int[] countdown = new int[5];
for (int i=0; i < countdown.length; i++){
    countdown[i] = countdown.length - i;
} 
//This creates an array with the numbers
//5,4,3,2,1 in that order

// This is an example of how to use a loop to display an array
for (int i=0; i <= countdown.length; i++){              
if(i == countdown.length){
        System.out.println("BLASTOFF!!!");
    }else{
        System.out.println(countdown[i]);
    }
}

That’s the basics of Arrays in Java. It’s a simple option available to you when coding that opens up a vast amount of possibilities.

Code available at https://github.com/StrategicNPC/CollaberaJUMPProjects/tree/master/Code/ArrayDemonstrations.
Additional information on at https://corejava.design.blog/2019/08/06/example-post/

Design a site like this with WordPress.com
Get started