Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8775187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:44:36+00:00 2026-06-13T18:44:36+00:00

I am trying to create a sparse matrix with the data from database. Here

  • 0

I am trying to create a sparse matrix with the data from database. Here is my code:

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) {
            }
        }
    }
}

I want to create the sparese matrix with 5 rows and original column size adsList.size()=641707. The for (wordSayac = 0; wordSayac < 5; wordSayac++) statement works fine for wordSayac=0 but while it is executing for wordSayac=1, I get the error: index out of bounds. Here is the output of the program:

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:44:38+00:00Added an answer on June 13, 2026 at 6:44 pm

    The problem in your code is that you assign adSayac = 0 before starting the for loop, but after the first iteration you don’t assign it back to 0, meaning whatever the value ended for wordSayac=0, now when executing wordSayac=1 that’s the first value used, and so forth.

    you probably want to assign it back to zero as the first line inside the for loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
I'm trying to make a wrapper for some C-based sparse-matrix-handling code (see previous question
I am trying to create a sparse matrix which has a 2D pattern run
I'm trying to create a sparse vector class in C++, like so: template<typename V,
Ok so I am trying create a login script, here I am using PHP5
I trying create a class derivated from System.Web.UI.Page and in override Render i set
I am trying create a xmldocument object by a different XML see the code
I'm trying to cluster some data with python and scipy but the following code
I'm trying to create a fly-by-seat-of-pants sparse vector library in scala and I'm hitting
I am trying to create variable number of sparse matrices. At first the best

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.