当前位置: 代码迷 >> J2SE >> 关于如何样从c的思维转到java面向对象的思维下,高手有什么可以给些建议吗
  详细解决方案

关于如何样从c的思维转到java面向对象的思维下,高手有什么可以给些建议吗

热度:1095   发布时间:2013-02-25 00:00:00.0
关于怎么样从c的思维转到java面向对象的思维上,高手有什么可以给些建议吗?
学过一段时间的java 可就是没有 面向对象的思维
代码为证 求帮助啊!!!
从控制台输入一个M*N矩阵,输出行列互换后的矩阵
Java code
import java.util.Scanner;public class juzhen {    public static void main(String[] args){        int M,N;        System.out.println("input M,N:");        Scanner reader=new Scanner(System.in);        M=reader.nextInt();        N=reader.nextInt();        String[][] arrayin = new String[M][N];        String[][] arrayout = new String[N][M];        for(int i=0;i<M;i++){            for(int j=0;j<N;j++){                //Scanner rd=new Scanner(System.in);                arrayin[i][j]=reader.next();            }        }                for(int i=0;i<N;i++){            for(int j=0;j<M;j++){                arrayout[i][j]=arrayin[j][i];                System.out.print(arrayout[i][j]+" ");            }            System.out.println("");        }    }}


------解决方案--------------------------------------------------------
Java code
public final class Matrix {    private int width;    private int height;    private Object[][] arrays;    private Object[][] reversedArray;    /**     *      * @param _width     * @param _height     * @param _arrays     */    public Matrix(int _width, int _height, Object[][] _arrays) {        this.width = _width;        this.height = _height;        arrays = new Object[width][height];        System.arraycopy(_arrays, 0, arrays, 0, _arrays.length);    }    /**     * reverse the Matrix     *      * @return     */    public final Object[][] reverse() {        reversedArray = new Object[height][width];        for (int x = 0; x < arrays.length; x++) {            Object[] items = arrays[x];            for (int y = 0; y < items.length; y++) {                reversedArray[y][x] = items[y];            }        }        return reversedArray;    }    /**     * print information     */    public final void print() {        for (Object[] items : reversedArray) {            for (Object item : items) {                System.out.print(item.toString() + " ");            }            System.out.println();        }    }    /**     * entrance     *      * @param args     */    public static void main(String[] args) {        String[][] arrays = new String[][] { { "1-1", "1-2", "1-3", "1-4" },                { "2-1", "2-2", "2-3", "2-4" }, { "3-1", "3-2", "3-3", "3-4" } };        Matrix matrix = new Matrix(arrays.length, arrays[0].length, arrays);        matrix.reverse();        matrix.print();    }}
  相关解决方案