Arrays are the ancestors of [[Java - Lists | ArrayList]]. |
Unlike ArrayList, we don’t have to import anything and create a separate object for it to work - arrays are natively supported in Java!
We can create an array by two ways - either we create it with the elements, or we specify the datatype and size to add them later.
String[] brands = {"espresso", "java", "latte"};
String[] brands = new String[2];
The reference of an array is the information about the location of the data [i.e. its index].
When we use brands[1]
to access the String java
, we mean [go to the beginning of the array and then move forward 1 time. Then return a chunk of data the size of the variable.].
We can use array index to add, access, modify elements.
//adding elements
brands[0] = "espresso";
brands[1] = "java";
//modifying elements
brands[0] = "latte";
//accessing elements
System.out.println(array[0]);
System.out.println(array[1]);
Sometimes, we’d want to access the end of the list. We can already do this in ArrayList
using IndexOf()
, but now we want to do it with arrays.
If you use negative indices in array [like you would do in python], Java throws an ArrayIndexOutOfBoundsException
error at you. This is because Java doesn’t support them in the first place.
We use the workaround of using length
[not a method call] to get the size of the array and subtracting 1 from it.
int lastIndex = brands.length - 1;
You can use arrays as method parameters like this :
public static String printMenu(String[] brands)
{
//statements
}