Arrays

Arrays in Java, like Python lists, Javascript arrays, and C arrays, store ordered values. Java arrays cannot be resized without creating a new array and making a copy.

We'll cover the following...

Java arrays, like C arrays, are more limited than their counterparts in Python or Javascript. Java arrays cannot be resized once created, and you must declare the type of the variables that the array will store in advance. The ArrayList class is more flexible, and a frequent alternative to arrays, but first let’s see how to use arrays.

An example of using arrays:

Press + to interact
class ArrayExample {
public static void main(String[] args) {
// an array of ints
int[] myNumbers = {10, 15, 20, 25, 30};
System.out.println(myNumbers[2]);
// arrays have an instance variable that stores
// the length of the array:
System.out.println(myNumbers.length);
// Arrays can be of any type a variable can be:
String[] myStrings = {"Narnia", "Oz", "Neverland"};
System.out.println("The Wizard of " + myStrings[1]);
// arrays can be modified
myStrings[2] = "Wonderland";
}
}

Notice that although you can determine the length of a string using the method someString.length(), you ...