代码:
import com.sun.xml.internal.ws.commons.xmlutil.Converter;
import javax.management.relation.RoleInfo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Formatter;
import java.util.List;
public class multi {
public static void main(String[] args) {
List<List<Double>> minList = new ArrayList<>();
List<Double> l1 = new ArrayList<>();
l1.add(1100.3);
l1.add(1200.4);
l1.add(1300.5);
minList.add(l1);
List<Double> l2 = new ArrayList<>();
l2.add(2100.4);
l2.add(2200.5);
l2.add(2300.5);
List<Double> l3 = new ArrayList<>();
l3.add(3100.5);
l3.add(3200.6);
l3.add(3300.5);
minList.add(l2);
minList.add(l3);
//minList.add(l3);
System.out.println("min");
for (int i = 0; i < minList.size(); i++) {
for (int j = 0; j < minList.get(i).size(); j++) {
System.out.print(" " + minList.get(i).get(j));
}
System.out.println("");
}
System.out.println(" max:");
List<List<Double>> maxList = multiMatrix(minList, 4, 3);
for (int i = 0; i < maxList.size(); i++) {
for (int j = 0; j < maxList.get(i).size(); j++) {
System.out.print(" " + new Formatter().format("%.2f", maxList.get(i).get(j)).toString());
}
System.out.println("");
}
}
private static List<List<Double>> multiMatrix(List<List<Double>> inData, int scaleCol, int scaleRow) {
int rowIn = inData.size();
int colIn = inData.get(0).size();
int rowOut = (rowIn - 1) * scaleRow + 1;
int colOut = (colIn - 1) * scaleCol + 1;
//List<List<Double>> outData = new ArrayList<>();
//int [ ][ ] arr=new int [5][3]; 为“5行3例”
Double[][] outDataAry = new Double[rowOut][colOut];
for (int col = 0; col < colIn - 1; col++) {
for (int row = 0; row < rowIn - 1; row++) {
double x0y0 = inData.get(row).get(col);
double x1y0 = inData.get(row).get(col + 1);
double x0y1 = inData.get(row + 1).get(col);
double x1y1 = inData.get(row + 1).get(col + 1);
for (int c = 0; c < scaleCol; c++) {
for (int r = 0; r < scaleRow; r++) {
double xDis = (double) c / scaleCol;
double yDis = (double) r / scaleRow;
double data = x0y0 * (1 - xDis) * (1 - yDis)
+ x0y1 * (1 - xDis) * (yDis)
+ x1y0 * (xDis) * (1 - yDis)
+ x1y1 * (xDis) * (yDis);
outDataAry[(row) * scaleRow + r][(col) * scaleCol + c] = data;
}
}
//处理最后一行
if ((row + 2) == rowIn) {
for (int i = 0; i < scaleCol; i++) {
outDataAry[rowOut - 1][(col) * scaleCol + i] =
inData.get(rowIn - 1).get(col) * (1 - (double) i / scaleCol)
+ inData.get(rowIn - 1).get(col + 1) * ((double) i / scaleCol);
}
}
}
}
//处理最后一列
for (int row = 0; row < rowIn - 1; row++) {
for (int i = 0; i < scaleRow; i++) {
outDataAry[row * scaleRow + i][colOut - 1] =
inData.get(row).get(colIn - 1) * (1 - (double) i / scaleRow)
+ inData.get(row + 1).get(colIn - 1) * ((double) i / scaleRow);
}
}
outDataAry[rowOut - 1][colOut - 1] = inData.get(rowIn - 1).get(colIn - 1);
List<List<Double>> outData = new ArrayList<>();
for (int i = 0; i < rowOut; i++) {
outData.add(Arrays.asList(outDataAry[i]));
}
return outData;
}
}
效果:
1100.3 1200.4 1300.5
2100.4 2200.5 2300.5
max:
1100.30 1125.32 1150.35 1175.38 1200.40 1225.43 1250.45 1275.48 1300.50
1433.67 1458.69 1483.72 1508.74 1533.77 1558.78 1583.80 1608.82 1633.83
1767.03 1792.06 1817.08 1842.11 1867.13 1892.14 1917.15 1942.16 1967.17
2100.40 2125.43 2150.45 2175.48 2200.50 2225.50 2250.50 2275.50 2300.50