Вы находитесь на странице: 1из 5

7/29/2019 One Dimensional Array In Java - Tutorial & Example

BEGINNERS PROGRAMS : Selection Sort Java – Algorithm 2 Ways | JavaSorting

HOME JAVA DATA TYPES JAVA VARIABLES CONTROL STATEMENTS JAVA ARGUMENTS

C TUTORIALS

``

One Dimensional Array In Java – Tutorial & Example


 in Java Programs  Comments Offon One Dimensional Array In Java – Tutorial & Example

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.

All the methods will be explained with sample programs and suitable examples. The compiler has also been
added so that you understand the whole thing clearly.

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.

https://javatutoring.com/java-one-dimensional-array/ 1/7
7/29/2019 One Dimensional Array In Java - Tutorial & Example

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 class OnedimensionalStandard
2 {
3 public static void main(String args[])
4 {
5 int[] a=new int[3];//declaration
6 a[0]=10;//initialization
7 a[1]=20;
8 a[2]=30;

9 //printing array

https://javatutoring.com/java-one-dimensional-array/ 2/7
7/29/2019 One Dimensional Array In Java - Tutorial & Example

10 System.out.println("One dimensional array elements are");


11 System.out.println(a[0]);
12 System.out.println(a[1]);
13 System.out.println(a[2]);
14 }
15 }

Output:

1 One dimensional array elements are


2 10
3 20
4 30

Using Scanner
1. 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 import java.util.*;
2 class OnedimensionalScanner
3 {
4 public static void main(String args[])
5 {
6 int len;
7 Scanner sc=new Scanner(System.in);
8 System.out.println("Enter Array length : ");
9 len=sc.nextInt();
10 int a[]=new int[len];//declaration
11 System.out.print("Enter " + len + " Element to Store in Array :\n");
12 for(int i=0; i<len; i++)
13 {
14 a[i] = sc.nextInt();

15 }

https://javatutoring.com/java-one-dimensional-array/ 3/7
7/29/2019 One Dimensional Array In Java - Tutorial & Example

16 System.out.print("Elements in Array are :\n");


17 for(int i=0; i<len; i++)
18 {
19 System.out.print(a[i] + " ");
20 }
21 }
22 }

Output:

1 Enter Array length :


2 4
3 Enter 4 Element to Store in Array :
4 1
5 2
6 3
7 4
8 Elements in Array are :
9 1 2 3 4

Using For Loop – One Dimensional Array

1 class OnedimensionalLoop
2 {
3 public static void main(String args[])
4 {
5 int a[]={10,20,30,40,50};//declaration and initialization
6 System.out.println("One dimensional array elements are :\n");
7 for(int i=0;i<a.length;i++)
8 {
9 System.out.println("a["+i+"]:"+a[i]);
10 }
11 }
12 }

Output:

1 One dimensional array elements are : 


2
https://javatutoring.com/java-one-dimensional-array/ 4/7
7/29/2019 One Dimensional Array In Java - Tutorial & Example

3 a[0]:10
4 a[1]:20
5 a[2]:30
6 a[3]:40
7 a[4]:50

Using String

1. 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 class OneDimensionString
2 {
3 public static void main(String[] args)
4 {
5 //declare and initialize one dimension array
6 String[] str = new String[]{"one", "two", "three", "four"};
7 System.out.println("These are elements of one Dimensional array.");
8 for (int i = 0; i < str.length; i++)
9 {
10 System.err.println(str[i] + " ");
11 }
12 }
13 }

Output:

1 These are elements of one Dimensional array.


2 one
3 two
4 three
5 four

Tweet Like 0 Share


Previous: Next:

https://javatutoring.com/java-one-dimensional-array/ 5/7

Вам также может понравиться