Java One Dimensional Array Explained with Examples

0

Java One Dimensional Array Explained with Examples
Java One Dimensional Array Explained with Examples

📑 Table of Contents

One Dimensional Array in Java

Java Array: A one-dimensional array in java is a collection of data of same data types. It can store data of same data types means an integer array can store integer value and character array can store character value. Arrays is allow to store multiple values of the same data type in a single variable. 

We do not creating separate variables like student1, student2, student3, you can create one array to hold all students. Array can be used to group of data simultaneously. We can not fetch data from array value directly therefore we can use index point value. The indexing of array always start with 0 and index value is always integer value.

Key Points about One Dimensional Arrays

  • Index Starts: Java array first element is at index [0] and last at length[-1].
  • Fixed Size: A java array once created array size we do not be changed.
  • Same Data_Type: Array all elements must be of the same data type.
  • Length Property: Java use array_name.length to get array size.
  • Memory Efficient: Array elements stored in contiguous memory locations.

Java Array Declaration Syntax

  • Data_type: The array data type of data that the array will hold (int, float, char, etc.).
  • Array_name: The name you choose for the array.
  • Array_size: The number of elements the array can hold.
  • New Operator: You can use the new operator to allocate memory and then assign value.

data_type array_name[array_size]={Array_Values};
int a[]={10.40,60,80,70};
value=10,40,60,80,70;
index=a[0][1][2][3][4][5];
int a[]=new int[5];
a[0]=10
a[1]=40
a[2]=60
a[3]=80
a[4]=70

Java Array Declaration and Initialization

Array is the are several ways to initialize arrays. The choice of initialization method depends on the requirements of your java program. Here are some common methods for initializing arrays in java.

  • Array_Data_Type: The type of data that the array will hold (integer, float, character etc.).
  • Array_Name: The name you choose for the array.
  • Array_Size: The number of elements the array can hold. {data1, data2,data3 ... dataN}:

int a[]={20.40,60,80,90};
a[0]=20
a[1]=40
a[2]=60
a[3]=80
a[4]=90

The values to initialize the array with the number of elements inside the curly braces must match the size of the array.

We declare and initialize an integer array numbers with a size of 5 and we provide five values. If you don't specify all the values during initialization.

Example of One Dimensional Array in Java

class Jiocoding {
public static void main(String[] args) {
int a[]={10,40,60,80,70};
System.out.println("Value of a[0]:"+a[0]);
System.out.println("Value of a[1]:"+a[1]);
System.out.println("Value of a[2]:"+a[2]);
System.out.println("Value of a[3]:"+a[3]);
System.out.println("Value of a[4]:"+a[4]);
}
}

============OUTPUT============
Value of a[0]:10

Value of a[1]:40
Value of a[2]:60
Value of a[3]:80
Value of a[4]:70

Array Length Function in Java

Array Length: A java length can be used to find the length of an array using the length function. The length function returns the number of elements in the array.

Array Length Example in Java

class Jiocoding {
public static void main(String[] args) {
int [] data={10,40,60,80,70};
int array_size=data.length;
System.out.print("Array Length:"+array_size);
}
}
============OUTPUT============
Array Length:5


What is Iteration Statement in Java

A Iterating array in java is a common operation that allows you can access the each elements in the array. There are several common method to iterate through an array in java using a for loop.

Accessing Java Array Elements using for Loop  

class Jiocoding {
public static void main(String[] args) {
int a[]={10,40,60,80,70};
for(int i=0;i<a.length;i++){
System.out.println("Array Value of a["+i+"]:"+a[i]);
}
}
}

============OUTPUT============
Value of a[0]:10
Value of a[1]:40
Value of a[2]:60
Value of a[3]:80
Value of a[4]:70

Dynamic Array in Java

A dynamic array occurs when the values for the array elements are  determined and assigned during the execution of the program. This is useful when you need to read values from user input perform calculations to determine the array elements. Arrays initialized at runtime can be modified during program execution.

Example of user-input Array in Java  

import java.util.Scanner;
class Jiocoding{
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int a[]=new int[5];
int i,n;
System.out.print("Enter Array Size:");
n=s.nextInt();
for(i=0;i<n;i++)
{
    System.out.print("Enter Array Elements "+(i+1)+":");
    a[i]=s.nextInt();
}
for(i=0;i<n;i++)
{
System.out.println("Value of a["+i+"]="+a[i]);
}
}
}

============OUTPUT============
Enter Array Size:5

Enter Array Element[0]:67
Enter Array Element[1]:86
Enter Array Element[2]:45
Enter Array Element[3]:89
Enter Array Element[4]:43
Value of a[0]:67
Value of a[1]:86
Value of a[2]:45
Value of a[3]:89
Value of a[4]:43

Update Array Element in Java

A Array element update or change the value of an array element by assigning a new value to using index number.

Change Value of Element in Array Java  

class Jiocoding {
public static void main(String[] args) {
int a[]={10,40,60,80,70};
for(int i=0;i<a.length;i++){
System.out.println("Before Array Element["+i+"]:"+a[i]);
}
a[2]=100;
for(int i=0;i<5;i++){
System.out.println("After Change Array Element["+i+"]:"+a[i]);
}
}
}

============OUTPUT============
Before Array Element[0]:20
Before Array Element[1]:48
Before Array Element[2]:87
Before Array Element[3]:56
Before Array Element[4]:19

After Change Array Element[0]:20
After Change Array Element[1]:48
After Change Array Element[2]:100
After Change Array Element[3]:56
After Change Array Element[4]:19

Conclusion of One Dimensional Array in Java

Conclusion: This is a all about one-dimensional array in java language. In this article cover of every concept of related array. Array program you can understand simple easy method. If your any query or problems array related than you can contact me by email. I can more easy solution provide. 

Post a Comment

0 Comments
Post a Comment (0)
To Top