我建議在4個階段的分裂問題:閱讀文本,建立字符串單元陣列,建立整數矩陣陣列,做矩陣的乘積:
import java.util.ArrayList;
import java.util.List;
public class MatrixMul {
static String[][] text2StrMatrices(String[] text) {
List<String[]> w = new ArrayList<>(12);
for(String line : text)
{
List<String> l = new ArrayList<>(line.length()/2);
for(int i = 0; i < line.length(); ++i)
{
char c = line.charAt(i);
if(c == ' ')
{
l.add(" ");
++i;
}
else {
String num = "";
while(c != ' ' && i < line.length()) {
num += c;
if(++i < line.length()) {
c = line.charAt(i);
}
}
l.add(num);
}
}
w.add(l.toArray(new String[l.size()]));
}
return w.toArray(new String[w.size()][]);
}
static int countValues(String[] row)
{
int count = 0;
for(String value : row) {
if(value.trim().length() > 0) {
++count;
}
}
return count;
}
static int[][][] strMatrices2IntegerMatrices(String[][] str) {
int count = str[0].length;
int row = 0;
while(row < str.length && count == countValues(str[row])) {
++row;
}
int first = -1;
for(int i = 0; first == -1 && i < str[row].length; ++i) {
if(str[row][i].trim().length() > 0) {
first = i;
}
}
int columns = 0;
if(first > 0) {
columns = first;
}
else {
columns = countValues(str[row]);
}
List<int[]> w = new ArrayList<>(4);
for(int r = 0; r < (first == 0 ? str.length : row); ++r)
{
int[] aRow = new int[columns];
for(int c = 0; c < columns; ++c) {
aRow[c] = Integer.parseInt(str[r][c]);
}
w.add(aRow);
}
int[][][] result = new int[2][][];
result[0] = w.toArray(new int[w.size()][]);
w.clear();
for(int r = 0; r < (first == 0 ? row : str.length); ++r)
{
int[] aRow = new int[str[0].length-columns];
for(int c = columns; c < str[r].length; ++c) {
aRow[c-columns] = Integer.parseInt(str[r][c]);
}
w.add(aRow);
}
result[1] = w.toArray(new int[w.size()][]);
return result;
}
private static int[][] matricesProduct(int[][] a, int[][] b)
{
int m = a.length;
int n = b[0].length;
int p = b.length;
assert (m == n) || (a[0].length == p): "Incompatible dimensions";
int[][] prod = null;
if(p > -1) {
prod = new int[m][n];
for(int i = 0; i < m; ++i) {
for(int j = 0; j < n; ++j) {
for(int k = 0; k < p; ++k) {
prod[i][j] += a[i][k] * b[k][j];
}
}
}
}
return prod;
}
static void test(
String title,
String[] text,
String[][] expectedStrMatrices,
int[][][] expectedMatrices,
int[][] expectedProduct)
{
System.out.println(title);
final String[][] observedStrMatrices = text2StrMatrices(text);
assert compare(expectedStrMatrices, observedStrMatrices):
"text2StrMatrices() failed";
final int[][][] observedMatrices =
strMatrices2IntegerMatrices(observedStrMatrices);
assert compare(expectedMatrices, observedMatrices):
"strMatrices2IntegerMatrices() failed";
final int[][] observedProduct =
matricesProduct(observedMatrices[0], observedMatrices[1]);
displayMatrix(observedProduct);
assert compare(expectedProduct, observedProduct):
"matricesProduct() failed";
}
public static void main(String[] args) {
final String[] text1 = {
"1 2 1 2 3",
"3 4 4 5 6",
"5 6",
"7 8",
};
final String[][] expectedStrMatrices1 = {
{ "1", "2", "1", "2", "3" },
{ "3", "4", "4", "5", "6" },
{ "5", "6" },
{ "7", "8" },
};
final int[][][] expectedMatrices1 = {{
{ 1, 2 },
{ 3, 4 },
{ 5, 6 },
{ 7, 8 },
},{
{ 1, 2, 3 },
{ 4, 5, 6 },
}};
final int[][] expectedProduct1 = {
{ 9, 12, 15 },
{ 19, 26, 33 },
{ 29, 40, 51 },
{ 39, 54, 69 },
};
test("First test case", text1, expectedStrMatrices1, expectedMatrices1, expectedProduct1);
final String[] text2 = {
"1 2 1 2 3",
"3 4 4 5 6",
" 7 8",
};
final String[][] expectedStrMatrices2 = {
{ "1", "2", "1", "2", "3" },
{ "3", "4", "4", "5", "6" },
{ " ", " ", " ", "7", "8" },
};
final int[][][] expectedMatrices2 = {{
{ 1, 2, 1 },
{ 3, 4, 4 },
},{
{ 2, 3 },
{ 5, 6 },
{ 7, 8 },
}};
final int[][] expectedProduct2 = {
{ 19, 23 },
{ 54, 65 },
};
test("Second test case", text2, expectedStrMatrices2, expectedMatrices2, expectedProduct2);
}// void main(String[] args)
private static void displayMatrix(int[][] matrix) {
for(int i = 0; i < matrix.length; ++i) {
for(int j = 0; j < matrix[i].length; ++j) {
System.out.printf("%2d ", matrix[i][j]);
}
System.out.println();
}
}
static boolean compare(String[][] left, String[][] right) {
if(left.length != right.length) {
return false;
}
for(int i = 0; i < left.length; ++i) {
if(left[i].length != right[i].length) {
return false;
}
for(int j = 0; j < left[i].length; ++j) {
if(! left[i][j].equals(right[i][j])) {
return false;
}
}
}
return true;
}
static boolean compare(int[][][] left, int[][][] right) {
if(left.length != right.length) {
return false;
}
for(int i = 0; i < left.length; ++i) {
if(left[i].length != right[i].length) {
return false;
}
for(int j = 0; j < left[i].length; ++j) {
if(left[i][j].length != right[i][j].length) {
return false;
}
for(int k = 0; k < left[i][j].length; ++k) {
if(left[i][j][k] != right[i][j][k]) {
return false;
}
}
}
}
return true;
}
private static boolean compare(int[][] left, int[][] right)
{
if(left.length != right.length) {
return false;
}
for(int i = 0; i < left.length; ++i) {
if(left[i].length != right[i].length) {
return false;
}
for(int j = 0; j < left[i].length; ++j) {
if(left[i][j] != right[i][j]) {
return false;
}
}
}
return true;
}
}
在哪裏做這些文本文件都來自? –
它們來自JUnit測試,因此它們是不可變的。 –
@ThorbjørnRavnAndersen我相信如果我們提供了最可能的答案給你的問題,版主會介入。 –