Create an anonymous array in java?
Answers
Answered by
0
class AnonymousArray
{
static void print(int a[])
{
for(int i=0;i<a.length;++i)
System.out.print(a[i]+" ");
}
static void print(int a[][])
{
for(int i=0;i<a.length;++i)
{
for(int j=0;j<a[i].length;++j)
System.out.print(a[i][j]+" ");
System.out.println("");
}
}
public static void main(String...s)
{
//1d anonymous array
print(new int[]{10,20,30,40});
System.out.println("n");
//2d anonymous array
print(new int[][]{{10,20},{30,40},{50,60}});
}
}
{
static void print(int a[])
{
for(int i=0;i<a.length;++i)
System.out.print(a[i]+" ");
}
static void print(int a[][])
{
for(int i=0;i<a.length;++i)
{
for(int j=0;j<a[i].length;++j)
System.out.print(a[i][j]+" ");
System.out.println("");
}
}
public static void main(String...s)
{
//1d anonymous array
print(new int[]{10,20,30,40});
System.out.println("n");
//2d anonymous array
print(new int[][]{{10,20},{30,40},{50,60}});
}
}
Answered by
2
We can create an Anonymous Array in Java by the following syntax,
new data_type[ ] {elementOne, elementTwo, elementThree, ...}
Similar questions