• 2D ArrayList Java

    Maxim6m0cj Member

    I am making a GUI – Based quiz game(netbeans) with a database now, my problem is I don’t know how to use the 2d ArrayList in java. I already have the idea of how I will store the following questions for the specific subject where the first dimensional array is for the ‘questionID’ and the second dimensional array is for the users ‘answers’ or something like this

    ArrayList>   question;
        ArrayList answer = new ArrayList();
        question['questionID']['userAnswer']
    

    This is the snippet

    ArrayList temp = new ArrayList();
                Class.forName("com.mysql.jdbc.Driver");
                Connection connect = DriverManager.getConnection("jdbc:mysql://localhost/root", "root", "easy");
                Statement stmt = connect.createStatement();
                ResultSet res = stmt.executeQuery("select * from question where subjectID = "+Student.subID+";");
                while(res.next()){
                    int q = res.getInt("questionID");
                    temp.add(q+"");
                    question.add(temp);
                    countQ++; //# of questions
                }
    

    I wonder if anyone could help me out how to store my data.

  • BuckRenard Member

    How about a DB structure like this? (There might be some issues with this structure – I didn’t really spend much time to think about all the scenarios.)

    question(question_id, question_desc, subject_id)
    question_answer(question_answer_id, question_answer_desc, question_id)
    user_answer(user_answer_id, user_id, question_answer_id)
    

    This can be stored in entity objects in Java. You’d have the same structure from the Database in the entity classes: Question, Question Answer and UserAnswer.

    To map it you could use an ORM framework (object relational mapping) like Hibernate, MyBATIS, or map the data back to the entity classes yourself (similar to what you did in your snippet). In this case you wouldn’t use a 2D array/list as the 2nd dimension is described by the entity object.

    So you’d use a structure like this when building up a list of questions (using the interface is always preferred over using an implementing class directly):

    List questions = new ArrayList<>();
    
Viewing 1 reply thread
  • You must be logged in to reply to this topic.
en_USEnglish