JAVA使用列信息在文件中写入矩阵。(矩阵变换)
我有一个存储矩阵的文件。此文件具有随机访问文件类型。此矩阵按列存储。我的意思是,在这个矩阵的第I行中存储了第I列(实矩阵)。有一个例子: 第i行:1 2 3 4(在文件中)。这意味着实矩阵有一个第i列:(1234)(被转置) 我需要以一种自然的方式(按行)将这个矩阵保存在一个新文件中,然后用FileReader打开并用TextArea显示JAVA使用列信息在文件中写入矩阵。(矩阵变换),java,Java,我有一个存储矩阵的文件。此文件具有随机访问文件类型。此矩阵按列存储。我的意思是,在这个矩阵的第I行中存储了第I列(实矩阵)。有一个例子: 第i行:1 2 3 4(在文件中)。这意味着实矩阵有一个第i列:(1234)(被转置) 我需要以一种自然的方式(按行)将这个矩阵保存在一个新文件中,然后用FileReader打开并用TextArea显示 你知道怎么做吗?如果是,请帮助=)好的,在理解了问题之后,我能想到的算法就是下一个 打开文件 从该文件中读取一行 如果您在第一排-> 3.1为正在读取的列创建
你知道怎么做吗?如果是,请帮助=)好的,在理解了问题之后,我能想到的算法就是下一个
N
文件,每个文件代表一列file = File.open(myFile)
columns = File[] // columns is a file array, each one will contain a column from the original file
for each line in file
numbersInThatLine : Integer[]
numbersInThatLine = parseArrayFrom( line ) // created an array of int's from the given line
writeArraryToFiles( array=numbersInThatLine, files=columns ) // write each number in a separate file
end
close( file )
output = File.new()
for each file in columns
output.write( file )
end
close( output )
List<Integer[]> matrix = new ArrayList<Integer[]>();
List<Integer> currentRow;
BufferedReader reader = new BufferedReader( yourFile );
String line = null;
while((line = reader.readLine()) != null ) {
Scanner scanner = new Scanner( line );
currentRow = new ArrayList<Integer>();
while( scanner.hasNextInt()){
currentRow.add( scanner.nextInt() );
}
matrix.add( convertListToArray( currentRow )); // See: http://stackoverflow.com/questions/960431/how-to-convert-listinteger-to-int-in-java
}
那么,如果您的文件
1 2 3 4
5 6 7 8
9 10 11 12
您将打开4个文件,在第一次通过时,您将获得
file0 = 1
file1 = 2
file2 = 3
file3 = 4
1 5 9 // from file0
2 6 10 // from file1
3 7 11 // from file2
4 8 12 // from file3
在第二遍中,您将有:
file0 = 1 5
file1 = 2 6
file2 = 3 7
file3 = 4 8
最后:
file0 = 1 5 9
file1 = 2 6 10
file2 = 3 7 11
file3 = 4 8 12
最后,将每个文件写入输出文件
file0 = 1
file1 = 2
file2 = 3
file3 = 4
1 5 9 // from file0
2 6 10 // from file1
3 7 11 // from file2
4 8 12 // from file3
这就是(如果我这次理解正确的话)你需要的
祝你好运
因此,该文件包含:
1 2 3 4
5 6 7 8
9 10 11 12
将代表矩阵:
[[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]]
?
请执行以下操作:
file = File.open(myFile)
columns = File[] // columns is a file array, each one will contain a column from the original file
for each line in file
numbersInThatLine : Integer[]
numbersInThatLine = parseArrayFrom( line ) // created an array of int's from the given line
writeArraryToFiles( array=numbersInThatLine, files=columns ) // write each number in a separate file
end
close( file )
output = File.new()
for each file in columns
output.write( file )
end
close( output )
List<Integer[]> matrix = new ArrayList<Integer[]>();
List<Integer> currentRow;
BufferedReader reader = new BufferedReader( yourFile );
String line = null;
while((line = reader.readLine()) != null ) {
Scanner scanner = new Scanner( line );
currentRow = new ArrayList<Integer>();
while( scanner.hasNextInt()){
currentRow.add( scanner.nextInt() );
}
matrix.add( convertListToArray( currentRow )); // See: http://stackoverflow.com/questions/960431/how-to-convert-listinteger-to-int-in-java
}
file = File.open(myFile)
columns = File[] // columns is a file array, each one will contain a column from the original file
for each line in file
numbersInThatLine : Integer[]
numbersInThatLine = parseArrayFrom( line ) // created an array of int's from the given line
writeArraryToFiles( array=numbersInThatLine, files=columns ) // write each number in a separate file
end
close( file )
output = File.new()
for each file in columns
output.write( file )
end
close( output )
List<Integer[]> matrix = new ArrayList<Integer[]>();
List<Integer> currentRow;
BufferedReader reader = new BufferedReader( yourFile );
String line = null;
while((line = reader.readLine()) != null ) {
Scanner scanner = new Scanner( line );
currentRow = new ArrayList<Integer>();
while( scanner.hasNextInt()){
currentRow.add( scanner.nextInt() );
}
matrix.add( convertListToArray( currentRow )); // See: http://stackoverflow.com/questions/960431/how-to-convert-listinteger-to-int-in-java
}
List matrix=new ArrayList();
列出当前行;
BufferedReader reader=新的BufferedReader(您的文件);
字符串行=null;
而((line=reader.readLine())!=null){
扫描仪=新扫描仪(行);
currentRow=新的ArrayList();
while(scanner.hasNextInt()){
currentRow.add(scanner.nextInt());
}
matrix.add(convertListToArray(currentRow));//请参见:http://stackoverflow.com/questions/960431/how-to-convert-listinteger-to-int-in-java
}
注意:我甚至没有编译上面的代码,所以它可能不工作是家庭作业吗?如果是这样的话,给它贴上标签!不,那不是作业=)那是项目的一部分1)如果[]-表示法意味着列,那是对的。2) 好吧,如果我是对的,你试着在内存中保存一个矩阵。这不是个好主意-我的矩阵太大了。@Dmitry:嗯,那样的话,我真的不明白你的问题。你又想干什么?你能用这样的话更新你的问题吗:我有这个。。。。我想这样做……好的。我有一个包含矩阵的文件。我想创建一个新文件,其中会有一个转置矩阵。这个矩阵很大,所以你不能把它保存在RAM中。我一次只能从文件中读取矩阵的一行。是否清楚=)?好的,那么您希望将列作为行?