Topics Covered
- what is an array
- how to initialize an array
- how to add elements to an array
- how to print the contents of an array
What is an array?
An array is basically a container of objects of the same type. The rule of creating an array of objects is basically a must. During creation of an array, it must be declared what type should it hold, and this must be followed strictly without fail otherwise a compilation problem or runtime errors will occur. What I mean to say is, if we declare an array of Integers and we add an element that is of type String? What would happen? It would result to compilation problem.
How to define and instantiate an array in java?
Those objects in an array is called an element. And each element can be called through array index.
The index of an array starts at 0 thus the first element of an array is located at index 0. Moreover the last element is equal to size of an array – 1. This is one of the fundamentals of an array that most programmers get confused on.
Examples
There are so much into arrays, that’s why let’s do it by providing an example
Below are some examples and some brief explanation.
int[] arrayInt = new int[5];
From the above code snippet, that how you instantiate array. As you can see we have used an int data type on the left side of the statement and beside it is the opening and closing square bracket which tells the compiler that this is an array of int and the variable name of the array is arrayInt. On the right side of.
// array of Strings of size 2 since values were already declared during initialization
String[] arrayString = new String[] {"test","1234"};
// multi dimensional array of strings. Below is a sample declaration. Basically we have an array with 5 elements. These elements are array with dimension 2.
String[][] arrayString = new String[5][2];
// example code in instantiating an array of objects
Object[] arrayObject = new Object[2];
How to access the elements inside an array
From the earlier part of this tutorial, we have already showed examples on how to instantiate an array. We have also tackled multi dimensional arrays. But what would be the use of these arrays? That’s what we gonna tackle on this part.
// access the elements inside the array
int index = 0; System.out.println(arrayString[index]);
// access all the elements inside the array using for loop
String[] arrayString = new String[]{"one","two"}; for(int index =0; index < arrayString.length; index++){ System.out.println("element at index "+index+" is "+arrayString[index]); }
// print all contents of a java array using advance for loop (for-each)
Integer[] integerArray = new Integer[]{1000,200}; for(Integer intNumber : integerArray){ System.out.println("value " + intNumber); }
How to copy elements of an array to another array
Using the arraycopy() method of System class, we would be able to copy all elements of an array or only specific elements of an array efficiently.
Syntax:
arraycopy(Object src, int srcPos,Object dest, int destPos, int length)
Example:
package com.teknoscope.tutorial; public class ArrayCopy { /** * This java sample code shows how to copy an array into another array * Property of teknoscope.com * You are free to modify for your personal use * All Rights Reserved * Version 1.0 * 08/02/2013 */ public static void main(String[] args) { Integer[] intArray = new Integer[]{1,2,3,4,5}; Object[] objectArray=new Object[5]; System.arraycopy(intArray, 0, objectArray, 0, 5); for(Object o:objectArray){ System.out.println(o.toString()); } } }
How to replace a specific element of an array
This is very simple scenario we just have to assign a value to the target index
String[] arrayString = new String[]{"one","two","three"}; arrayString[1] = "five"; //assuming that we want to change the 2nd element of our array which is at index 1
I have also written a compilation of java tutorials on java array examples. This compilation contains source codes for specific scenarios which you can readily modify to suit your needs.