二维数组计数和翻转java

时间:2015-04-20 12:18:26

标签: java arrays

如何读取文件的输入,将其转换为二维数组,并将其水平翻转?

这是我真正拥有的,因为我不知道如何开始:

import java.util.Scanner;
import java.io.*:
import java.util.*:

public class project1 {

   public static void main(String[] args) {

       String[][] myString = new String [row][column];

1 个答案:

答案 0 :(得分:0)

使用此函数读取矩阵:

 public static int[][] create2DIntMatrixFromFile(String filename) throws Exception {
        int[][] matrix = null;

        // If included in an Eclipse project.
        InputStream stream = ClassLoader.getSystemResourceAsStream(filename);
        BufferedReader buffer = new BufferedReader(new InputStreamReader(stream));

        // If in the same directory - Probably in your case...
        // Just comment out the 2 lines above this and uncomment the line
        // that follows.
        //BufferedReader buffer = new BufferedReader(new FileReader(filename));

        String line;
        int row = 0;
        int size = 0;

        while ((line = buffer.readLine()) != null) {
            String[] vals = line.trim().split("\\s+");

            // Lazy instantiation.
            if (matrix == null) {
                size = vals.length;
                matrix = new int[size][size];
            }

            for (int col = 0; col < size; col++) {
                matrix[row][col] = Integer.parseInt(vals[col]);
            }

            row++;
        }

        return matrix;
    }     

这个转置它

 public static double[][] transposeMatrix(double [][] m){
        double[][] temp = new double[m[0].length][m.length];
        for (int i = 0; i < m.length; i++)
            for (int j = 0; j < m[0].length; j++)
                temp[j][i] = m[i][j];
        return temp;
    }

的引用:

Read .txt file into 2D Array

transpose double[][] matrix with a java function?