线程“main”中的异常java.lang.IndexOutOfBoundsException - Java

时间:2012-11-03 11:22:52

标签: java matrix indexoutofboundsexception

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

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&lt; 5; wordSayac ++)语句适用于wordSayac = 0但是当它为wordSayac = 1执行时,我得到错误:index out of bounds。以下是该计划的输出:

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

1 个答案:

答案 0 :(得分:0)

您的代码中的问题是您在开始for循环之前分配adSayac = 0,但在第一次迭代之后您不会将其分配回0,这意味着无论wordSayac = 0的值是什么,现在当执行wordSayac = 1表示使用的第一个值,依此类推。

你可能想把它作为for循环中的第一行归回零。