[go: up one dir, main page]

0% found this document useful (0 votes)
2 views17 pages

Arrays in Java 6

An array in Java is a container object that holds a fixed number of values of a single type, with a length established at creation. Arrays allow random access to elements via indexes but have fixed sizes and cannot store heterogeneous data. They are useful in various applications, including data structures, mathematical problems, and processing tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views17 pages

Arrays in Java 6

An array in Java is a container object that holds a fixed number of values of a single type, with a length established at creation. Arrays allow random access to elements via indexes but have fixed sizes and cannot store heterogeneous data. They are useful in various applications, including data structures, mathematical problems, and processing tasks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Arrays in Java

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

Disadvantages of Arrays in Java


The size of the array cannot be increased or decreased once it is declared—
arrays have a fixed size
Java cannot store heterogeneous data. It can only store a single type of
primitives
Now that we understand what Java arrays are- let us look at how arrays in Java
are declared and defined.
Application areas of array
Arrays can be used to implement stack and queue data structures.
CPU scheduling algorithm can be implemented using an array
Arrays can be a better approach to implement Tree data structure.
The Heap, Map and Set use the binary search tree and the balanced
binary tree.
Various mathematical problems like matrices can be easily and
efficiently solved
Used to implement lookup The lookup table is itself an array that
stores pre-fetched or pre-calculated values that save the computation
time or fetching time.
Application conti…
Matrix operations can be implemented using arrays.
The large arrays can also be used to emulate in-program dynamic
memory allocation
We can use arrays to determine the partial or complete flow in the
code.
Arrays can be used in speech processing, where every speech signal
is an array.
The two-dimensional array also called a matrix is used in image
processing.
Steps to implement Arrays

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];

Initializing a Two Dimensional Array


When initializing a two-dimensional array, you enclose each row's initialization list in its own set of braces.
Here is an example:

int[][] values - { {10, 20, 30}, {40, 50, 60}, {70, 80, 90} };

Accessing element in a two-dimensional array

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.

You might also like