So far we’ve only stored single values in a variable, but arrays let us store multiple values
inside a single variable. To declare an array, you need a type keyword (e.g. char), followed by
the name of the array, along with square brackets, and then assign it to a new object of that
class, along with the size of the array in square brackets.
public class Main { public static void main(String[] args) { char vowels[] = new char[5]; // declaration
vowels[0] = 'a'; // definition vowels[1] = 'e'; vowels[2] = 'i'; vowels[3] = 'o'; vowels[4] = 'u';
System.out.println(vowels[2]); // print out an element in the array }}When you use println to print an element in an array, that element is printed as a string,
which means before printing the whole array you have to convert it into a string — by importing
java.util.Arrays and using the toString() method.
import java.util.Arrays;
public class Main { public static void main(String[] args) { char vowels[] = new char[5];
vowels[0] = 'a'; vowels[1] = 'e'; vowels[2] = 'i'; vowels[3] = 'o'; vowels[4] = 'u';
System.out.println(Arrays.toString(vowels)); }}Instead of declaring and defining your array on separate lines, you can do it all in one line.
import java.util.Arrays;
public class Main { public static void main(String[] args) { char vowels[] = {'a', 'e', 'i', 'o', 'u'}; // declare and define it
vowels[1] = 'f'; // change the element in the array at index 1
System.out.println(Arrays.toString(vowels)); }}Array methods
Finding the length of an array
public class Main { public static void main(String[] args) { char vowels[] = {'a', 'e', 'i', 'o', 'u'};
System.out.println(vowels.length); }}Sorting an unsorted array
import java.util.Arrays;
public class Main { public static void main(String[] args) { char vowels[] = {'e', 'i', 'a', 'u', 'o'};
int startingIndex = 1; int endingIndex = 3;
Arrays.sort(vowels); // sorts the array Arrays.sort(vowels, startingIndex, endingIndex); // sorts from index 1 to 3 (non-inclusive)
System.out.println(Arrays.toString(vowels)); }}Searching for a value in an array
import java.util.Arrays;
public class Main { public static void main(String[] args) { char vowels[] = {'a', 'e', 'i', 'o', 'u'}; char key = 'o';
int startingIndex = 0; int endingIndex = 4;
Arrays.sort(vowels); // binarySearch only works on sorted arrays
int foundItemIndex = Arrays.binarySearch(vowels, key); // returns the index of the key if found
int anotherFoundItemIndex = Arrays.binarySearch(vowels, startingIndex, endingIndex, key); // can specify the start and end of the search
System.out.println(foundItemIndex); }}Filling an array
You can fill an array with a value of your choice.
import java.util.Arrays;
public class Main { public static void main(String[] args) { char vowels[] = {'a', 'e', 'i', 'o', 'u'};
Arrays.fill(vowels, 'x');
System.out.println(Arrays.toString(vowels)); // outputs [x, x, x, x, x]
// this fill method also works with a starting and ending index }}Creating a copy of an array
import java.util.Arrays;
public class Main { public static void main(String[] args) { int numbers[] = {1, 2, 3, 4, 5};
int copyOfNumbers[] = Arrays.copyOf(numbers, numbers.length); // original array, then the length of the new array
int startingIndex = 1; int endingIndex = 3; // can also serve as the length of the new array
int anotherCopyOfNumbers[] = Arrays.copyOfRange(numbers, startingIndex, endingIndex); // same as above but you specify start/end indexes instead of a length
System.out.println(Arrays.toString(numbers)); System.out.println(Arrays.toString(copyOfNumbers)); }}Comparing arrays
import java.util.Arrays;
public class Main { public static void main(String[] args) { int numbers[] = {1, 2, 3, 4, 5}; int copyOfNumbers[] = Arrays.copyOf(numbers, numbers.length);
System.out.println(Arrays.equals(numbers, copyOfNumbers)); // outputs true }}