Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i add for each loop in place of for loop in my code? Is there any possible way to increase performance in my code? Thanks in advance

Java
import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.Arrays;
    import java.util.Scanner;
    public class permutation {
    	
        static Scanner s=new Scanner(System.in);
        static String 
        input =s.nextLine();
        static int size = s.nextInt();
        
       static boolean[] num = new boolean[input.length()];
        static FileWriter fstream =null; 
        
        public static void generate(String data, BufferedWriter out) {
        	 
        	if (data.length() == size) {
            	
        		try {
        			out.newLine();
    				out.write(data);
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    
    
                
              
                return;
            } 
            for (int i = 0; i < input.length(); ++i) {
                if (!num[i]) {
                  num[i] = true;
                    generate(data + input.charAt(i), out);
                    num[i] = false;      
               }
                
            }
        }
    
      
        public static void main(String[] args) throws Exception {
        	fstream = new FileWriter("D:\\out2.txt",true);
        	   BufferedWriter out=new BufferedWriter(fstream);
            char[] chars = input.toCharArray();
            Arrays.sort(chars);
            String sorted = new String(chars);
            input=sorted;
           generate("", out);
            out.close();
           
            
        }
    }


What I have tried:

Iam unable to typecast from object to integer
Posted
Updated 20-Oct-16 0:32am
v2

1 solution

Substituting the for with foreach, though possible, would be impractical (and ugly). In any case you wouldn't get any performance gain, with such replacement.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900