![]() |
| Java One Dimensional Array Explained with Examples |
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.
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}:
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
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 Example in Java
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
Accessing Java Array Elements using for Loop
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
Example of user-input Array in Java
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
Change Value of Element in Array Java
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.

