Arrays in Java
Array is a container that holds objects that are of similar type.Array length is determined during it creation.Below diagram shows an array of ten objects.
Example of Array in Java
public class ArrayExample { public static void main(String args[]) { int [] arrayNumbers; arrayNumbers=new int[6]; arrayNumbers[0]=100; arrayNumbers[1]=700; arrayNumbers[2]=200; arrayNumbers[3]=800; arrayNumbers[4]=600; arrayNumbers[5]=400; System.out.println("Element at index 0: " + arrayNumbers[0]); System.out.println("Element at index 1: " + arrayNumbers[1]); System.out.println("Element at index 2: " + arrayNumbers[2]); System.out.println("Element at index 3: " + arrayNumbers[3]); System.out.println("Element at index 4: " + arrayNumbers[4]); System.out.println("Element at index 5: " + arrayNumbers[5]); } }
Declaring other types of array
Just like other variables array declaration has two components i.e
Array Datatype
Array Name
Array naming should follow naming conventions we discussed earlier.
NB:Array declaration does not create actually array but notifies compiler this variable will hold array data type.Below are other types of array declaration.
byte[] arrayOfBytes; short[] arrayOfShorts; long[] arrayOfLongs; float[] arrayOfFloats; double[] arrayOfDoubles; boolean[] arrayOfBooleans; char[] arrayOfChars; String[] arrayOfStrings;
Also putting the brackets at the end of array variable name is allowed
i.e int arrayNumbers[]however this is discouraged.
Array creation, Initializing and Accessing it
One way to create array is to use new keyword .eg
int [] arrayNumbers=new Array[5];
Assigning elements in an array can be done as show below.
arrayNumbers[0]=100; arrayNumbers[1]=700; arrayNumbers[2]=200; arrayNumbers[3]=800; arrayNumbers[4]=600; arrayNumbers[5]=400;
Elements in the array can then be accessed by using their index.
System.out.println("Element at index 0: " + arrayNumbers[0]); System.out.println("Element at index 1: " + arrayNumbers[1]); System.out.println("Element at index 2: " + arrayNumbers[2]); System.out.println("Element at index 3: "
Alternatively,many programmers consider creating and initializing array at the same time.
int arrayNumbers[]={200,300,400,500,600,700};
Creating a multidimensional array in Java
multidimensional arrays are also known as an array of arrays.They are created using two or more brackets [] [].
Example
public class MultiArrayExample { public static void main(String args[]) { String[][] marks = { {"John ", "Eric ", "Jessica "}, {"40", "70","80"} }; // Mr. Smith System.out.println(marks[0][0] + marks[1][0]); // Ms. Jones System.out.println(marks[0][1] + marks[1][1]); System.out.println(marks[0][2] + marks[1][2]); System.out.println("Array length is " +marks.length ); } }
Output
John 40Eric 70Jessica 80
Array length can be determined by
System.out.println("Array length is " +marks.length );
Using arraycopy
method
the arraycopy method can be used to copy data from one array into another.
Example
public class ArrayCopyMethodExample { public static void main(String args[]) { char copyFrom[]={'d','r','e','r','i','c','p','f'}; char[] copyTo = new char[4]; System.arraycopy(copyFrom, 2, copyTo, 0, 4); System.out.println(new String(copyTo)); } }Output
run:ericBUILD SUCCESSFUL (total time: 0 seconds)