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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:23:57+00:00 2026-05-18T12:23:57+00:00

I Have written such a code below and I will pass two lists to

  • 0

I Have written such a code below and I will pass two lists to this method which tree list is empty list and array list is a list of objects.

    public void permute(List<Element> tree, List<Element> array) {
    if (array.size() <= 1) {
        for (Element i : array) {
            tree.add(i);
        }
        System.out.println(tree.toString());
    } else {
        for (int i = 0; i < array.size(); i++) {
            try {
                list = array.subList(0, i);
                list.add(array.get(i + 1));
                tree.add(array.get(i));
                permute(tree, list);

            } catch (StringIndexOutOfBoundsException exception) {
                exception.printStackTrace();
            }
        }
    }

}

but it will print the lines below:

[digit :7  probability: 66.0, digit :7  probability: 66.0]
[digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0]
[digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0]
[digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0]
[digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0]
[digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0]
[digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0, digit :7  probability: 66.0]

also lines are continued but I copied here just some of them!
what is its problem?
please help me
thanks
EDIT:
This is the code for Strings and I used it for my objects :

  import java.io.*;
public class NewClass{
    public static void main(String args[]) throws IOException{
        String str;
        System.out.println("Enter the initial string");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        str=br.readLine();
        System.out.println("Permutations are :");
        permute("", str);
    }

  public static void permute(String beginningString, String endingString) {
    if (endingString.length() <= 1)
      System.out.println(beginningString + endingString);
    else
      for (int i = 0; i < endingString.length(); i++) {
        try {
          String newString = endingString.substring(0, i) + endingString.substring(i + 1);

          permute(beginningString + endingString.charAt(i), newString);         
        } catch (StringIndexOutOfBoundsException exception) {
          exception.printStackTrace();
        }
      }
  }
}
  • 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-18T12:23:58+00:00Added an answer on May 18, 2026 at 12:23 pm

    Try the code below. I have simply taken your permute method which works for strings, and translated it (almost line-by-line) into one which should work for any kind of list.

    public static <E> void permute2(List<E> beginningList, List<E> endingList) {
        if (endingList.size() <= 1) {
            //combine the two lists
            List<E> result = new ArrayList<E>(beginningList);
            result.addAll(endingList);
            System.out.println(result);
        } else
            for (int i = 0; i < endingList.size(); i++) {
    
                //create a list without the ith element
                List<E> newList = new ArrayList<E>(endingList);
                newList.remove(i);
    
                //create a list by adding the ith element to beginning
                List<E> newBeginning = new ArrayList<E>(beginningList);
                newBeginning.add(endingList.get(i));
    
                permute2(newBeginning, newList);
            }
    }
    

    Example:

    public static void main(String args[]) {
        permute2(new ArrayList<Integer>(), new ArrayList<Integer>(Arrays.asList(1, 2, 3)));
    }
    

    Prints:

    [1, 2, 3]
    [1, 3, 2]
    [2, 1, 3]
    [2, 3, 1]
    [3, 1, 2]
    [3, 2, 1]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written such the code below that ; I have an array list
Hi I have written such a code below but it returns wrong output which
I have written this code to convert string in such format 0(532) 222 22
I have written code such as the following, which by assumption shouldn't compile. I'm
Hi I have written such a this code and I want to know that
have written this little class, which generates a UUID every time an object of
I have written this code inside a servlet to delete certain records from three
I have written this code in JavaScript and works perfectly fine when I include
http://jsfiddle.net/B2rdD/3/ I've Created 25 buttons using HTML and I have written jquery code such
I have written some code which performs a Monte Carlo simulation and produces curves

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.