Solved

Multiple for loops ?

Posted on 2001-06-08
2
315 Views
Last Modified: 2010-08-05
Q : Is there any way to avoid using multiple for loop ?

For example :
I have three numbers say 1, 2 and 3
1 represents A, B, C, D, E
2 represents F, G, H, I,
3 represents K, L, M, N,

I want to produce an output combination of these options like
AFK
AFL
AFM
...
...
...
so on..

In this example I have 3 numbers but practically I have 10 Numbers.

What I have done is looped in 10 for loops and getting the result inside inner most loop.
Working fine ! but it looks ugly

Again I have a problem, If user asks for comination of 1,2,3 and 6,7,8,9 ( skipped 4, 5)
How should I proceed ???

Pl let me know if U R not clear with the question.

Thanks,
Kishor


0
Comment
Question by:kishor_voderhobli
2 Comments
 
LVL 2

Accepted Solution

by:
bagi112599 earned 100 total points
ID: 6169051
Say,
1 represents A, B, C, D, E (call it section 1)
2 represents F, G, H, I, (call it section 2)
3 represents K, L, M, N, (call it section 3 etc.)

and we have to create create all possible 3 char words
so that i'th char is from i'th section right?
Here we go:
import java.util.*;
class ManyLoops
{
  Vector sectors;
  Vector words;
  public ManyLoops(){
    sectors = new Vector();
    words = new Vector();
    words.addElement("");
  }
     public static void main(String[] args){
    ManyLoops manyLoops = new ManyLoops();
    manyLoops.init();
    manyLoops.makeWords();
    manyLoops.publish();
     }

     public void init(){
    sectors.addElement("ABCDE");
    sectors.addElement("FGHI");
    sectors.addElement("KLMN");
  }

     public void makeWords(){
    for(int j=0; j<sectors.size();j++){
      extend((String)sectors.elementAt(j));
    }
  }

     public void extend(String sector){
    int len = words.size();
    for(int i=0; i<len;i++){
      for(int j=0; j<sector.length()-1;j++){
          words.addElement((String)words.elementAt(i)+ sector.substring(j,j+1));
      }
    }

    for(int i=0; i<len;i++){
        words.setElementAt(
              (String)words.elementAt(i)+
                  sector.substring(sector.length()-1,sector.length()), i
                );
    }

     }

  public void publish(){
  //prints out all words it knows:
    for(int i=0; i<words.size();i++){
      System.out.println((String)words.elementAt(i));
    }
     }
}



0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 8975396
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:


--  points to bagi


Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
sudhakar_koundinya
EE Cleanup Volunteer
---------------------
If you feel that your question was not properly addressed, or that none of the comments received were appropriate answers, please post your concern in THIS thread.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Java and GPO 11 73
Error with Java/Cache JDBC Classpath 2 31
rhino JavaScript import, load 25 69
Basic Java Case or If-Else statement... 3 43
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now