データベースからのデータを使用して疎行列を作成しようとしています。これが私のコードです:
package helloworld;
import cern.colt.matrix.impl.RCDoubleMatrix2D;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class CreateVectors {
public static void main(String[] args) throws IOException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/kdd";
String user = "user";
String password = "pass";
List<Integer> wordsList = new ArrayList<Integer>();
List<Integer> adsList = new ArrayList<Integer>();
try {
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
//create wordsList
String sqlQuery = "Select wordID from wtpji_normalized_ordered group by wordID";
rs = stmt.executeQuery(sqlQuery);
while (rs.next()) {
wordsList.add(rs.getInt(1));
}
rs.close();
System.out.println("wordsList");
//create adsList
sqlQuery = "Select adID from wtpji_normalized_ordered group by adID";
rs = stmt.executeQuery(sqlQuery);
while (rs.next()) {
adsList.add(rs.getInt(1));
}
rs.close();
System.out.println("adsList");
//create one vector
RCDoubleMatrix2D mymatrix = new RCDoubleMatrix2D(5, adsList.size());
int adSayac = 0, wordSayac;
mymatrix.assign(0);
for (wordSayac = 0; wordSayac < 5; wordSayac++) {
sqlQuery = "Select adID, Nwtpji from wtpji_normalized_ordered where wordID=" + wordsList.get(wordSayac);
System.out.println("where WordID= " + wordsList.get(wordSayac));
rs = stmt.executeQuery(sqlQuery);
rs.first();
while(!rs.isLast()){
if (adsList.get(adSayac) == rs.getInt(1)) {
float Fwtpji = rs.getFloat(2);
double Dwtpji = Fwtpji;
Dwtpji = Math.floor(Dwtpji * 100) / 100;
mymatrix.setQuick(wordSayac, adSayac, Dwtpji);
adSayac++;
rs.next();
}
else{
adSayac++;
}
}
System.out.println(wordsList.get(wordSayac));
rs.close();
System.out.println("Close resultset");
}
System.out.println("FINITO");
} catch (SQLException ex) {
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
}
}
}
}
5 行で元の列サイズの adsList.size()=641707 のスペア行列を作成したいと考えています。for (wordSayac = 0; wordSayac < 5; wordSayac++) ステートメントは、wordSayac=0 の場合は正常に機能しますが、wordSayac=1 の場合は実行中にエラーが発生します: 範囲外のインデックス。プログラムの出力は次のとおりです。
0
Close resultset
where WordID= 1
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 641707, Size: 641707
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at helloworld.CreateVectors.main(CreateVectors.java:95)
Java Result: 1