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 6883077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T05:20:11+00:00 2026-05-27T05:20:11+00:00

I’ve trying to display random data from the database example in my database i

  • 0

I’ve trying to display random data from the database example in my database i have total of 20 question. i want to display randomly 10 qns every time i access the app. And for the second time that i access it, it will display the other 10 qns that has not been display before.

my code:

ArrayList<Integer> randList = new ArrayList<Integer>();
    Random randGenerator = new Random();

    ArrayList<String> list2 = new ArrayList<String>(db.selectData("MCQ",new String [] {"_id"}, "testId=0", null, null, null, null));

    //random 10 n.o
    for(int i=0;i<10;i++)
    {
        int randNum = randGenerator.nextInt(list2.size());

        while (randList.contains(randNum))
        {
            randNum = randGenerator.nextInt(list2.size());
        }
        randList.add(randNum);
        Log.d("rand no:"+i,String.valueOf(randNum));
    }

    ArrayList<String> idList = new ArrayList<String>();
    //get the 10 _id that are chosen
    for (int i=0; i<10;i++)
    {
        idList.add(list2.get(Integer.valueOf(randList.get(i))));
        Log.d("id:"+i,String.valueOf((list2.get(Integer.valueOf(randList.get(i))))));
    }

    ArrayList<String> list3 = new ArrayList<String>(db.selectData("MCQ",new String [] {"Question"}, null, null, null, null, null));
    Log.d("size",String.valueOf(list3.size()));
    ArrayList<String> qnsList = new ArrayList<String>();

    // == set question in arrangement of randList == //
    for(int i=0;i<=list3.size();i++)
    {
        qnsList.add(list3.get(i));
        Log.d("_id:"+i,String.valueOf(list3.get(i)));
    }

i have tested it again and again and realized that weirdly, it could not read the data at _id=20

this is what i get in my log cat:

E/AndroidRuntime(1284): java.lang.IndexOutOfBoundsException: Invalid index 20, size is 20
E/AndroidRuntime(1284):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)

there are more, but i think it is useless as i have tried running with just _id=19 and it works but once i change to 20 it crashed.

  • 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-05-27T05:20:12+00:00Added an answer on May 27, 2026 at 5:20 am

    Change this line:

    for(int i=0;i<=list3.size();i++)
    

    to be:

    for(int i=0;i<list3.size();i++)
    

    You were iterating through the loop 1 too many times (on the last iteration, when i = list3.size() you would get an IndexOutOfBoundsException when you do list3.get(i) since list indexes are 0-based so the twentieth and last element is actually at index 19).

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
i want to parse a xhtml file and display in UITableView. what is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I want to construct a data frame in an Rcpp function, but when I
I have a reasonable size flat file database of text documents mostly saved in
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.