Transpose of Matrix
public class Transpose_Matrix
{
/**
* @param args
*/
public static void main(String[] args)
{
String sRes="";
int aArray[][]=new int [3][3];
aArray[0][0]=0;
aArray[0][1]=0;
aArray[0][2]=0;
aArray[1][0]=1;
aArray[1][1]=1;
aArray[1][2]=1;
aArray[2][0]=2;
aArray[2][1]=2;
aArray[2][2]=2;
for (int i=0 ; i<=aArray.length-1 ;i++)
{
for (int j=0;j<=aArray[0].length-1;j++)
{
sRes=sRes+" "+aArray[i][j];
}
sRes=sRes+"\n";
}
System.out.println(sRes);
sRes="";
//-----------------------------------------Method 1
for (int j=0;j<=aArray[0].length-1;j++)
{
for (int i=0 ; i<=aArray.length-1 ;i++)
{
sRes=sRes+" "+aArray[i][j];
}
sRes=sRes+"\n";
}
System.out.println(sRes);
sRes="";
//-----------------------------------------Method 2
for (int i=0 ; i<=aArray.length-1 ;i++)
{
for (int j=0;j<=aArray[0].length-1;j++)
{
sRes=sRes+" "+aArray[j][i];
}
sRes=sRes+"\n";
}
System.out.println(sRes);
}
}
No comments:
Post a Comment