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

One Dimensional Array in Java One Dimensional Array – Using

The methods used in this article are as follows:


• Using Standard Method
Standard Method
1 class OnedimensionalStandard
• Using Scanner 2 {
• Using String 3 public static void main(String args[])
4 {
5 int[] a=new int[3];//declaration
An array is a collection of elements of one specific type in a 6 a[0]=10;//initialization
horizontal fashion. The array in contention here is that of the one- 7 a[1]=20;
dimensional array in Java programming. 8 a[2]=30;
Anything having one-dimension means that there is only one 9 //printing array
10 System.out.println("One dimensional array elements are");
parameter to deal with. In regular terms, it is the length of 11 System.out.println(a[0]);
something. Similarly, as far as an array is concerned, one 12 System.out.println(a[1]);
dimension means it has only one value per location or index. 13 System.out.println(a[2]);
14 }
One-dimensional array in Java programming is an array with a 15 }
bunch of values having been declared with a single index.
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
As you can see in the example given above, firstly, you need to len and declare an array int[len].
declare the elements that you want to be in the specified array. 2) To store elements in to the array for i=0 to i<length of an array
Secondly, the location of each element needs to particularized as read the element using sc.nextInt() and store the element at the
well, since that is where the elements will be stored respectively. index a[i].
• Thirdly, you need to declare the array accordingly. 3) Display the elements of an array for loop iterates from i=0 to
As it is visible, the three elements that have been listed i<length of an array print the array element a[i].
simultaneously are as follows: 1 import java.util.*;
2 class OnedimensionalScanner
• 10 3 {
• 20 4 public static void main(String args[])
• 30 5 {
6 int len;
Hence, the result is printed as 10 20 30 respectively in single 7 Scanner sc=new Scanner(System.in);
lines, since it is a one-dimensional array. 8 System.out.println("Enter Array length : ");
9 len=sc.nextInt();
Thus, the methods used to do so in Java are as follows: 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(); Using String
15 } 1. We declared one-dimensional string array with the elements
16 System.out.print("Elements in Array are :\n");
17 for(int i=0; i<len; i++) strings.
18 { 2) To print strings from the string array. for i=0 to i<length of the
19 System.out.print(a[i] + " ");
20 } string print string which is at the index str[i].
21 } One Dimensional Array Java Program Using String
22 }
1 class OneDimensionString
Output: 2 {
1 Enter Array length : 3 public static void main(String[] args)
2 4 4 {
3 Enter 4 Element to Store in Array : 5 //declare and initialize one dimension array
4 1 6 String[] str = new String[]{"one", "two", "three", "four"};
5 2 7 System.out.println("These are elements of one Dimensional array.");
6 3 8 for (int i = 0; i < str.length; i++)
7 4 9 {
8 Elements in Array are : 10 System.err.println(str[i] + " ");
9 1 2 3 4 11 }
12 }
13 }
Output:
1 These are elements of one Dimensional array.
2 one
Using For Loop – One Dimensional Array 3 two
4 three
1 class OnedimensionalLoop 5 four
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:
One dimensional array elements are :
1
2 a[0]:10
3 a[1]:20
4 a[2]:30
5 a[3]:40
6 a[4]:50
7
Two-Dimensional Array in Java Two Dimensional – Using Standard Method
1 class TwodimensionalStandard
2 {
A two-dimensional entity, in general, is something that has two 3 public static void main(String args[])
specific parameters. Those two parameters are usually length and 4 {
breadth since it is a physical quantity. Similarly, a two- 5 int[][] a={{10,20},{30,40}};//declaration and initialization
dimensional array is an array which technically has one row of 6 System.out.println("Two dimensional array elements are");
7 System.out.println(a[0][0]);
elements; however, each row has a bunch of elements defined by 8 System.out.println(a[0][1]);
itself. Basically, you need to define both the rows and columns 9 System.out.println(a[1][0]);
and then go ahead with declaring the elements in the respective 10 System.out.println(a[1][1]);
11 }
locations or indexes. 12 }
Output:
1 Two dimensional array elements are
2 10
3 20
4 30
5 40
Using For Loop
1. In two dimensional array represent as rows and columns.
2) To print the two-dimensional array, for loop iterates from o to
i<3 for loop iterates from j=0 to j<2 print the element which is at
the index a[i][j].
As you can see in the example given above, firstly, you need to 1 class TwodimensionalLoop
specify the number of rows that you are assigning with the array. 2 {
3 public static void main(String args[])
• In this case, it is 2.
4 {
• Next, you need to mention the number of columns that you 5 int[][] a={{10,20},{30,40},{50,60}};//declaration and initialization
want to assign with the array. 6 System.out.println("Two dimensional array elements are");
7 for (int i = 0; i < 3; i++)
• Here, it is 3. 8 {
9 for (int j = 0; j < 2; j++)
• Thus, there will be a total of 6 elements that can go into a 2×3 10 {
Matrix. 11 System.out.println(a[i][j]);
12 }
The elements entered as per the example are as follows: 13 }
1 14 }
2 15 }
3 Output:
4 1 Two dimensional array elements are
5 2 10
6 3 20
Hence, the elements are arranged accordingly and you will get 4 30
5 40
your two-dimensional array. 6 50
7 60 7 2
8 3
9 4
Using Scanner 10 5
11 6
1. Read the row length, column length of an array using 12 Elements in Array are :
sc.nextInt() method of Scanner class. 13 Row [0]: Column [0] :1
14 Row [0]: Column [1] :2
2) Declare the array with the dimension row, column. 15 Row [0]: Column [2] :3
3) for i=0 to i<row for j=0 to j<column sc.nextInt() reads the 16 Row [1]: Column [0] :4
entered number and insert the element at a[i][j]. 17 Row [1]: Column [1] :5
import java.util.*; 18 Row [1]: Column [2] :6
1
2
class TwoDimensionalScanner Two Dimensional – Using String
{ 1. To print the elements of two-dimensional string array for i=0
3
public static void main(String args[])
4
{ to i<3 for j=0 to j<2 prints the string element which is at the
5
6
index str[i][j].
Scanner sc=new Scanner(System.in);
7
System.out.println("Enter Row length of an array : "); 2) Here i indicates row number and j indicates column number
8
9
int row=sc.nextInt(); Dimensional Array Java Using String
System.out.println("Enter column length of an array : "); Java
10
int column=sc.nextInt();
11
int a[][]=new int[row][column];//declaration 1 class TwoDimensionalString
12
System.out.print("Enter " + row*column + " Elements to Store in Array 2 {
13
:\n"); 3 public static void main(String[] args)
14
for (int i = 0; i < row; i++) 4 {
15
{ 5 String[][] str = new String[][]{{"one", "two"}, {"three", "four"},{"five","six"}};
16
for(int j = 0; j < column; j++) 6 System.out.println("Two dimensional string array elements are :\n");
17
{ 7 for (int i = 0; i < 3; i++)
18
a[i][j] = sc.nextInt(); 8 {
19
} 9 for (int j = 0; j < 2; j++)
20
} 10 {
21
System.out.print("Elements in Array are :\n"); 11 System.out.println("str["+i+"]["+j+"]:"+str[i][j]);
22
for (int i = 0; i < row; i++) 12 }
23
{ 13 }
24
for(int j = 0; j < column; j++) 14 }
25
{ 15 }
26
System.out.println("Row ["+i+"]: Column ["+j+"] :"+a[i][j]);
27 Output:
}
28
} 1 Two dimensional string array elements are :
29
} 2
30
} 3 str[0][0]:one
Output: 4 str[0][1]:two
5 str[1][0]:three
1 Enter Row length of an array : 6 str[1][1]:four
2 2 7 str[2][0]:five
3 Enter column length of an array : 8 str[2][1]:six
4 3
5 Enter 6 Elements to Store in Array :
6 1

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