这是dbConnection.java,它返回一个二维浮点矩阵。
public float [][] connectToDB() throws Exception {
Class.forName("com.mysql.jdbc.Driver").newInstance();
// Load the database details into the variables.
String dbUrl = "jdbc:mysql://" + dbHost + ":" + dbPort + "/" + dataBase;
dbString = dbUrl;
try {
// Create the database connection, if it is closed.
if ((conn == null) || conn.isClosed()){
conn = DriverManager.getConnection(dbString, dbUsername, dbPassword);
Statement st = null;
ResultSet rs = null;
st = conn.createStatement();
String query = "select * from Placement";
rs = st.executeQuery(query);
ResultSetMetaData rsmd = rs.getMetaData();
int numCols = rsmd.getColumnCount();
while (rs.next()) {
float cg = rs.getFloat(1);
float m = rs.getFloat(2);
float a = rs.getFloat(3);
if ((cg > 7.0 && cg <= 9.0) && (m > 75 && m <= 90) && (a > 75 && a <= 90)){
CGPA.add(cg);
Marks.add(m);
Attendance.add(a);
}
}
Float[] cgpa = CGPA.toArray(new Float[0]);
Float[] marks = Marks.toArray(new Float[0]); //After elgibility .... Marks are of highest weight
Float[] attendance = Attendance.toArray(new Float[0]);
int lengthCgpa = cgpa.length;
int lengthMarks = marks.length;
int lengthAttendance = marks.length;
float [][] distMatrixCgpa = new float [lengthCgpa][lengthCgpa];
float [][] distMatrixMarks = new float [lengthMarks][lengthMarks];
float [][] distMatrixAttendance = new float [lengthAttendance][lengthAttendance];
float [][] distMatrixAverage = new float [lengthAttendance][lengthAttendance];
for (int r = 0; r < lengthCgpa; ++r) {
for (int c = 0; c < lengthCgpa; ++c ){
distMatrixCgpa [r][c] = calcDistance(cgpa[r], cgpa[c]);
}
}
for (int r = 0; r < lengthMarks; ++r) {
for (int c = 0; c < lengthMarks; ++c ){
distMatrixMarks [r][c] = calcDistance(marks[r], marks[c]);
}
}
for (int r = 0; r < lengthAttendance; ++r) {
for (int c = 0; c < lengthAttendance; ++c ){
distMatrixAttendance [r][c] = calcDistance(attendance[r], attendance[c]);
}
}
for (int r = 0; r < lengthAttendance; ++r) {
for (int c = 0; c < lengthAttendance; ++c ){
distMatrixAverage [r][c] = (distMatrixCgpa [r][c]
+ distMatrixMarks [r][c]
+ distMatrixAttendance [r][c])/3;
}
}
*****//This prints the correct float matrix but finally the function
//returns a null matrix! :(
distMatrixFormatted = new float [lengthCgpa][lengthCgpa];
for (int r = 0; r < lengthCgpa; ++r) {
for (int c = 0; c < lengthCgpa; ++c ) {
DecimalFormat df = new DecimalFormat("#.##");
distMatrixFormatted [r][c] = Float.valueOf(df.format(distMatrixAverage [r][c]));
System.out.print(distMatrixFormatted [r][c] + " ");
}
System.out.print("\n");
}*****
rs.close();
st.close();
}
} catch (SQLException sqlex) {
System.out.println("SQLException while connecting and inserting into " +
"the database table: " + sqlex.toString());
} catch (Exception ex) {
System.out.println("Exception while connecting and inserting into the" +
" database table: " + ex.toString());
ex.printStackTrace();
} finally {
// Close the Statement and the connection objects.
if (conn != null)
conn.close();
}
return distMatrixFormatted;
}
当我在main()中调用上面的函数时,它返回一个空矩阵.....(Agnes.java)
public static void main(String args[]) throws Exception {
try{
dbConnection P = new dbConnection();
float ad[][] = P.connectToDB();
int length = ad.length;
double [][] Array = new double [ad.length][ad.length];
ReadDistance readdistance = new ReadDistance();
Array = readdistance.read(length, Array);
*****
//Converting Float Matrix to Double!
//This produces Null matrix !!!!
for (int i = 0 ; i < Array.length ; ++i) {
for (int j = 0 ; j < Array.length; ++j) {
Array[i][j] = (double) Array[i][j];
System.out.print(Array[i][j] + " ");
}
System.out.print("\n");
}*****
Agnes agnes = new Agnes(Array, "single");
agnes.run();
} catch (Exception e){
e.printStackTrace();
}
}
如何在main函数中检索正确的float矩阵?
请帮忙!
PS:两者都只是代码片段
答案 0 :(得分:1)
我认为这是问题:
Array[i][j] = (double) Array[i][j];
你期待实现了什么?我怀疑你的意思是:
Array[i][j] = ad[i][j];
但是在这种情况下你根本不会使用Array
的先前值......你似乎在这两行中创建了两个“有用的”数据数组:
float ad[][] = P.connectToDB();
Array = readdistance.read(length, Array);
......但是你要把一个复制到另一个。为什么?你为什么不同时使用它们?
(顺便说一句,我强烈建议你重新考虑你的变量命名,并将connectToDb
重构为一个相当短的方法。你可能也想把它称为别的,因为它显然不仅仅是< em>将连接到数据库......