1DArray Notes&Programs
1DArray Notes&Programs
In this lesson, we will learn about One Dimensional Array in Java Programming
along with some examples.
What is One Dimensional Array (1D Array) in Java?
Example 1:
int a[]={12,18,6};
Example 2:
Note: If an array is initialized, without assigning any value, then the default value
of each cell of the array will be 0.
Store Numbers in a One Dimensional Array
To store the number in each cell of the array we can use the following syntax.
array_name[index]=value;
Example
a[0]=26;
a[1]=15;
a[2]=34;
Access Numbers in a One Dimensional Array
We can access any number stored in a 1D array using the following syntax.
array_name[index];
Example
System.out.println(a[0]+" "+a[1]+" "+a[2]);
Output: 26 15 34
Enter 10 numbers
11
15
28
31
49
54
72
81
93
14
List of even numbers
28 54 72 14
Here, you can see that we have run a for loop 10 times to store the user's input in
the array. After that we have run another for loop 10 times to access each
number from the array and print only the even numbers from it.
Example 2
Program to input 5 numbers in an array and print all the numbers from the
backside of the array. Example: 12 18 16 Output: 16 18 12
import java.util.Scanner;
public class Example
{ public static void main(String args[])
{ int a[]=new int[5], i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 5 numbers");
for(i=0; i<5; i++)
{ a[i]=sc.nextInt();
}
for(i=4; i>=0; i--)
{ System.out.print(a[i]+" ");
}
}// end of function }// end of class Output
Enter 5 numbers
48
21
97
64
53
53 64 97 21 48
Here, you can see that we have run a for loop 5 times to store the user's input in
the array. After that we have run another for loop in reverse order to print all the
numbers from the back side of the array.
Question 3.
Question 4.
Question 5.
Question 6.