0

我正在尝试使用数据库中的数据创建一个稀疏矩阵。这是我的代码:

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
4

1 回答 1

0

您的代码中的问题是您在开始 for 循环之前分配了 adSayac = 0,但在第一次迭代之后您没有将其分配回 0,这意味着无论 wordSayac=0 的值结束,现在执行 wordSayac=1 时使用的第一个值,依此类推。

您可能希望将其分配回零作为 for 循环内的第一行。

于 2012-11-03T11:30:21.240 回答