1 and 2 Dimensional Array in Java

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

One Dimensional Array in Java One Dimensional Array – Using

The methods used in this article are as follows: Standard Method


 Using Standard Method 1 class OnedimensionalStandard
 Using Scanner 2 {
3 public static void main(String args[])
 Using String 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  
7 a[1]=20;  
horizontal fashion. The array in contention here is that of the 8 a[2]=30;  
one-dimensional array in Java programming. 9 //printing array  
Anything having one-dimension means that there is only one 10 System.out.println("One dimensional array elements are");    
11 System.out.println(a[0]);    
parameter to deal with. In regular terms, it is the length of 12 System.out.println(a[1]);    
something. Similarly, as far as an array is concerned, one 13 System.out.println(a[2]);    
dimension means it has only one value per location or index. 14 }
15 }
One-dimensional array in Java programming is an array with a
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 len and declare an array int[len].
As you can see in the example given above, firstly, you need to 2) To store elements in to the array for i=0 to i<length of an
declare the elements that you want to be in the specified array. array read the element using sc.nextInt() and store the element
Secondly, the location of each element needs to particularized as at the index a[i].
well, since that is where the elements will be stored respectively. 3) Display the elements of an array for loop iterates from i=0 to
 Thirdly, you need to declare the array accordingly. i<length of an array print the array element a[i].
1 import java.util.*;
As it is visible, the three elements that have been listed 2 class OnedimensionalScanner
simultaneously are as follows: 3 {
4    public static void main(String args[])
 10 5    {    
 20 6 int len;
 30 7 Scanner sc=new Scanner(System.in);
8 System.out.println("Enter Array length : ");
Hence, the result is printed as 10 20 30 respectively in single 9 len=sc.nextInt();
lines, since it is a one-dimensional array. 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++)
Thus, the methods used to do so in Java are as follows: 13         {
14            a[i] = sc.nextInt();
15         }   1. We declared one-dimensional string array with the
16         System.out.print("Elements in Array are :\n");
17         for(int i=0; i<len; i++)
elements strings.
18         { 2) To print strings from the string array. for i=0 to i<length of
19            System.out.print(a[i] + "  ");
20         }  
the 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
a[0]:10
2
a[1]:20
3
a[2]:30
4
a[3]:40
5
a[4]:50
6
7

Using String Two-Dimensional Array in Java


A two-dimensional entity, in general, is something that has two 3 public static void main(String args[])
4 {    
specific parameters. Those two parameters are usually length 5 int[][] a={{10,20},{30,40}};//declaration and initialization  
and breadth since it is a physical quantity. Similarly, a two- 6 System.out.println("Two dimensional array elements are");    
dimensional array is an array which technically has one row of 7 System.out.println(a[0][0]);    
8 System.out.println(a[0][1]);    
elements; however, each row has a bunch of elements defined by 9 System.out.println(a[1][0]);    
itself. Basically, you need to define both the rows and columns 10 System.out.println(a[1][1]);    
and then go ahead with declaring the elements in the respective 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].
1 class TwodimensionalLoop
2 {
As you can see in the example given above, firstly, you need to 3 public static void main(String args[])
specify the number of rows that you are assigning with the array. 4 {    
 In this case, it is 2. 5 int[][] a={{10,20},{30,40},{50,60}};//declaration and initialization  
6 System.out.println("Two dimensional array elements are");    
 Next, you need to mention the number of columns that 7 for (int i = 0; i < 3; i++)
you want to assign with the array. 8 {
9             for (int j = 0; j < 2; j++)
 Here, it is 3. 10     {
11                 System.out.println(a[i][j]);
 Thus, there will be a total of 6 elements that can go into a 12             }
2×3 Matrix. 13 }
14 }
The elements entered as per the example are as follows: 15 }
1 Output:
2 1 Two dimensional array elements are
3 2 10
4 3 20
4 30
5 5 40
6 6 50
Hence, the elements are arranged accordingly and you will get 7 60
your two-dimensional array.
Two Dimensional – Using Standard Method Using Scanner
1 class TwodimensionalStandard
2 {
1. Read the row length, column length of an array using 14 Row [0]:  Column [1] :2
15 Row [0]:  Column [2] :3
sc.nextInt() method of Scanner class. 16 Row [1]:  Column [0] :4
2) Declare the array with the dimension row, column. 17 Row [1]:  Column [1] :5
18 Row [1]:  Column [2] :6
3) for i=0 to i<row for j=0 to j<column sc.nextInt() reads the
entered number and insert the element at a[i][j]. Two Dimensional – Using String
1 import java.util.*; 1. To print the elements of two-dimensional string array for
2 class TwoDimensionalScanner i=0 to i<3 for j=0 to j<2 prints the string element which is at
3 { the index str[i][j].
4    public static void main(String args[])
5    {     2) Here i indicates row number and j indicates column number
6 Dimensional Array Java Using String
7 Scanner sc=new Scanner(System.in);
8
Java
System.out.println("Enter Row length of an array : ");
9 int row=sc.nextInt(); 1 class TwoDimensionalString
10 System.out.println("Enter column length of an array : "); 2 {
11 int column=sc.nextInt(); 3     public static void main(String[] args)
12 int a[][]=new int[row][column];//declaration     4     {
13 System.out.print("Enter " + row*column + " Elements to Store in Array :\n"); 5         String[][] str = new String[][]{{"one", "two"}, {"three", "four"},{"five","six"}};
14         for (int i = 0; i < row; i++) 6         System.out.println("Two dimensional string array elements are :\n");    
15 { 7 for (int i = 0; i < 3; i++)
16     for(int j = 0; j < column; j++) 8 {
17     { 9             for (int j = 0; j < 2; j++)
18            a[i][j] = sc.nextInt(); 10     {
19     } 11                 System.out.println("str["+i+"]["+j+"]:"+str[i][j]);
20 }   12             }
21         System.out.print("Elements in Array are :\n"); 13 }        
22         for (int i = 0; i < row; i++) 14    }
23 { 15 }
24     for(int j = 0; j < column; j++)
25     { Output:
26        System.out.println("Row ["+i+"]:  Column ["+j+"] :"+a[i][j]); 1 Two dimensional string array elements are :
27     } 2  
28 }   3 str[0][0]:one
29    } 4 str[0][1]:two
30 } 5 str[1][0]:three
6 str[1][1]:four
Output: 7 str[2][0]:five
1 Enter Row length of an array : 8 str[2][1]:six
2 2
3 Enter column length of an array :
4 3
5 Enter 6 Elements to Store in Array :
6 1
7 2
8 3
9 4
10 5
11 6
12 Elements in Array are :
13 Row [0]:  Column [0] :1

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy