Thomas(Tripp) White
Posted on September 13, 2021
When I first learned Java I was coming from a JavaScript background. It felt like I was in the wild west with JavaScript. It is dynamically typed so I can have a variable be one data type and change it whenever I want. I could start creating a function without knowing what the return would be and just figure it out or change it at a whim. Java put a stop to this right away. Java is a Static typed language. Whenever we define a variable or a method we must know exactly what that data type is or our program won’t even compile. Knowing what data types and how to manage them in Java is crucial to being a Java developer. This article is dedicated to Arrays, ArrayList, LinkedList and HashMaps. I’ll give a quick summary on what they are and how they work and give a little example of what it looks like.
Arrays
Arrays are an object that allows you to store a collection of ordered values (elements) in a single variable. Arrays in Java have a set size. This is different to how an array in JavaScript works. When you create your array, you must know how many elements you want and what data type these elements will be. You can not add elements to an array. You can only modify them. If you create an array with 5 elements, you may not add a six. You will either have to replace an element with the new 6th one or create a new Array with 6 elements.
Since elements in an Array are ordered you can access them by an index number. The index number is the position of the element in the array. IMPORTANT: Arrays start with an index of 0. If you want to access the first element in the array, the index will be [0]
not [1]
. To change the element of an Array you can access the element and set it equal to the new value.
These are great to use when you have a predetermined number of values and need to access random elements throughout the array.
class Main {
public static void main(String args[]) {
// creating an array of strings called dogs. This creates a new array with two elements.
String[] dogs = {"Ada", "Taz"};
// This changes the value of the first element "Ada" to "Snoopy"
dogs[0]="Snoopy";
//You can also create an array without having the values. This will create an array with 4 elements.
int[] randomNums = new int[4];
}
}
ArrayList
ArrayList are very similar to Arrays. An ArrayList is a resizable array. This means you can add or remove elements. An ArrayList actually contains a regular Array that we discussed earlier. When you add an element to the ArrayList it will check to see if there is room in the array for the element. If there is not enough room it will create a new Array with the new size and remove the old one. This is done behind the scenes but its important to know.
To use an ArrayList you must import java.util.ArrayList;
This will give you access to the different methods that come with ArrayList. IMPORTANT: Elements in an ArrayList are objects. If you want to create an ArrayList of a primitive data type you will need to use a wrapper class. <Boolean>
<Character>
<Integer>
etc.
ArrayList are great for sorting and accessing data. If you need to access random elements inside of an Array and the Array size may change, ArrayList is a great option.
import java.util.ArrayList;
class Main {
public static void main(String args[]) {
//Creates an empty ArrayList of data type Strings
ArrayList<String> dogs = new ArrayList<String>();
// Add elements to the newly created ArrayList
dogs.add("Ada");
dogs.add("Taz");
dogs.add("Snoopy");
//removes(“the element at index 2”)
dogs.remove(2);
//prints out the first element of the ArrayList dogs
System.out.println(dogs.get(0));
}
}
LinkedList
LinkedList is very similar to an ArrayList. You might notice the List at the end of the name. This is because they both implement the List interface. This gives them a lot of the same characteristics and methods. What makes LinkedList different is in the way that they are constructed. Remember how an ArrayList contains and Array and will create and remove the Array as needed. A LinkedList stores its items in “containers”. This “container” holds your element. When you add new elements it creates new “container” and links it to a different “container” in the LinkedList. That is where it gets its name from. With the nature of how these work, it is more efficient to use a LinkedList when you want access to the first or last element of the array.
To use LinkedList you must import java.util.LinkedList’
import java.util.LinkedList;
class Main {
public static void main(String args[]) {
//This creates a new LinkedList of Strings that is empty
LinkedList<String> dogs = new LinkedList<String>();
//This adds new elements to our LinkedList
dogs.add("Ada");
dogs.add("Taz");
dogs.add("Snoopy");
//This removes the last element of our LinkedList
dogs.removeLast();
//Both lines will print the first element of our LinkedList
System.out.println(dogs.get(0));
System.out.println(dogs.getFirst());
}
}
HashMap
HashMaps are a bit different from what we talked about before. HashMaps can be think of as key
value
pairs. Before, when we wanted something we would get it by calling its index. HashMaps do not use an index to get values like Arrays do. To get a value from a HashMap you access it by the key associated with that value. You can have different data types for keys and values in a hasMap. This means you can have a string key
and a int value
if you wanted. HashMaps can make accessing specific values easy and quick.
To use HashMaps you must import java.util.Hashmap;
When creating a HashMap we pass in the data type of both the key and value inside of <key, value>
. These are objects so we will need to use the associated wrappers for primitive data types. The first object passed will be the key data type and the second will be the value data type.
import java.util.HashMap;
class Main {
public static void main(String args[]) {
//creates a HashMap called studentGrades that have a key that is a string and a value that is a char
HashMap<String, Character> studentGrades = new HashMap<String, Character>();
//adding to HashMap
studentGrades.put("Tripp White", 'A');
studentGrades.put("Class Clown", 'F');
studentGrades.put("Teachers Pet", 'C');
//changes the Teachers Pet to an A for good behavor ; )
studentGrades.replace("Teachers Pet", 'A');
//prints out the value of "Teachers Pet" key in the studentGrades HashMap
System.out.println(studentGrades.get("Teachers Pet"));
}
}
That is a basic outline of Arrays, ArrayList, LinkedList, and HashMaps. I hope this helps you in understanding Java and build some great applications. As always look at the documentation for a full description and the many methods that can be used with each one.
Posted on September 13, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024