Possible Array Declarations and 2-D Arrays.

In this article,Possible Array Declarations and 2-D Arrays are explained in detail with examples.
In java we can declare an one dimensional arrays in possible ways.
int a[];
int []a;
int...a; //used only as varargs in methods
          // as last parameter.
Command line arguments
The main method takes string arrays as the input parameter.Such parameters could be passed as arguments while executing the Java program.
Syntax
java(class name)args...(separate with spaces)
Here, The size of arguments passed to the main method are dynamic i.e, We can pass zero or more arguments.Hence, main method parameter is var args. (From topic Possible Array Declarations and 2-D Arrays)
Ex
//program related to Possible Array Declarations and 2-D Arrays--command line arguments
class CommandLineArgs1
{
public static void main(String...args)
{
for(int i=0;i<args.length;i++) //using for loop
{
System.out.println("value is: "+args[i]);
}
for(String s:args) //args is command line arguments ments using for each loop.
{
System.out.println("Value is: "+s);
}
}
}
Note
1.For main method, command line arguments is varargs(0 or more parameters). This situation  works for all possible array declarations.
public static void main(String args[])
public static void main(String[] args)
public static void main(String ...args)
public static void main(String...a)              (From topic Possible Array Declarations and 2-D Arrays)
All these types as varargs.
2.The above situation does not work with normal methods.
m1(String s,int...p) // this is the correct format.
m1(String s,int []p) // wrong format.
m1(String s,int p[]) // wrong format.

Two-Dimensional Arrays.
It is an array in which elements are assumed to be stored in the form of rows and columns.
Syntax
(datatype) array-name[][];               (From topic Possible Array Declarations and 2-D Arrays)
Ex
int a[][];
After declarations , we allocate the size using new operator
Syntax
array-name=new datatype[row size][column size];
Ex a=new int[3][3];

Array in matrix form
To access elements of 2-D array we use row and column indexes.
  • Row indexes range from 0 to(row size-1)
  • column indexes range from o to(column size-1)
the combination of row and a column index, we get a particular cell value.
Ex  a[0][0],a[1][1],a[2][2] etc..,
Note
The best way to access cell elements of 2-D array is through nested loops.
Syntax
Outer-loop: //represents no of rows.
{
inner-loop: // represents no. of columns.
{
------
}
}
Ex
//program related to Possible Array Declarations and 2-D Arrays--2D Array
class ArrayTwoD
{
public static void main(String args[])
{
int i,j;
int a[][]; //2D array declaration.
a=new int[2][2]; //2D array creation.
a[0][0]=11;
a[0][1]=22;
a[1][0]=33;
a[1][1]=44;
System.out.println(a[0][0]+"\t"+a[0][1]);
System.out.println(a[1][0]+"\t"+a[1][1]);
System.out.println();
for(i=0;i<2;i++) //rows index.
{
for(j=0;j<2;j++) //column index.
{
System.out.println(a[i][j]+"\t");
}
System.out.println("\n");
}
}
}
Output
ArrayTwoD-Javaform
                         To get the code file use me.

Two-Dimensional(2-D) Array Initialization.
During declaration of a 2D-array, if it is initialized with the mathematical set of values then it is known as array initialization.
int a[][]={ {11,22},{33,44}}; (From topic Possible Array Declarations and 2-D Arrays)
Here, the size of 2D-array is 2 rows, 2 columns based on no.of sets initialized.
Ex
//Program related to Possible Array Declarations and 2-D Arrays--2D array initialization
class ArrayTwoD1
{
public static void main(String args[])
{
int i,j;
int a[][]={{15,26},{35,46}};
System.out.println(a[0][0]+"\t"+a[0][1]);
System.out.println(a[1][0]+"\t"+a[1][1]);
System.out.println();
for(i=0;i<2;i++) //rows index
{
for(j=0;j<2;j++) //column index
{
System.out.println(a[i][j]+"\t");
}
System.out.println("\n");
}
}
}
Output
ArrayTwoD1-Output-Javaform
                           To get the code file use me.


Begin your career in Digital Marketing,What is digital marketing? Digital Marketing online course. It's an current evolving technology which can give support to establish your own startup through Digital Marketing.

Do check my new startup Surprise Planners in HyderabadLavish Surprises our services are surprise party planners in Hyderabadsurprise giftssurprise birthday party planners Hyderabad, Wedding anniversary surprises, surprise planners Hyderabad.

Hi Friends, Please comment down your views in the comment section below. Thank you...

No comments:

Connect with Us by Subscribing here for more Updates..!

* indicates required