Second lab was All about the Array
And we came to have a huge amount of interesting facts about the Array
Here is some of things I have learned in lab 02
An array is a group of like-typed variables that are referred to by a common name.Arrays in Java work differently than they do in C/C++. Following are some important point about Java arrays.
In Java all arrays are dynamically allocated.(discussed below)
Since arrays are objects in Java, we can find their length using member length. This is different from C/C++ where we find length using sizeof.
A Java array variable can also be declared like other variables with [] after the data type.
The variables in the array are ordered and each have an index beginning from 0.
Java array can be also be used as a static field, a local variable or a method parameter.
The size of an array must be specified by an int value and not long or short.
The direct superclass of an array type is Object.
Every array type implements the interfaces Cloneable and java.io.Serializable.
Array can contains primitives data types as well as objects of a class depending on the definition of array. In case of primitives data types, the actual values are stored in contiguous memory locations. In case of objects of a class, the actual objects are stored in heap segment.
Creating, Initializing, and Accessing an Array
One-Dimensional Arrays :
The general form of a one-dimensional array declaration is
type var-name[];
OR
type[] var-name;
An array declaration has two components: the type and the name. type declares the element type of the array. The element type determines the data type of each element that comprises the array. Like array of int type, we can also create an array of other primitive data types like char, float, double..etc or user defined data type(objects of a class).Thus, the element type for the array determines what type of data the array will hold.
Example:
// both are valid declarations
int intArray[];
or int[] intArray;
byte byteArray[];
short shortsArray[];
boolean booleanArray[];
long longArray[];
float floatArray[];
double doubleArray[];
char charArray[];
// an array of references to objects of
// the class MyClass (a class created by
// user)
MyClass myClassArray[];
Object[] ao, // array of Object
Collection[] ca; // array of Collection
. // of unknown type
Although the above first declaration establishes the fact that intArray is an array variable, no array actually exists. It simply tells to the compiler that this(intArray) variable will hold an array of the integer type. To link intArray with an actual, physical array of integers, you must allocate one using new and assign it to intArray.
Instantiating an Array in Java
When an array is declared, only a reference of array is created. To actually create or give memory to array, you create an array like this:The general form of new as it applies to one-dimensional arrays appears as follows:
var-name = new type [size];
Here, type specifies the type of data being allocated, size specifies the number of elements in the array, and var-name is the name of array variable that is linked to the array. That is, to use new to allocate an array, you must specify the type and number of elements to allocate.
Example:
int intArray[]; //declaring array
intArray = new int[20]; // allocating memory to array
OR
int[] intArray = new int[20]; // combining both statements in one
Arrays of Objects
An array of objects is created just like an array of primitive type data items in the following way.
Student[] arr = new Student[7]; //student is a user-defined class
The studentArray contains seven memory spaces each of size of student class in which the address of seven Student objects can be stored.The Student objects have to be instantiated using the constructor of the Student class and their references should be assigned to the array elements in the following way.
Student[] arr = new Student[5];
// Java program to illustrate creating an array of
// objects
.
class Student
{
. public int roll_no;
. public String name;
. Student(int roll_no, String name)
. {
. this.roll_no = roll_no;
. this.name = name;
. }
}
.
// Elements of array are objects of a class Student.
public class GFG
{
. public static void main (String[] args)
. {
. // declares an Array of integers.
. Student[] arr;
.
. // allocating memory for 5 objects of type Student.
. arr = new Student[5];
.
. // initialize the first elements of the array
. arr[0] = new Student( 1,”Ali”);
.
. // initialize the second elements of the array
. arr[1] = new Student(2,”Rafay”);
.
. // so on…
. arr[2] = new Student(3,”zain”);
. arr[3] = new Student(4,”Faiz”);
. arr[4] = new Student(5,”Umar”);
.
. // accessing the elements of the specified array
. for (int i = 0; i < arr.length; i++)
. System.out.println(“Element at “ + i + “ : “ +
. arr[i].roll_no +” “+ arr[i].name);
. }
}
Output:
Element at 0 : 1 Ali
Element at 1 : 2 Rafay
Element at 2 : 3 zain
Element at 3 : 4 Faiz
Element at 4 : 5 umar
And we left the lab with byeee byeee!
