You are on page 1of 28

Java Arrays

Arrays
• Is a collection of variables of the same type, referred to by a common
name.
• Arrays are data structures consisting of related data items of the same
type. Each item of an array is called an element. The position number
of the element is called the element’s index or subscript. The first
element in every array has index zero.
Consider the example:
int num1 = 1;
int num2 = 5;
int num3 = 6;

int num [ ] = {1, 5, 6};


INDEX num[0] num[1] num[2]
VALUES 1 5 6
Logical presentation of Array
Declaring an Array
• Before you can create an array, you must first declare a variable that
refers to the array.
• This variable declaration should indicate the type of elements that are
stored by the array followed by a set of empty brackets.
• Syntax in declaring an array:
dataType[] arrayName;
• Example:
int[ ] num;
String[ ] msg;
• In java it is also acceptable to place the brackets in the variable name
rather than the type. For example consider the following two
statements create an array of String elements:
String[ ] names; // an array of string elements
String names[ ]; //another array of string elements
Array is an object
• It follows that arrayName is a reference variable. Therefore the above
statements only declares a reference variable. To store a data, we
must create the array and specify its length with a constructor
statement. This process in Java is called instantiation (the Java word
for creates). In order to instantiate an object, we need to use a
constructor for this.
Accessing an Arrays
• To access an array element, or a part of the array, you use a number
called an index or a subscript.
• Syntax in accessing an array:
arrayName[indexExp]
• Example:
int list [] = new int[10];
list[3]=69; //assigns a value of 69 to the 3rd element in the array
System.out.println( list[3] ); //display the 3rd element in the array
Array length
• This length determines the number of elements that can be stored in
the array. It can access the length of an array by using the length field
of the array variable.
• Syntax in accessing an array length:
arrayName.length
• Example:
int list [] = new int[10];
System.out.println(list.length ); //displays the length of the array
One-Dimensional Array
• Is a list of related variables. Such lists are common in programming.
• To declare a one-dimensional array:
Type array-name[ ] = new type [size];
One Dimensional Array
class ArrayEx {
public static void main (String args[]) {
String Course [] = new String [3];
Course[0] = "Bachelor of Science in Information Technology";
Course[1] = "Bachelor of Science in Information Systems";
Course[2] = "Bachelor of Science in Computer Science";
System.out.println ("Course: " + Course[1]);
}
}
One Dimensional Array
class ArrayEx {
public static void main (String args[]) {
int sample [] = new int[5];
int x;

for (x=0;x<5;x=x+1)
{
sample[x] = x;
}
for (x=0;x<5;x=x+1)
{
System.out.println ("Array Number [" + x + "]: " + sample[x]);
}
}
}
Output:
class ArrayEx {
public static void main (String args[]) {
int num [] = new int[5];
int min=0, max=0, x;
num[0] = 10;
num[1] = 0;
num[2] = 12;
num[3] = 14;
num[4] = 69;
min = max = num[0];
for (x=1; x<5; x++)
{
if (num[x] < min)
min = num[x];
if (num[x] > max)
max = num[x];
}
System.out.println ("Min and Max: " + min + " " + max);
}
}
Use Array Initializers
class ArrayEx {
public static void main (String args[]) {
int num [] = {12,44,145,1,124};
int min=0, max=0, x;
min = max = num[0];
for (x=1; x<5; x++)
{
if (num[x] < min)
min = num[x];
if (num[x] > max)
max = num[x];
}
System.out.println ("Min and Max: " + min + " " + max);
}
}
Demonstrate an Array overrun.

class ArrayEx {
public static void main (String args[]) {
int num [] = new int[5];
int x;
for (x=0; x<100; x=x+1)
{
num[x]=1;
System.out.println ("Num: " + x);
}

}
}
Sorting an Array
• Because a one-dimensional array organizes data into an indexable
linear list.
• The best known, simplest, and easiest to understand is called the
Bubble Sort.
Bubble Sort
• Gets its name from the way it performs the sorting operation. It uses
the repeated comparison and, if necessary, exchange of adjacent
elements in the array.
• In this process, small values move forward one end and large ones
toward the other end.
Example:
for (x=1; x<size; x++)
for (y=size-1; y >=x; y--) {
if (num[y-1] > num[y]) {//if out of order
//Exchange Elements
t = num[y-1];
num[y-1] = num[b];
num[y] = t;
}
}
class ArrayEx {
public static void main (String args[]) {
int num [] = {212,41,516,125,312};
int x, y, z, a;
int size;

size = 5; //no. of elements to sort


//display Original Array
System.out.print ("Original Array Stored: ");
for (a=0;a<size;a++)
{
System.out.print (" " + num[a]);
}
//Bubble Sort
for (x=1;x<size;x++)
for (y=size-1; y>=x;y--)
{
if (num[y-1]>num[y]) //if out of order
{
//exchange elements
z = num[y-1];
num[y-1] = num[y];
num[y] = z;
}
}
System.out.println (" ");
//display sorted array
System.out.print ("Sorted Array: ");
for (a=0;a<size;a++)
{
System.out.print (" " + num[a]);
}
}
}
Multidimensional arrays
• It is also called arrays of arrays.
• Aside from a special initializer, the syntax of multidimensional arrays is
already implied from the syntax for one dimensional arrays.
• Two dimensional arrays may be thought of as a two dimensional table
or matrix.
• Syntax of Multi-dimensional array:
dataType[] arrayName=[subscript][subcript];
Example:
int[][] table = new int[2][4];

• This two-dimensional array will have two rows and four columns.

2 4 7 15
3 1 8 10
public class twodimenarray1 {

public static void main(String[] args) {

int t, i;
int table [] [] = new int [3] [4];

for (t=0; t<3; t++)


{
for (i=0; i<4; i++)
{
table [t] [i] = (t*4)+i+1;
System.out.print (table[t][i] + " ");
}
System.out.println (" ");
}
}
}
Arrays of three or more Dimensions
Type name [] [] … [] = new type [size1] [size2] … [sizeN];

For example, the following declaration creates a 4x10x3 dimensional


integer array:

int multidim [] [] [] = new int [4] [10] [3];


public class twodimenarray1 {

public static void main(String[] args) {


int sqrs [] [] = {
{1, 1}, {2, 4}, {3, 9}, {4, 16},
{5, 25}, {6, 36}, {7, 49}, {8, 64},
{9, 81}, {10, 100} };
int i, j;

for (i=0; i<10; i++)


{
for (j=0; j<2; j++)
System.out.print (sqrs[i][j] + " ");
System.out.println (" ");
}

}
}
Using the length member
• Because arrays are implemented as objects, each array has associated
with it a length instance variable that contains the number of
elements that the array can hold.
public class twodimenarray1 {

public static void main(String[] args) {


int i;
int nums1 [] = new int [10];
int nums2 [] = new int [10];

for (i=0; i<nums1.length; i++)


nums1[i] = i;

//copy nums1 to nums2


if (nums2.length >=nums1.length) //Use length to compare array sizes
for (i=0; i<nums1.length; i++)
nums2[i] = nums1[i];
for (i=0; i<nums2.length; i++)
System.out.print (nums2[i] + " ");

}
}
End…

You might also like