Initializing an array

One Dimensional Array

Initializing an array means assigning values to the array elements. It can be either primitive values (12, 75.5, true, etc) or object reference (Animal, Person, String, etc)

package com.ibytecode.arrays;
class Student
{
	String name;
	int id;
}

public class OneDReferenceArray {
	public static void main(String[] args) {
		Student[] students = new Student[3];
		
		Student studentOne = new Student();
		students[0] = studentOne; //Null reference
		students[1] = new Student();
		
		System.out.println(students[2]); //Null reference
		System.out.println(students[2].name);//NullPointerException
	}
}

null
Exception in thread “main” java.lang.NullPointerException
at com.ibytecode.arrays.TwoDReferenceArray.main(TwoDReferenceArray.java:17)

  • Individual elements in the array can be accessed using index.
    students[0] = new Student();
  • Index should start from 0 to (size-1) i.e.) if the size = 3 then index numbers will be from 0 through 2.
  • Trying to access index < 0 or index >= size will throw ArrayIndexOutOfBoundsException.

How many objects will be created in the memory?

  • One Student [] array object referenced by students
  • One Student object referenced by studentOne
  • One Student object referenced by students[1].

So, totally three objects in the memory and two reference variables will be created.

Two Dimensional (2D) Array

package com.ibytecode.arrays;
public class TwoDArray {
	public static void main(String[] args) {
		double[][] studentAvgs; //declaring a 2D array reference
		studentAvgs = new double[3][]; //creating an array object

		double[] student1Marks = new double[2];
		student1Marks[0] = 80.0;
		student1Marks[1] = 90.0;
		
		//1st element holds two double elements
		studentAvgs[0] = student1Marks; 
      
      		//2nd element holds an array of three double elements
		studentAvgs[1] = new double[3]; 
		studentAvgs[1][0] = 75.5;
		studentAvgs[1][1] = 65.5;
		studentAvgs[1][2] = 85.5;		
      
		//3rd element holds an array of one double element
		studentAvgs[2][0] = 95.5; 
	}
}

How many objects will be created in the memory?

  • One double[] [] array object referenced by studentAvgs
  • One double[] array object referenced by student1Marks
  • One double[] array object referenced by studentAvgs[1] with size 3.
  • One double[] array object referenced by studentAvgs[2] with size 1.

So, totally four objects in the memory and two reference variables will be created.

Initializing array elements using a loop

Here you can use length array variable to loop till the array size and assign values for each element.

Primitive Array

package com.ibytecode.arrays;
public class InitializingPrimitiveArray {
	public static void main(String[] args) {
		int[] numbers = new int[5];
		int j = 0;
		for(int i = 0; i < numbers.length; i++)	{
			numbers[i] = j += 10;
			System.out.print(numbers[i] + " ");
		}
	}
}

10 20 30 40 50

Reference Array

package com.ibytecode.arrays;
class Student {
	String name;
	int id;
}

public class InitializingReferenceArray {
	public static void main(String[] args) {
		Student[] students = new Student[3];
		System.out.println(students.length);
		for (int i = 0; i < students.length; i++) {
			students[i] = new Student();
		}
	}
}

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.