DEV Community

John Hill
John Hill

Posted on

Arrays in Java

If you have used a language similar to Ruby or JavaScript Arrays grow and shrink as you need them to. If you need to add or remove an item to an array there are methods that make it pretty simple. If you want an integer, string, and boolean value all in one array you could do that too. In Java with just a normal array it's not that simple.

In Java when you declare an array you also have to declare the type of array you want. Which means unlike other languages you can't have multiple datatypes in a single array. The array can only hold the type of information you set at declaration. This means you cannot have an array with a string, integer, and boolean all in a single array.

int[] arryInt; //declaration only
String[] strArr;

Also in Java when you create an array it is set at the length you set at creation. You can do this in two ways, you can set the length or create it with the initial information you need in it. You cannot make it bigger or smaller after creating it, based on you data. Once the length is set on the array that will be the length of that array for the entirety of the program. Say for instance you have an array which is the length if 10 but only have 8 number stored in it, the remain 2 indices would still have an integer stored in it, it would be default to 0. So this would be fine if we have a fixed set of data.

int[] intArr = new int[5];   //this declares and creates intArr 
                               with a length of 5
String nameArr = { Sam, Becky, Vincent, Katie }; 

In Java the first index of an array is index 0. In Java if you have an array with a length of 10 the last index would be 9. When you add an item to an array you just add this to the specific index where you want to put the information. If in our code we then try to add an item to index 10 we would get an IndexOutOfBounds error because although there are 10 indices the last index if 9 because we start at 0. Similarly to get or use the information out of the array we would just use the index in the array where the information is stored.

int arrInt = new int[10];
arrInt[0] = 200;
arrInt[3] = 43; 
arrInt[10] = 30;  //IndexOutOfBounds error no index 10

int x = arrInt[3];  //would assign 43 to x


What happens if we need an array but we don't know how many indices. We may need 1 or 2 or we may need 1000. We may need to add or remove an index based on our info and how it changes. Well we could set an array to the maximum number of indices we know we will need; which would take a large chunk of memory, some of which we may never even use. A solution to this is that we could use an ArrayList. ArrayList is part of the java.util package.

import java.util.ArrayList

Similar to a normal array it can still only hold one data type. Items in an ArrayList are actually objects so we need to use the wrapper class for each data type; for instance instead of int we need to use Integer. Where it differs is it can grow and shrink as we need. Say we needed an array of names. We can add names to the list using the .add() method. If we don't need a name on the list we can also remove the name using .remove().

ArrayList<String> students = new ArrayList<String>;

student.remove(1);  //this would remove the student at index 1 
                   which would be Sarah resizing our index to 2

There are other methods we can use on an ArrayList as well. The .set() method allows us to set the information in the ArrayList at a specific index. The .clear() method completely empties an ArrayList. The .size() will return the size of an ArrayList similar to the method you use on an array. You can also use the .add() to insert an item at a specific index.

ArrayList<String> students = new ArrayList<String>;

student.set(1, "Ruby");   //this would change Sarah to Ruby
student.add(1, "Sarah");  // this inserts Sarah into index 1 
                          above Ruby

student.size();          //this would return 4 

student.clear();         //this empties the ArrayList

While this is brief this should help give you the basics on Arrays and ArrayList and how to use them in Java.

Top comments (1)

jasterix profile image

This is great! One of the first challenges I faced learning Java was understanding how to recreate JavaScript arrays when solving algo challenges because Java has so many data structures to choose from