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

Array in Java

Arrays in Java are fundamental data structures that store multiple values of the same type in a single variable, allowing for efficient data management. They have fixed lengths, support contiguous memory allocation, and can store both primitive types and objects. Java arrays can be declared and defined easily, with three main types: one-dimensional, two-dimensional, and multi-dimensional arrays.

Uploaded by

opbabul2005
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)
2 views4 pages

Array in Java

Arrays in Java are fundamental data structures that store multiple values of the same type in a single variable, allowing for efficient data management. They have fixed lengths, support contiguous memory allocation, and can store both primitive types and objects. Java arrays can be declared and defined easily, with three main types: one-dimensional, two-dimensional, and multi-dimensional arrays.

Uploaded by

opbabul2005
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/ 4

Array in Java

Introduction
Arrays allow you to store multiple items of the same type together, making it easier to manage and
manipulate data.
An Array contains a limited amount of numbered spots (indices) for values. The length (or size) of an
Array is the amount of these spots, i.e. how many values can you place in the Array. The values in an
Array are called elements.
Arrays are used to store multiple values in a single variable, instead of declaring separate variables
for each value.
To declare an array, define the variable type with square brackets:
Arrays in Java are one of the most fundamental data structures that allow us to store multiple values
of the same type in a single variable. They are useful for storing and managing collections of data.
Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of
memory management. For primitive arrays, elements are stored in a contiguous memory location,
for non-primitive arrays, references are stored at contiguous locations, but the actual objects may
be at different locations in memory.
Key features of Arrays:
 Contiguous Memory Allocation (for Primitives): Java array elements are stored in
continuous memory locations, which means that the elements are placed next to each other
in memory.
 Zero-based Indexing: The first element of the array is at index 0.
 Fixed Length: Once an array is created, its size is fixed and cannot be changed.
 Can Store Primitives & Objects: Java arrays can hold both primitive types (like int, char,
boolean, etc.) and objects (like String, Integer, etc.)

What is an Array in Java?


An array refers to a data structure that contains homogeneous elements. This means that all the
elements in the array are of the same data type. Let's take an example:

This is an array of seven elements. All the elements are integers and homogeneous. The green box
below the array is called the index, which always starts from zero and goes up to n-1 elements. In this
case, as there are seven elements, the index is from zero to six.
There are three main features of an array:
1. Dynamic allocation: In arrays, the memory is created dynamically, which reduces the amount
of storage required for the code.
2. Elements stored under a single name: All the elements are stored under one name. This
name is used any time we use an array.
3. Occupies contiguous location: The elements in the arrays are stored at adjacent positions.
This makes it easy for the user to find the locations of its elements.

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.

Define an Array in Java


Arrays in Java are easy to define and declare. First, we have to define the array. The syntax for it is:

Here, the type is int, String, double, or long. Var-name is the variable name of the array.

Declare an Array in Java


These are the two ways that you declare an array in Java. You can assign values to elements of the
array like this:

We have declared an array arr of type integer. The size of the array is 5, meaning that it can have five
elements. The array is assigned with elements for each of the index positions. We'll run a for loop to
print the elements in the array. A counter variable "i" is used to increment the index position after
checking if the current index position is less than the length of the array.

Types of Arrays
There are three types of arrays. We use these types of arrays as per the requirement of the program.
These are:

One-dimensional Array
Also known as a linear array, the elements are stored in a single row. For example:
Two-dimensional Array
Two-dimensional arrays store the data in rows and columns:

In this, the array has two rows and five columns. The index starts from 0,0 in the left-upper corner to
1,4 in the right lower corner.

Multi-dimensional Array
This is a combination of two or more arrays or nested arrays. We can even use more than two rows
and columns using the following code:

Here, we are using three rows and three columns, but we are only using two for loops. Regardless of
how many rows and columns are entered, the number of for loops will always be two.

1. Array Declaration
To declare an array in Java, use the following syntax:
type[] arrayName;
 type: The data type of the array elements (e.g., int, String).
 arrayName: The name of the array.
Note: The array is not yet initialized.

2. Create an Array
To create an array, you need to allocate memory for it using the new keyword:
// Creating an array of 5 integers
int[] numbers = new int[5];
This statement initializes the numbers array to hold 5 integers. The default value for each element
is 0.

3. Access an Element of an Array


We can access array elements using their index, which starts from 0:
// Setting the first element of the array
numbers[0] = 10;
// Accessing the first element
int firstElement = numbers[0];
The first line sets the value of the first element to 10. The second line retrieves the value of the first
element.

4. Change an Array Element


To change an element, assign a new value to a specific index:
// Changing the first element to 20
numbers[0] = 20;

5. Array Length
We can get the length of an array using the length property:
// Getting the length of the array
int length = numbers.length;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy