0% found this document useful (0 votes)
10 views

1.8.2 Array

Arrays in Java are collections of similar data types that allow for indexed access and are created as objects during runtime. They can hold both primitive types and object references, with a fixed length once initialized. The document also covers array declaration, initialization, processing, and the use of two-dimensional arrays.

Uploaded by

Infinite Elixir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

1.8.2 Array

Arrays in Java are collections of similar data types that allow for indexed access and are created as objects during runtime. They can hold both primitive types and object references, with a fixed length once initialized. The document also covers array declaration, initialization, processing, and the use of two-dimensional arrays.

Uploaded by

Infinite Elixir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

ARRAYS IN JAVA

What are Arrays in Java?


An array is a collection of similar types of data. It is a container that holds data (values) of
one single type.
For example, you can create an array that can hold 100 values of integer type. In Java, arrays are a
fundamental construct that allows you to store and access a large number of values conveniently.

Arrays are objects which store multiple variables of the same type. It can hold primitive types as
well as object references. In fact most of the collection types in Java which are the part
of java.util package use arrays internally in their functioning. Since Arrays are objects, they are
created during runtime .The array length is fixed.

Arrays in Java are homogeneous data structures implemented in Java as objects. Arrays store one or
more values of a specific data type and provide indexed access to store the same. A specific element
in an array is accessed by its index. Arrays offer a convenient means of grouping related information.

Obtaining an array is a two-step process.

● First, you must declare a variable of the desired array type


● Second, you must allocate the memory that will hold the array, using new, and assign it to the array
variable

General Form of Java Array Initialization


CS8392

OBJECT ORIENTED PROGRAMMING

General Form of Java Array Initialization

Example
CS8392

OBJECT ORIENTED PROGRAMMING

Arrays can be initialized when they are declared. The array will automatically be created large
enough to hold the number of elements you specify in the array initializer. There is no need to use new.
Features of Array

● Arrays are objects

● They can even hold the reference variables of other objects

● They are created during runtime

● They are dynamic, created on the heap

● The Array length is fixed

Array Declaration
First let us get in to declaration of array which holds primitive types. The declaration of
array states the type of the element that the array holds followed by the identifier and square braces
which indicates the identifier is array type.

Example 1: Declaring an array which holds elements of integer type.

int MyArray[];

The above statement creates an array reference on the stack.

Declaring Array

int []aiMyArray;
int aiMyArray[];
CS8392

OBJECT ORIENTED PROGRAMMING

Initialization:

Creating Arrays
You can create an array by using the new operator with the following syntax

Syntax
arrayRefVar = new dataType[arraySize];
The above statement does two things

● It creates an array using new dataType[arraySize].

● t assigns the reference of the newly created array to the variable arrayRefVar.

Declaring an array variable, creating an array, and assigning the reference of the array to the variable can be c

dataType[] arrayRefVar = new dataType[arraySize];


Alternatively you can create arrays as follows
dataType[] arrayRefVar = {value0, value1, ..., valuek};
The array elements are accessed through the index. Array indices are 0-based; that is, they
start from 0 to arrayRefVar.length-1.
CS8392

OBJECT ORIENTED PROGRAMMING

Example

Following statement declares an array variable, myList, creates an array of 10 elements of double type and as

double[] myList = new double[10];


Following picture represents array myList. Here, myList holds ten double values and the
indices are from 0 to 9.

Processing Arrays
When processing array elements, we often use either for loop or foreach loop because all of
the elements in an array are of the same type and the size of the array is known.
Example
Here is a complete example showing how to create, initialize, and process arrays
public class TestArray {
public static void main(String[] args) {
double[] myList = {1.9, 2.9, 3.4, 3.5};
// Print all the array elements
for (int i = 0; i < myList.length; i++) {
System.out.println(myList[i] + " ");
}
CS8392

OBJECT ORIENTED PROGRAMMING

// Summing all elements


double total = 0;
for (int i = 0; i < myList.length; i++) {
total += myList[i];
}
System.out.println("Total is " + total);
// Finding the largest element
double max = myList[0];
for (int i = 1; i < myList.length; i++) {
if (myList[i] > max) max = myList[i];
}
System.out.println("Max is " + max);
}
}
OUTPUT
1.9
2.9
3.4
3.5
Total is 11.7
Max is 3.5

The for each Loops


Introduced a new for loop known as foreach loop or enhanced for loop, which enables you to
traverse the complete array sequentially without using an index variable.

Example
The following code displays all the elements in the array myList –
public class TestArray
{
public static void main(String[] args)
{
double[] myList = {1.9, 2.9, 3.4, 3.5};
CS8392

OBJECT ORIENTED PROGRAMMING

// Print all the array elements


for (double element: myList)
{
System.out.println(element);

}}}

This will produce the following result

Output
1.9
2.9
3.4
3.5

Passing Arrays to Methods


Just as you can pass primitive type values to methods, you can also pass arrays to methods.
For example, the following method displays the elements in an int array

Example
public static void printArray(int[] array)
{
for (int i = 0; i < array.length; i++)
{
System.out.print(array[i] + " ");
}
}

You can invoke it by passing an array. For example, the following statement invokes the printArray method t

Example
printArray(new int[]{3, 1, 2, 6, 4, 2});

Returning an Array from a Method

A method may also return an array. For example, the following method returns an array that is the reversal of
CS8392

OBJECT ORIENTED PROGRAMMING

Example
public static int[] reverse(int[] list) {
int[] result = new int[list.length];

for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {


result[j] = list[i];
}
return result;
}

Two Dimensional Array


The Two Dimensional Array in Java programming language is nothing but an Array
of Arrays. In Java Two Dimensional Array, data stored in row and columns, and we can access the
record using both the row index and column index .
If the data is linear, we can use the One Dimensional Array. However, to work with
multi-level data, we have to use the Multi-Dimensional Array. Two Dimensional Array in Java is the
simplest form of Multi-Dimensional Array.
CS8392

OBJECT ORIENTED PROGRAMMING

Example
class TwodimensionalStandard
{
public static void main(String args[])
{
int[][] a={{10,20},{30,40}};//declaration and initialization
System.out.println("Two dimensional array elements are");
System.out.println(a[0][0]);
System.out.println(a[0][1]);
System.out.println(a[1][0]);
System.out.println(a[1][1]);
}
}
Output:

10

20
CS8392

OBJECT ORIENTED PROGRAMMING

30

40

You might also like