简体   繁体   English

Java读取文件并写入数组

[英]Java read file and write into array

--------------- SOLVED !!! - - - - - - - - 解决了 !!! ----------------------- I would like to thank you, everyone, for your help ! -----------------------我要感谢你们大家的帮助!

I have to read a file where I have nxm elements, and then put those elements into 2D array, and then print it out. 我必须读取一个包含nxm元素的文件,然后将这些元素放入2D数组中,然后将其打印出来。 I'm a little bit stuck at printing my array out. 我有点无法打印阵列。 In file.txt I have 2x2 => 2 lines and 2 columns, and elements are:1 2 3 4. 在file.txt中,我有2x2 => 2行和2列,并且元素是:1 2 3 4。

Here is my code: 这是我的代码:

public class ex_1 
{
    public static void main(String args[])
    {   
        FileReader fr = new FileReader("FirstMatrix.txt");
        BufferedReader br = new BufferedReader(fr);
        String s = br.readLine();
        String[] split = s.split("x");
         int k=Integer.parseInt(split[0]);
         int l=Integer.parseInt(split[1]);
        System.out.println("Matrix dimensions: "+k+" lines,  "+l+" columns si "+k*l+" elements");

        System.out.print("Elements in matrix are: \n"); 

        int[][] FirstMatrix = new int [k][l];
                while ((s = br.readLine()) != null) 
                {       
                    for(int i=0; i<FirstMatrix.length; i++)
                        for(int j=0; j<FirstMatrix[i].length;j++)
                        {
                            FirstMatrix[i][j] = Integer.parseInt(s);
                            System.out.println("FirstMatrix["+i+"]["+j+"]="+FirstMatrix[i][j]);
                        }   
                }
                br.close();

My output, how it is: 我的输出,如何:

FirstMatrix[0][0]=1   
FirstMatrix[0][1]=1    
FirstMatrix[1][0]=1    
FirstMatrix[1][1]=1    
FirstMatrix[0][0]=2    
FirstMatrix[0][1]=2    
FirstMatrix[1][0]=2    
FirstMatrix[1][1]=2    
FirstMatrix[0][0]=3    
FirstMatrix[0][1]=3    
FirstMatrix[1][0]=3    
FirstMatrix[1][1]=3    
FirstMatrix[0][0]=4    
FirstMatrix[0][1]=4    
FirstMatrix[1][0]=4    
FirstMatrix[1][1]=4       

How I want it to be: 我希望它是:

FirstMatrix[0][0]=1   
FirstMatrix[0][1]=2    
FirstMatrix[1][0]=3    
FirstMatrix[1][1]=4           

Does anyone know how I can fix this print out please? 有谁知道我该如何解决此打印问题?

EDIT!! 编辑!! If I change code like this 如果我这样更改代码

    int[][] FirstMatrix = new int [k][l];

                while ((s = br.readLine()) != null) 
                {       
                    for(int i=0; i<k;i++)
                        for(int j=0; j<l; j++)
                        {
                        FirstMatrix[i][j] = Integer.parseInt(s);
                        }
                }
                br.close();

                for(int i=0; i<FirstMatrix.length; i++)
                    for(int j=0; j<FirstMatrix[i].length;j++)
                    {
                        System.out.println("FirstMatrix["+i+"]["+j+"]="+FirstMatrix[i][j]);
                    }

I get this output: 我得到以下输出:

FirstMatrix[0][0]=4
FirstMatrix[0][1]=4
FirstMatrix[1][0]=4
FirstMatrix[1][1]=4

You're doing n*n times the loop. 您正在执行n * n次循环。 Because everytime that you read the file are entering the loop. 因为每次读取文件都进入循环。 If you know that every line is a number, you could read only one time and print each row. 如果您知道每一行都是数字,那么您只能阅读一次并打印每一行。

Try it and let me know what happen. 试试看,让我知道会发生什么。

Best regards from Mexico 来自墨西哥的问候

After this line System.out.print("Elements in matrix are: \\n"); 在此行之后, System.out.print("Elements in matrix are: \\n"); paste this code. 粘贴此代码。

int[][] FirstMatrix = new int [l][k];

        for(int j=0; j<l;j++)
        {  
            for(int i=0; i<k; i++) {
                s = br.readLine());
                FirstMatrix[j][i] = Integer.parseInt(s);
                System.out.println("FirstMatrix["+j+"]["+i+"]="+FirstMatrix[j][i]);
            }
        }

Don't use while loop. 不要使用while循环。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ex_1 {
    public static void main(String args[]) throws IOException {
        FileReader fr = new FileReader("FirstMatrix.txt");
        BufferedReader br = new BufferedReader(fr);
        String s = br.readLine();
        String[] split = s.split("x");
        int k = Integer.parseInt(split[0]);
        int l = Integer.parseInt(split[1]);
        System.out.println("Matrix dimensions: " + k + " lines,  " + l + " columns si " + k * l + " elements");

        System.out.print("Elements in matrix are: \n");

        int[][] FirstMatrix = new int[k][l];

        for (int lineIndex = 0; lineIndex < k; lineIndex++) {


                for (int columnIndex = 0; columnIndex < l; columnIndex++) {
                    s = br.readLine();
                    FirstMatrix[lineIndex][columnIndex] = Integer.valueOf(s);
                }

        }

        for (int a = 0; a < k; a++) {
            for (int b = 0; b < l; b++)
                System.out.print(FirstMatrix[a][b] + " ");
            System.out.println();
        }

        br.close();

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM