如何在代码中为每个循环代替for循环添加? [英] How can I add for each loop in place of for loop in my code?

查看:85
本文介绍了如何在代码中为每个循环代替for循环添加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何为代码中的for循环添加每个循环?有没有办法提高我的代码性能?提前致谢



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

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

推荐答案

用foreach代替for,虽然可能,但是不切实际(而且很难看)。在任何情况下,你都不会获得任何性能上的提升。
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.


这篇关于如何在代码中为每个循环代替for循环添加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆