One Dimensional Array Program in Java – In this article, we will detail in on all the different methods to describe the one-dimensional array program in Java with suitable examples & sample outputs.
The methods used in this article are as follows:
- Using Standard Method
- Using Scanner
- Using String
An array is a collection of elements of one specific type in a horizontal fashion. The array in contention here is that of the one-dimensional array in Java programming.
Anything having one-dimension means that there is only one parameter to deal with. In regular terms, it is the length of something. Similarly, as far as an array is concerned, one dimension means it has only one value per location or index.
One-dimensional array in Java programming is an array with a bunch of values having been declared with a single index.
As you can see in the example given above, firstly, you need to declare the elements that you want to be in the specified array.
Secondly, the location of each element needs to particularized as well, since that is where the elements will be stored respectively.
- Thirdly, you need to declare the array accordingly.
As it is visible, the three elements that have been listed simultaneously are as follows:
- 10
- 20
- 30
Hence, the result is printed as 10 20 30 respectively in single lines, since it is a one-dimensional array.
Thus, the methods used to do so in Java are as follows:
One Dimensional Array – Using Standard Method
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class OnedimensionalStandard { public static void main(String args[]) { int[] a=new int[3];//declaration a[0]=10;//initialization a[1]=20; a[2]=30; //printing array System.out.println("One dimensional array elements are"); System.out.println(a[0]); System.out.println(a[1]); System.out.println(a[2]); } } |
Output:
1 2 3 4 |
One dimensional array elements are 10 20 30 |
Using Scanner
- Read the array length as sc.nextInt() and store it in the variable len and declare an array int[len].
2) To store elements in to the array for i=0 to i<length of an array read the element using sc.nextInt() and store the element at the index a[i].
3) Display the elements of an array for loop iterates from i=0 to i<length of an array print the array element a[i].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.util.*; class OnedimensionalScanner { public static void main(String args[]) { int len; Scanner sc=new Scanner(System.in); System.out.println("Enter Array length : "); len=sc.nextInt(); int a[]=new int[len];//declaration System.out.print("Enter " + len + " Element to Store in Array :\n"); for(int i=0; i<len; i++) { a[i] = sc.nextInt(); } System.out.print("Elements in Array are :\n"); for(int i=0; i<len; i++) { System.out.print(a[i] + " "); } } } |
Output:
1 2 3 4 5 6 7 8 9 |
Enter Array length : 4 Enter 4 Element to Store in Array : 1 2 3 4 Elements in Array are : 1 2 3 4 |
Using For Loop – One Dimensional Array
1 2 3 4 5 6 7 8 9 10 11 12 |
class OnedimensionalLoop { public static void main(String args[]) { int a[]={10,20,30,40,50};//declaration and initialization System.out.println("One dimensional array elements are :\n"); for(int i=0;i<a.length;i++) { System.out.println("a["+i+"]:"+a[i]); } } } |
Output:
1 2 3 4 5 6 7 |
One dimensional array elements are : a[0]:10 a[1]:20 a[2]:30 a[3]:40 a[4]:50 |
Using String
- We declared one-dimensional string array with the elements strings.
2) To print strings from the string array. for i=0 to i<length of the string print string which is at the index str[i].
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class OneDimensionString { public static void main(String[] args) { //declare and initialize one dimension array String[] str = new String[]{"one", "two", "three", "four"}; System.out.println("These are elements of one Dimensional array."); for (int i = 0; i < str.length; i++) { System.err.println(str[i] + " "); } } } |
Output:
1 2 3 4 5 |
These are elements of one Dimensional array. one two three four |