0% found this document useful (0 votes)
103 views

One Dimensional Array in Java - Tutorial & Example

Java Notes

Uploaded by

Johnmark Padilla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

One Dimensional Array in Java - Tutorial & Example

Java Notes

Uploaded by

Johnmark Padilla
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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

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
BEGINNERS PROGRAMS : Left Arrow Star Patter
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.
HOME JAVA DATA TYPES JAVA VARIABLES CONTROL STATEMENTS JAVA ARGUMENTS Secondly, the location of each element needs to particularized as well, since that is where the elements will
be stored respectively.
C TUTORIALS
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

One Dimensional Array In Java – Tutorial & Example 30

 in Java Programs  Comments Offon One Dimensional Array In Java – Tutorial & Example
Hence, the result is printed as 10 20 30 respectively in single lines, since it is a one-dimensional array.
One Dimensional Array Program in Java – In this article, we will detail in on all the different methods
Thus, the methods used to do so in Java are as follows:
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. One Dimensional Array – Using Standard
The methods used in this article are as follows: Method
Using Standard Method 1 class OnedimensionalStandard
Using Scanner 2 {
Using String 3 public static void main(String args[])
4 {
An array is a collection of elements of one specific type in a horizontal fashion. The array in contention here 5 int[] a=new int[3];//declaration
is that of the one-dimensional array in Java programming. 6 a[0]=10;//initialization
Anything having one-dimension means that there is only one parameter to deal with. In regular terms, it is 7 a[1]=20;
the length of something. Similarly, as far as an array is concerned, one dimension means it has only one 8 a[2]=30;
 
value per location or index. 9 //printing array

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


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

10 System.out.println("One dimensional array elements are"); 16 System.out.print("Elements in Array are :\n");


11 System.out.println(a[0]); 17 for(int i=0; i<len; i++)
12 System.out.println(a[1]); 18 {
13 System.out.println(a[2]); 19 System.out.print(a[i] + " ");
14 } 20 }
15 } 21 }
22 }
Output:
Output:
1 One dimensional array elements are
2 10 1 Enter Array length :
3 20 2 4
4 30 3 Enter 4 Element to Store in Array :
4 1
5 2
Using Scanner
6 3
1. Read the array length as sc.nextInt() and store it in the variable len and declare an array int[len]. 7 4
8 Elements in Array are :
2) To store elements in to the array for i=0 to i<length of an array read the element using sc.nextInt() and 9 1 2 3 4
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 Using For Loop – One Dimensional Array
element a[i].
1 class OnedimensionalLoop
2 {
1 import java.util.*;
3 public static void main(String args[])
2 class OnedimensionalScanner
4 {
3 {
5 int a[]={10,20,30,40,50};//declaration and initialization
4 public static void main(String args[])
6 System.out.println("One dimensional array elements are :\n");
5 {
7 for(int i=0;i<a.length;i++)
6 int len;
8 {
7 Scanner sc=new Scanner(System.in);
9 System.out.println("a["+i+"]:"+a[i]);
8 System.out.println("Enter Array length : ");
10 }
9 len=sc.nextInt();
11 }
10 int a[]=new int[len];//declaration
12 }
11 System.out.print("Enter " + len + " Element to Store in Array :\n");
12 for(int i=0; i<len; i++) Output:
13 {
14 a[i] = sc.nextInt(); 1 One dimensional array elements are :
 
15 } 2
https://javatutoring.com/java-one-dimensional-array/ 3/7 https://javatutoring.com/java-one-dimensional-array/ 4/7
« »
7/29/2019 One Dimensional Array In Java - Tutorial & Example 7/29/2019 One Dimensional Array In Java - Tutorial & Example

3 a[0]:10 Two Dimensional Array In Java – Exception Handling In Java – Tutorial &
4 a[1]:20 JavaTutoring Examples
5 a[2]:30
6 a[3]:40 RELATED POSTS !
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]. Two Dimensional Array In Multi Dimensional Array In Java Program To Check Even
Java – JavaTutoring Java – Tutorial & Program Numbers | 4 Ways

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++)
Java Program To Calculate Java Program To Calculate Java Program To Calculate
9 { EMI – Monthly & Annum Exponent Value | 4 Ways Future Investment Value
10 System.err.println(str[i] + " ");
11 }
12 }
13 }

Output:

1 These are elements of one Dimensional array. RANDOM POSTS RANDOM POSTS
2 one
3 two C Program To Remove First Occurrence Of A Java Program To Calculate Volume Of Prism | 3
4 three Word From String | 4 Ways Simple ways
5 four
C Program To Compare Two Strings – 3 Easy Java Program To Calculate Exponent Value | 4
Ways | C Programs Ways
Tweet Like 0 Share

C Program To Search All Occurrences Of A Java Program To Calculate Power Of Number |


 Character In String | C Programs 4 Ways 
Previous: Next:

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


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

C Program To Count Frequency Of Each Two Dimensional Array In Java – JavaTutoring


Character In String | C Programs
Java Program To Find Area of Parallelogram –
C Program Area Of Trapezium – 3 Ways | C Programs
Programs

© 2019. Copyrighted Protected. Duplication or Copying Our Site Content Is Strictly Prohibited. However, Refer

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

You might also like