Java Arrays Tutorial Part2

 

Java arrays Notes  Part-2

coding-zon-double-array-java-part2
👈PART-1

Table of Contents

  1. Double Dimensional Array
    1. Double Dimensional Array structure
    2. Declaration of Double Dimensional Array
    3. Initialization of Double Dimensional Array
      1.     Direct initialization
      2.     Creating array using new
    4. Printing   Dimensional Array
    5. char Array
    6. String Array

 
 

 Double Dimensional Array

 
 

 In a double dimensional array, values are stored in the form of rows and columns.

2D Array structure

 

 

is stored at 0, (first row and first col)

is stored at 0, 1  (first row and second col)  

is stored at 1, 0  (second row and first col)  

4  is stored at 1, 1  (second row and second col)

 

Declaration of Double Dimensional Array

syn:

datatype arrayname [rows][cols];

ex:  
  int a[][];  

 

array can be declared as shown below.

     
int [][]a;
int []a[];
 

 

Initialization of Double Dimensional Array

 
2 types
 
type1:
 
    int a[][] = {{2,3},{4,5}}; //direct initialization
 
 
 
type2: 
 

Declaring double dimensional array using new operator and using index position storing array values.

 
 
int a[][] = new int[2][2]; 
 
 a[0][0] = 1;
 a[0][1] = 2
 a[1][0] = 3;
 a[1][1] = 4
 
 

if size is not specified, it can any size.

 

 GotoMenu

Printing  ddarray


a[][] = {{1,2},{3,4}};
 
System.out.print(a[0][0]);
System.out.print(a[0][1]);
System.out.print("\n");
System.out.print(a[1][0]);
System.out.print(a[1][1]); 
 

program

class DDarray
{
public static void main(String args[]){

int a[][] = {{1,2},{3,4}}; 
 
System.out.print(a[0][0]);
System.out.print(a[0][1]);
System.out.print("\n");
System.out.print(a[1][0]);
System.out.print(a[1][1]); 
 
}
}
o/p:
---
1 2
3 4

 

Initialization of Double Dimensional char Array

char names[4][10] = {{'a','a','a','a'},
                     {'b','b','b'},
                     {'c','c','c','c'},
                     {'d','d','d','d'}
                    };
 
 
//4 rows, any no. of columns less than 10.
program:

public class DDarray
{
public static void main(String args[]){
char names[][] =  {{'a'},
                   {'b','b'},
                   {'c','c','c'},
                   {'d','d','d','d'}
                    };
                        
for(int i = 0;i<4;i++)
{
System.out.println(names[i]);
}
System.out.println("");
}
}



Declaration of String in Double  Dimension  Array

ex1

String names [3][20] = {{"india"},{"Briton"},{"America"}}; 
 
names are 3, and length of the name can be max. of 20.
 

ex2


String names[][] = {{"india","Rupee"},{"France","Euro"},
                    {"Us","dollar"},{"japan","yen"}};
 
 
 
public class DDarray1
{
public static void main(String args[]){ 

 

String names[][] = {{"India","Rupee"},
                    {"France","Euro"},
                    {"USA","Dollar"},
                    {"Japan","Yen"}};
 

for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
System.out.print(names[i][j]+"\t"+names[i][j+1]);
break;
}
System.out.println("");
}
}
}

 o/p:

India    Rupee
France    Euro
USA    Dollar
Japan    Yen

 Practice:

Print cities and capital accept city print capital.
Print names contact numbers in dd array
Create items and  prices array calculate total print all items, prices, totalbill
Take marks and subjects arrays, find hightest marks in whcih subject

 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP