Array and ArrayList
Array and ArrayList
Arrays
Single dimensional,
Multidimensional and
Jagged Array
Declaring and using ArrayList
class
Array
Definition:
An array is a group/collection of variables of the
same type that are referred to by a common name.
Arrays of any type can be created and may have
one or more dimensions.
A specific element in an array is accessed by its
index (subscript).
Examples:
Collection of numbers
Collection of names
Lovely Professional University,
Punjab
Introducing Arrays
double[] myList = new double[10];
myList
reference
Array reference
variable
Array element at
index 5
myList[0]
5.6
myList[1]
4.5
myList[2]
3.3
myList[3]
13.2
myList[4]
myList[5]
34.33
myList[6]
34
myList[7]
45.45
myList[8]
99.993
11123
Lovely Professional
myList[9] University,
Punjab
Element value
Creating Arrays
arrayRefVar = new datatype[arraySize];
Example:
myList = new double[10];
Example
STEP 1 : (Declaration)
int marks[];
marks null
STEP 2: (Memory Allocation)
marks = new int[5];
marks
0
marks[0]
0
marks[1]
0
marks[2]
marks[3]
marks[4]
10
marks[0]
0
marks[1]
0
marks[2]
0
marks[3]
0
marks[4]
Example
class Demo_Array
{
public static void main(String args[])
{
int marks[];
marks = new int[3];
marks[0] = 10;
marks[1] = 35;
marks[2] = 84;
Indexed Variables
The array elements are accessed through the index. The
array indices are 0-based, i.e., it starts from 0 to
arrayRefVar.length-1. In the example in Figure , myList holds
ten double values and the indices are from 0 to 9.
Each element in the array is represented using the following
syntax, known as an indexed variable:
arrayRefVar[index];
For Example:
myList[2] = myList[0] + myList[1];
Array Initializers
Declaring, creating, initializing in one step:
double[] myList = {1.9, 2.9, 3.4, 3.5};
This shorthand syntax must be in one statement.
This shorthand notation is equivalent to the following statements:
double[] myList = new double[4];
myList[0] = 1.9;
myList[1] = 2.9;
myList[2] = 3.4;
myList[3] = 3.5;
class Myarray
{
public static void main(String [] ar)
{
int a[]= {1,2,3,4,5,6,7,8,9};
int total = 0;
for(int i : a)
total +=i;
System.out.println("sum of the elements " +total);
}
}
Copying Arrays
Often, in a program, you need to duplicate an array or a part of
an array. In such cases you could attempt to use the assignment
statement (=), as follows:
list2 = list1;
list2
Contents
of list1
Contents
of list2
list1
list2
Garbage
Contents
of list1
Contents
of list2
Example:
System.arraycopy(sourceArray,
0, targetArray, 0,
sourceArray.length);
Lovely Professional University,
Punjab
+
+
+
+
+
"
"
"
"
"
");
");
");
");
");
}
}
Lovely Professional University,
Punjab
public classTest
{ public static void main(String[] args)
{
int x = 1;
// x represents an int
value
int[] y = new int[10];
// y represents an array of int
values
m(x,y) ;
System.out.println("x is "+ x);
System.out.println("y[0] is "+ y[0]);
}
public static void m (int number , int [] numbers)
{
number = 1001;// Assign a new value to number
numbers[0] = 5555;// Assign a new value to numbers[0]
}
}
Lovely Professional University,
Punjab
result[j] = list[i];
}
return result;
}
For example, the following statement returns a new array list2
with elements 6,5,4,3,2,1.
int [] list1 = {1,2,3,4,5,6};
int[] list2 = reverse(list1);
Lovely Professional University,
Punjab
Multidimensional Arrays
Declare/Create Two-dimensional Arrays
// Declare array ref var
dataType[][] refVar;
// Create array and assign its reference to
variable
refVar = new dataType[10][10];
// Combine declaration and creation in one
statement
dataType[][] refVar = new dataType[10][10];
// Alternative syntax
dataType refVar[][]
= new dataType[10][10];
Lovely Professional University,
Punjab
Lengths of Two-dimensional
Arrays
int[][] array = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
array.length
array[0].length
array[1].length
array[2].length
array[3].length
array[4].length
Lovely Professional University,
ArrayIndexOutOfBoundsException
Punjab
class TwoDimArr
{
public static void main(String args[])
{
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++)
{
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++)
{
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
Lovely Professional University,
Punjab
Jagged Arrays
Each row in a two-dimensional array is itself an
array. So, the rows can have different lengths.
Such an array is known as a jagged array. For
example,
int[][] matrix = {
{1, 2, 3, 4, 5},
{2, 3, 4, 5},
{3, 4, 5},
{4, 5},
{5}
};
matrix.length is 5
matrix[0].length is 5
matrix[1].length is 4
matrix[2].length is 3
matrix[3].length is 2
matrix[4].length is 1
java.util.ArrayList
+ArrayList()
+isEmpty(): boolean
+size(): int
import java.util.ArrayList;
class Box {
private double width;
private double height;
private double depth;
Box(Box ob) {
// pass object to constructor
width = ob.width;
height = ob.height;
depth = ob.depth;
}
Box(double w, double h, double d) {
width = w;
height = h;
depth = d;
}
Box() {
width = -1; // use -1 to indicate
height = -1; // an uninitialized
depth = -1; // box
}
Box(double len) {
width = height = depth = len;
}
double volume() {
return width * height * depth;
}
}
Lovely Professional University,
Punjab
{ System.out.println(name);
}*/
Thank You