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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:02:04+00:00 2026-05-11T19:02:04+00:00

This might be stupid, but I’m want to know if it’s possible, lets start

  • 0

This might be stupid, but I’m want to know if it’s possible, lets start with 5×5 a matrix

int[][] x = new int[5][5];      
Random rg = new Random();

now let’s fill it with Pseudo Random information

for(int i=0;i<5;i++){
    for(int j =0; j<5;j++){
        x[i][j] = rg.nextInt(); 
    }           
}

but how can I do this right with one Single for?

for(int i=0, j=0; i<5; (j==5?i++, j=0:j++){
    x[i][j] = rg.nextInt();
}

this is not working 🙁

  • 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-11T19:02:04+00:00Added an answer on May 11, 2026 at 7:02 pm

    You need to compute the row and column from a single index, then:

    for(int i = 0; i < 5 * 5; i++)
    {
       int row = i / 5;
       int column = i % 5;
       x[row][column] = rg.nextInt();
    }
    

    The use of / and % is classic, here: as you iterate over the indices of the matrix, the division is used to figure out which row you’re on. The remainder (%) is then the column.

    This gorgeous ASCII art shows how the 1-dimensional indices are located in the 2D matrix:

     0  1  2  3  4
     5  6  7  8  9
    10 11 12 13 14
    15 16 17 18 19
    20 21 22 23 24
    

    It should be clear that for any value in the first row, that value divided by 5 is the row index itself, i.e. they’re all 0.

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

Sidebar

Related Questions

This might be a stupid question but I'm new at all this. I want
I know this might be a very stupid question but I am new to
I'm new to WPF and therefore this might be a stupid question, but I
This might be a stupid questions but I wanted to know what happens if
Ok, so this might be kinda stupid, I dunno know, but I was wondering
I'm very new to web development and I know this might be stupid approach
I know this might sound very stupid but I am a total noob in
This might be a stupid question but I want to add a - into
This might be a stupid question but is it possible to use javascript/jQuery in
So this might be a stupid question but... I want to package a specific

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.