Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type.
An array is used to store a collection of data, but it is often more useful to think of an array as a collection of
variables of the same type.
2. Table of content
What is array? Why it is used ? Declaration? Basic program.
Array01
Types of Array? 1-D Array. 2-D Array
Array type02
Problem resolution03
What we have discussed so far, some problem statements
Summary04
4. What is array?
Java provides a data structure, the
array, which stores a fixed-size
sequential collection of elements of
the same type.
An array is used to store a collection
of data, but it is often more useful to
think of an array as a collection of
variables of the same type.
5. Basic Declaration of Array
Sytax : Data-type var-name = new Data-type [size];
Example :
int intArray[]; //Array declaration
intArray = new int[20]; //Memory allocation
6. 6
Arrays
An array is an ordered list of values
0 1 2 3 4 5 6 7 8 9
79 87 94 82 67 98 87 81 74 91
An array of size N is indexed from zero to N-1
scores
The entire array
has a single name
Each value has a numeric index
This array holds 10 values that are indexed from 0 to 9
7. Basic terminologies
The values held in an array are called array elements
An array stores multiple values of the same type (the element type)
The element type can be a primitive type or an object reference
Therefore, we can create an array of integers, or an array of
characters, or an array of String objects, etc.
In Java, the array itself is an object
Therefore the name of the array is a object reference variable, and
the array itself must be instantiated
8. Bounds Checking
Once an array is created, it has a fixed size
An index used in an array reference must specify a valid element
That is, the index value must be in bounds (0 to N-1)
The Java interpreter throws an ArrayIndexOutOfBoundsException
if an array index is out of bounds
This is called automatic bounds checking
9. Initializer Lists
An initializer list can be used to instantiate and initialize an array in
one step
The values are delimited by braces and separated by commas
Examples:
int[] units = {147, 323, 89, 933, 540,
269, 97, 114, 298, 476};
char[] letterGrades = {'A', 'B', 'C', 'D', ’F'};
10. Accessing Java Array Elements
Example:
// accessing the elements of the specified array
for (int i = 0; i < arr.length; i++)
System.out.println("Element at index " + i + " : "+ arr[i]);
Each element in the array is accessed via its index. The index begins with 0 and ends at (total array
size)-1.
Syntax: array-name[Index-value]
11. Basic Code
class ArrayExample
{
public static void main (String[] args)
{
// declares an Array of integers.
int[] arr;
// allocating memory for 5 integers.
arr = new int[5];
// initialize the first elements of the array
arr[0] = 10;
// initialize the second elements of the array
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4] = 50;
// accessing the elements of the specified array
for (int i = 0; i < arr.length; i++)
System.out.println("Element at index " + i + “:” +arr[i] );
}
}
OUTPUT:
Element at index 0 : 10
Element at index 1 : 20
Element at index 2 : 30
Element at index 3 : 40
Element at index 4 : 50
12. Two-Dimensional Arrays
A one-dimensional array stores a list of elements
A two-dimensional array can be thought of as a table of elements, with
rows and columns
one
dimension
two
dimensions
13. Two-Dimensional Arrays
To be precise, a two-dimensional array in Java is an array of arrays
A two-dimensional array is declared by specifying the size of each dimension
separately:
int[][] scores = new int[12][50];
A two-dimensional array element is referenced using two index values
value = scores[3][6]
The array stored in one row or column can be specified using one index
14. 14
Multidimensional Arrays
An array can have many dimensions
If it has more than one dimension, it is called a multidimensional
array
Each dimension subdivides the previous one into the specified
number of elements
Each array dimension has its own length constant
Because each dimension is an array of array references, the arrays
within one dimension can be of different lengths
• these are sometimes called ragged arrays
16. Use case
WHY ARRAY?
Problem?
• Implement an application that will calculate 100
students exam average.
• Variables needed?
int studentA;
int studentB;
int studentC;
int studentD;
17. Some programs
• Write a Java program to find smallest and second smallest elements of a given array.
• Write a Java program to find all combination of four elements of a given array whose sum is equal
to a given value.
• Write a Java program to count the number of possible triangles from a given unsorted array of
positive integers.