How to create an array in Java
An array in Java is a type of variable that can be used to store multiple values. These values are stored based on a key that can then be used to look up this information.
Arrays can be useful for developers to store, arrange, and retrieve large amounts of data. Whether you’re tracking high scores in a computer game or storing information about clients in a database, an array is often the best choice.
Also read: Using arrays in Python
How do you create an array in Java? It all depends on the type of array you want to use!
How to create an array in Java
The word “array” is defined as a data structure made up of a collection of elements. These elements must be identified by at least one “index” or “key”.
There are several data objects in Java, so we could call them arrays. We’ll refer to the first as the “Java Array”. While this makes things a little more confusing, it actually resembles what we would call a “list” in many other programming languages!
This is the easiest way to think of a Java array: as a list of sequential values. Here, each value in the sequence is automatically assigned a key based on its relative position. The first index is always “0” and from there the number increases gradually with each new element.
However, unlike a list in Python, Java arrays are fixed in size. There is no way to remove or add elements to the array at run time. This limitation is great for optimized code, but of course it has some limitations.
To create this type of array in Java, simply create a new variable of your chosen data type with square brackets to indicate that it is actually an array. We then enter each value in curly brackets separated by commas. Values are then accessed using the index based on the order of this list.
String listOfFruit[] = {"apple", "orange", "lemon", "pear", "grape"}; System.out.println(listOfFruit[2]);
While it is not possible to change the size of a Java array, we can change certain values:
listOfFruit[3] = “melon”;
ArrayLists
If you need to use arrays that can be resized in Java, you can go for the ArrayList. An ArrayList is not that fast, but it gives you more flexibility at runtime.
To create an array list, you need to initialize it with the data type we selected. Then we can add each element individually using the add method. We also need to import ArrayList from the Java.util package.
import java.util.ArrayList; class Main { public static void main(String[] args) { ArrayList<String> arrayListOfFruit = new ArrayList<String>(); arrayListOfFruit.add("Apple"); arrayListOfFruit.add("Orange"); arrayListOfFruit.add("Mango"); arrayListOfFruit.add("Banana"); System.out.println(arrayListOfFruit); } }
Now we can add and remove elements in our code at any time. Note, however, that this will change the positions of all other values and their respective keys. So should I do this:
System.out.println(arrayListOfFruit.get(3)); arrayListOfFruit.add(2, "Lemon"); System.out.println(arrayListOfFruit.get(3));
I would get a different output every time I printed. Notice that we are using “get” to return values at certain indexes, and that I can add values in different positions by passing my index as the first argument.
How to create an array in Java using cards
Another type of array in Java is the map. A map is an associative array that uses key / value pairs that do not change.
For example, this is a perfect way to save phone numbers. Here you can use the numbers as values and the names of the contacts as an index. So “197701289321” could get the key “Jeff”. This makes it much easier for us to quickly find the data we need, even as we add and remove data from our list!
We do it:
import java.util.HashMap; import java.util.Map; Map<String, String> phoneBook = new HashMap<String, String>(); phoneBook.put("Adam", "229901239"); phoneBook.put("Fred", "981231999"); phoneBook.put("Dave", "123879122"); System.out.println("Adam's Number: " + phoneBook.get("Adam"));
As you can see then, a Java array is always an array, but an array is not always a Java array!
Using the multidimensional array in Java
Head not turning enough? Then take a look at the multidimensional array in Java!
This is a type of Java array with two “columns”.
Imagine your typical Java array is an Excel spreadsheet. If so, you would have created a table with only a single column. We could think of it as a “one-dimensional” database as the data only changes from top to bottom. We have as many lines as we want (1st Dimension), but only one column (the hypothetical 2ndnd Dimensions).
To add more columns, let’s just add a second set of square brackets. We then fill in the rows and columns. The resulting data structure can be thought of as an “array of arrays” where each element is itself an entire array!
In this example we are using integers (whole numbers):
int[][] twoDimensions = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, };
But we can take this idea even further by creating a three-dimensional array! This would be a Array of 2D arrays. You would build it like this:
int[][][] threeDimensions = { { {1, 2, 3}, {4, 5, 6} }, { {-1, -2, -3}, {-4, -5. -61}, } };
While this idea is difficult to come up with, try to imagine a database with three axes with cells moving in every direction.
How to Create an Array in Java! While many people reading this never have to deal with three-dimensional arrays, it just goes to show how powerful and adaptable Java really is.
In fact, the list of things you can achieve with Java is unlimited. How is the array list. Why not continue your education with one of the best resources for learning Java?