JavaSE系列代码05:返回值是数组类型的方法

The general form of array description is: type specifier array name [constant expression] ; where the type specifier is any basic or construction data type. The array name is a user-defined array identifier. A constant expression in square brackets represents the number of data elements, also known as the length of an array.

public class Javase_05
{
  public static void main(String[] args)
  {
    int[][] a={{1,2,3},{4,5,6},{7,8,9}};    //定义二维数组
    int[][] b=new int [3][3];
    trans pose=new trans();      //创建trans类的对象pose
    b=pose.transpose(a);        //用数组a调用方法,返回值赋给数组b
    for (int i=0;i<b.length;i++)   //输出数组的内容
    {
      for (int j=0;j<b[i].length;j++)
        System.out.print(b[i][j]+ "   " );
     System.out.print("\n");      //每输出数组的一行后换行
    }
  }
}
class trans
{
  int temp;
  int[][] transpose(int[][] array)       //返回值为二维整型数组
  {
    for (int i=0;i<array.length;i++)     //将矩阵转置
      for(int j=i+1;j<array[i].length;j++)
      {
        temp=array[i][j];
        array[i][j]=array[j][i];
        array[j][i]=temp;
      }
    return array;    //返回二维数组
  }
}
发布了13 篇原创文章 · 获赞 160 · 访问量 8544

猜你喜欢

转载自blog.csdn.net/blog_programb/article/details/105376715