Arrays in Java 6
Arrays in Java 6
What is an array?
An array is a container object that holds a fixed number of
values of a single type.
The length of an array is established when the array is
created.
After creation, its length is fixed.
In Java, all arrays are dynamically allocated. (discussed
below)
Arrays may be stored in contiguous memory [consecutive memory
locations].
Since arrays are objects in Java, we can find their length using the
object property 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 has an index
beginning with 0.
Java array can also be used as a static field, a local variable, or a
method parameter.
Types of array
Advantages of Arrays in Java
Java arrays enable you to access any element randomly with the help of
indexes
It is easy to store and manipulate large data sets
Accessing
• Array_name[index]
Creating • X=arr[3] Initializing
• Data_type array_name[];
• int(or)
arr[]Data_type[]
= { 1, 5, 10,array_name;
15, 20 }; (or) (or)
• var-name = new type[size];
• Int number = new int[10];
• int intArray[]; (or) • number[0] = 11;
• int[] intArray; (or) • number[1] = 22;
• int[] Number = new •int[10];
number[2] = 33;
One Dimensional Array
A single-dimensional
array is a linear
structure of elements.
Holds same data
type and can be
accessed using an
index.
Example
public class Array_1D {
public static void main(String[] args) {
int[] a = {25, 50, 75, 100};
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
System.out.println(a[3]);
}
}
import java.util.Scanner;
public class Array_1D {
public static void main(String[] args) {
int[] a=new int[4];
Scanner sc=new Scanner(System.in);
for (int i=0;i<4;i++)
a[i]=sc.nextInt();
for (int i=0;i<4;i++)
System.out.println(a[i]);
}
}
Practice
1. Calculate the sum of all elements in an array.
2. Swap the end of elements in an array.
3. Find length of an array.
Two Dimensional
Array
A two-dimensional array,
also known as a 2D array,
is a collection of data
elements arranged in a grid-
like structure with rows and
columns.
Each element in the array
is referred to as a cell and
can be accessed by its row
and column indices/indexes.
2D-Array Declaration,
Initialization and Access
Array Declaration:
int[][] numbers = new int[3][5];
int[][] values - { {10, 20, 30}, {40, 50, 60}, {70, 80, 90} };
To access one of the elements in a two-dimensional array, you must use both subscripts. For example, the
following statement stores the number 20 in numbers[1][2]:
numbers[1][2] = 20;
public class Test1
{
public static void main(String[] args)
{
// Should print the values in table
int count = 0;
int table=new table[3][3];
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
table[row][col] = count;
count++;
System.out.print(table[row][col] + " ");
}
}
}
}
Practice
1. Find sum of given numbers
2. Find the maximum and minimum value from the given array.
3. Add two matrices
4. Multiple two matrices
5. Store item name, price per unit, quantity of sales and amount of sales in a matrix format and
display it.