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

  • Home
  • SEARCH
  • 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 913555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:34:51+00:00 2026-05-15T17:34:51+00:00

My for loop is too long. As demo there is three for loops going

  • 0

My for loop is too long. As demo there is three for loops going on for methodB. Is there a way I could reduce this to a short for loop (one for loop)? Thanks

    public class MyMates  
    {

       private static TreeSet<String> myTable = new TreeSet<String>();

       private static String[] names1 = null;
       private static String[] names2 = null;
       private static String[] names3 = null;


       public MyMates()
       {
        super();
        myTable = new TreeSet<String>();
       }


       public  static String methodA(String aTemp)
       {
         String[] names1 = new String[] {"Amy", "Jose", "Jeremy", "Alice", "Patrick"};
         String[] names2 = new String[] { "Alan", "Amy", "Jeremy", "Helen", "Alexi"};
         String[] names3 = new String[] { "Adel", "Aaron", "Amy", "James", "Alice" };

         return aTemp;
      } 



    public static String methodB(String bTemp)
     {

        for (int i = 0; i < names1.length; i++) {
        myTable.add(names1[i]);
        }
        System.out.println(myTable);

        for (int i = 0; i < names2.length; i++) {
        myTable.add(names2[i]);
        }
        System.out.println(myTable);

       for (int i = 0; i < names3.length; i++) {
       myTable.add(names3[i]);
       }
       System.out.println(myTable);

    return bTemp;
   }

enter code here

  • 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-15T17:34:52+00:00Added an answer on May 15, 2026 at 5:34 pm

    An alternative which uses a 2 dimensional array and two nested loops
    (I don’t understand parameter and return values in methods A/B)

    public class MyMates {
        private static TreeSet<String> myTable = new TreeSet<String>();
        private static String[][] names;
    
        public MyMates() {
            super();
            myTable = new TreeSet<String>();
        }
    
        public static String methodA(String aTemp) {
            names = new String[][] {
                    { "Amy", "Jose", "Jeremy", "Alice", "Patrick" },
                    { "Alan", "Amy", "Jeremy", "Helen", "Alexi" },
                    { "Adel", "Aaron", "Amy", "James", "Alice" } };
    
            return aTemp;
        }
    
        public static String methodB(String bTemp) {
    
            for (int row = 0; row < names.length; row++) {
                for (int i = 0; i < names[row].length; i++) {
                    myTable.add(names[row][i]);
                    System.out.println(myTable);
                }
            }
            return "";
        }
    
        public static void main(String[] args) {
            methodA("");
            methodB("");
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So lets say there's a server process that takes way too long. The client
Maybe I've been staring at this problem for too long, maybe there isn't an
I use a scheduler to ensure that my loop does not take too long,
I'm not going to provide the code because it's too long. The python script
I think I have been staring at this code too long and I am
I did some timestamp prints from within script, this piece is taking too long:
Alright, so I've been yelled at for making questions too long, and too short,
Okay been working on this too long and can't get it to work. I
Maybe I've just been staring at this screen for too long, but I cannot
As far as I can tell, there is no reliable way to loop a

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.