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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:24:29+00:00 2026-06-09T21:24:29+00:00

I have four arrays, each with 3 (but can be more) elements. I am

  • 0

I have four arrays, each with 3 (but can be more) elements. I am trying to fill a 4 x aa.length*bb.length*cc.length*dd.length array with all possible combinations of each element. I am trying to do this with nested for loops, but my logic is faulty. I am not sure what the most efficient way to do this would be.
this is what my caffeine starved brain has come up with so far.

String[] AA={DDDD, HHHH, ZZZZ};
String[] BB={DDDD, HHHH, ZZZZ};
String[] CC={DDDD, HHHH, ZZZZ};
String[] DD={DDDD, HHHH, ZZZZ};


String[][] 2Darray = new String[4][AA.length*BB.length*CC.length*DD.length];

for (int i = 0; i <AA.length; i++){

  for (int j = 0; j < BB.length; j++){

    for (int k = 0; k < CC.length; k++){

      for (int L = 0; L < DD.length; L++){

        2Darray[3][i+j+k+L] = DD[L]; 
        2Darray[2][i+j+k] = CC[k];
        2Darray[1][i+j] = BB[j];
        2Darray[0][i] = AA[i];

      }
    }
  }
}

the printed output of this looks like:

DDDD DDDD DDDD DDDD
HHHH DDDD DDDD DDDD
ZZZZ DDDD DDDD DDDD
null HHHH DDDD DDDD
null ZZZZ DDDD DDDD
null null HHHH DDDD
null null ZZZZ DDDD
null null null HHHH
null null null ZZZZ
null null null null
null null null null
null null null null
...etc

What is a better way to approach this?

  • 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-09T21:24:30+00:00Added an answer on June 9, 2026 at 9:24 pm

    Try this

        String[] AA = {"DDDD", "HHHH", "ZZZZ"};
        String[] BB = {"DDDD", "HHHH", "ZZZZ"};
        String[] CC = {"DDDD", "HHHH", "ZZZZ"};
        String[] DD = {"DDDD", "HHHH", "ZZZZ"};
    
    
        String[][] result = new String[4][AA.length * BB.length * CC.length * DD.length];
    
        int row = 0;
        for (int i = 0; i < AA.length; i++) {
    
            for (int j = 0; j < BB.length; j++) {
    
                for (int k = 0; k < CC.length; k++) {
    
                    for (int L = 0; L < DD.length; L++) {
    
                        result[3][row] = DD[L];
                        result[2][row] = CC[k];
                        result[1][row] = BB[j];
                        result[0][row] = AA[i];
                        System.out.println(result[0][row] + " " +result[1][row] + " " +result[2][row] + " " +result[3][row]);
                        row++;
                    }
                }
            }
        }
    

    and output

    DDDD DDDD DDDD DDDD
    DDDD DDDD DDDD HHHH
    DDDD DDDD DDDD ZZZZ
    DDDD DDDD HHHH DDDD
    DDDD DDDD HHHH HHHH
    DDDD DDDD HHHH ZZZZ
    DDDD DDDD ZZZZ DDDD
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I will have four word list arrays. $array1 = array('red','blue','green');//it can have more elements
How can I store arrays in single array? e.g. I have four different arrays,
I have an array which can be any size (in rows) but is always
I'm lost trying to create and populate arrays. I have four wrapper panels, which
I have a project that requires the following. Four arrays will be declared in
I will be short, what i have is array=( one.a two.b tree.c four.b five_b_abc)
I have four arrays that are coming in from the client. Let's say that
I have four product gallery with the possibility to check the detail of each
I have a ruby script array when each element needs processing : threads =
In my UIViewController descendant, I have a set of four blocks. Each block is

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.