Array in Java
An array however is an object that can store a group of values, all of the same type. Creating and using an array in java is similar to creating and using any other type of object. We declare a reference variable and use the new keyword to create an instance of the array in memory.
  • An array is a list of similar things
  • An array has a fixed:
    • name
    • type
    • length
  • These must be declared when the array is created.
  • An Array size declaratory must be a non-negative integer expression;
  • Once an array is created, its size cannot be changed.
  • the elements are accessed by their index
  • in Java, array indices start at 0.


Declaring Arrays

int  intArray[]; // declares intArray to be an array of integers
intArray = new int[10]; //sets up 10 integer-sized spaces in memory, labelled intArray[0] to intArray[9]
int intArray[] = new int[10]; //combines the two statements in one line

Alternate Array Declaration Notation:
Difference :
Int  [] salary, age, mobileNo; // These are three array variable.
Int  salary[], age, mobileNo; // Only salary is an array variable .
Assigning Values

refer to the array elements by index to store values in them.
intArray[0] = 10;
intArray[1] = 12;
intArray[2] = 16; 

  • can create and initialise in one step:
int intArray[] = {10, 12, 16, 17, 26, 33, 44, 11, 58, 54};
Iterating Through Arrays
  • for loops are useful when dealing with arrays:

for (int i = 0; i < intArray.length; i++) {
  intArray[i] = getSomeValue();

Arrays of Objects
  • So far we have looked at an array of primitive types.
    • integers
    • Could also use doubles, floats, characters…
  • Often want to have an array of objects
    • Students, Books, Loans ……
  • Need to follow 3 steps.
1.Declare the array
            private Student studentList[];

    • this declares studentList
2.Create the array
  studentList = new Student[10];
    • this sets up 10 spaces in memory that can hold references to Student objects
3.Create Student objects and add them to the array: studentList[0] = new Student("Sunil", "CS");
Array Length :

Each array in java has a public field name length. This field contains the numbers of elements in the array.
Int size = array_variable_name.length;
Reassigning Array Reference Variable
int array1[]=new int [10];
// Reassign array1 to a new Array
array1=new int[5]

Copying Array :

Because an array is an object, there is distinction between an array and the variable that references it. The array and reference variable are separate entities.
int []array1={2,4,6,8,10};
int [] array2=array1 // this does not copy array1.

Passing Arrays as Arguments to methods:
Public static void showArray(int [] array1)
For(int i=0; i<array1.length; i++)
When we pass an array as an argument, we simply pass the variable that references the array.
When an entire array is passed into a method, it is passed just as an object passed. The actual array itself is not passed but a reference to the array is passed into  the parameter. array_function


Latest Tutorial

Most Visited Tutorial