【问题标题】:Storing integer characters from a read file into a 2-Dimensional array将读取文件中的整数字符存储到二维数组中
【发布时间】:2014-03-12 22:57:54
【问题描述】:

我正在尝试编写一个程序来读取一个数组文件(以行作为第一个字符,列作为下一个字符,然后是一个 RxC 术语框)并尝试确定是否有五个水平、垂直或对角线相邻的字符相同,颜色不同(在我的 GUI 主程序中)

代码非常慢,只适用于较小的数组?我不明白我做错了什么。

文件如下所示:

 5 4
 1 2 3 4 5
 1 2 3 4 5
 7 3 2 0 1
 6 1 2 3 5   

代码:

public class fiveinarow
{
  int[][] Matrix = new int [100][100];
  byte[][] Tag = new byte [100][100];
  int row, col;
  String filepath, filename;

  public fiveinarow()
  {
    row = 0;
    col = 0;
    filepath = null;
    filename = null;
  }

  public void readfile()
  {
    JFileChooser chooser = new JFileChooser();
    chooser.setDialogType(JFileChooser.OPEN_DIALOG );
    chooser.setDialogTitle("Open Data File");

    int returnVal = chooser.showOpenDialog(null);
    if( returnVal == JFileChooser.APPROVE_OPTION) 
    {
      filepath = chooser.getSelectedFile().getPath();
      filename = chooser.getSelectedFile().getName();
    }

    try 
    {
            Scanner inputStream  = new Scanner(new FileReader(filepath));
        int intLine;
    row = scan.nextInt();
    col = scan.nextInt();
    for (int i=0; i < row; i++)
            {
        for (int j = 0 ; j < col; j++)
        {
                        int[][]Matrix = new int[row][col];
            Matrix[i][j] = inputStream.nextInt();
                    }
    }
}


    catch(IOException ioe)
    {
      System.exit(0);
    }
  }

当我计算一个 7x7 时,我得到了打开的确认,并且处理给出了一个所有零的数组 (7x7)。 当我计算 15x14 时,我得到“线程中的异常”AWT-EventQueue-0 错误并且处理时没有数组。

【问题讨论】:

  • 您似乎混合了行和列(如果您的示例数据是正确的数据)。您的示例有四行五列

标签: java arrays


【解决方案1】:

一些建议:

  1. 创建一个返回 int[][] 的方法以读取您的输入文件
  2. 读取行列后,创建矩阵 int[][] result = new int[row][column];
  3. 在循环中,将每个整数读入矩阵 (result[i][j] = scan.nextInt();

扫描完一行上的所有数字后不要忘记移动到下一行'

你可能会使用类似的东西:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import javax.swing.JFileChooser;

public class ReadMatrix {
static ReadMatrix mReadMatrix;
int[][] matrix;
int row, col;
String filepath, filename;

/**
 * @param args
 */
public static void main(String[] args) {
    mReadMatrix = new ReadMatrix();
    mReadMatrix.readfile();
}

// int[][] Matrix = new int [100][100];
// byte[][] Tag = new byte [100][100];


public void readfile() {
    JFileChooser chooser = new JFileChooser();
    chooser.setDialogType(JFileChooser.OPEN_DIALOG);
    chooser.setDialogTitle("Open Data File");

    int returnVal = chooser.showOpenDialog(null);
    if (returnVal == JFileChooser.APPROVE_OPTION) {
        filepath = chooser.getSelectedFile().getPath();
        filename = chooser.getSelectedFile().getName();
    }

    Scanner inputStream;
    try {
        inputStream = new Scanner(new FileReader(filepath));
        row = inputStream.nextInt();
        col = inputStream.nextInt();
        System.out.println(" matrix is " + row + " rows and " + col + " columns");
        matrix = new int[row][col];

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                matrix[i][j] = inputStream.nextInt();
                System.out.println(" " + i + "," + j + ": " + matrix[i][j]);
            }
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

【讨论】:

  • 我将 'try' 修改为 int[][]Matrix = new int[row][col]; Matrix[i][j] = inputStream.nextInt(); 但它给了我一个全 ​​0 的数组并且不适用于更大的文件大小?
  • 你能给我一个使用代码的例子吗?我不认为我完全理解。
  • 代码输出到系统,当我需要它在 gui 中并传递给重绘数组的驱动程序时。
【解决方案2】:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多