如何在Java的Lambda表达式中的两个foreach循环之间打印一条新线

发布于 2025-02-04 18:51:56 字数 769 浏览 4 评论 0原文

我正在尝试在Java中创建lambda表达式以运行以下线程。我需要做 结果如下。

12345
12345
12345
12345

但是,根据我编写的代码,它是由1234512345123451234512345。我确定了这一点,因为第二次foreach循环之后缺乏println(“”)语句。因此,我尝试编写新的println(“”)语句,但它不起作用。如何解决此问题?我应该在哪里写println(“”)语句?

public class Tb {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Runnable r2 = () -> {
            IntStream.iterate(1,i-> i+1).limit(5).
            forEach(i-> IntStream.iterate(1, j->j+1).limit(5).
                    forEach(j->{System.out.print(j);}) 
                    
            
                    
            
                    );
            
        };
        
        new Thread(r2).start();

    }

}


I am trying to create a lambda expression in java to run the following thread. I need to make the
outcome as below.

12345
12345
12345
12345

But according to the code I have written it comes as 1234512345123451234512345. I have identified this happens because of the lack of a println("") statement after the second forEach loop. Therefore I tried writting a new println("") statement but it does not work. How can fix this issue? Where should I write the println("") statement here?

public class Tb {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        Runnable r2 = () -> {
            IntStream.iterate(1,i-> i+1).limit(5).
            forEach(i-> IntStream.iterate(1, j->j+1).limit(5).
                    forEach(j->{System.out.print(j);}) 
                    
            
                    
            
                    );
            
        };
        
        new Thread(r2).start();

    }

}


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

十二 2025-02-11 18:51:56

只需在第二个intstream之前添加新行。

IntStream.iterate(1, i -> i + 1).limit(5).forEach(i -> {
                System.out.println();
                IntStream.iterate(1, j -> j + 1).limit(5).forEach(System.out::print);
            });

NB:我还为我的喜好修改了最后的 foreach 。

Just add a new line before the 2nd IntStream.

IntStream.iterate(1, i -> i + 1).limit(5).forEach(i -> {
                System.out.println();
                IntStream.iterate(1, j -> j + 1).limit(5).forEach(System.out::print);
            });

NB: I have also modified the last foreach for my preference.

东京女 2025-02-11 18:51:56

要在每个foreach循环之后添加线路,需要在第二个foreach之后添加println,例如下面。

`

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Runnable r2 = () -> {
        IntStream.iterate(1, i -> i + 1).limit(5)
                .forEach(i -> {
                    IntStream.iterate(1, j -> j + 1).limit(5).forEach(j -> {
                        System.out.print(j);

                    });
                    System.out.println();
                });

    };

    new Thread(r2).start();

}

To add the line after every forEach loop need to add println after the second forEach such as below.

`

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Runnable r2 = () -> {
        IntStream.iterate(1, i -> i + 1).limit(5)
                .forEach(i -> {
                    IntStream.iterate(1, j -> j + 1).limit(5).forEach(j -> {
                        System.out.print(j);

                    });
                    System.out.println();
                });

    };

    new Thread(r2).start();

}
哆啦不做梦 2025-02-11 18:51:56

只需将您的语句放入curly brackets {} 中,然后添加println语句:

public static void main(String[] args) {
    Runnable r2 = () -> {
        IntStream.iterate(1,i-> i+1)
                .limit(5)
                .forEach(i-> {
                    IntStream.iterate(1, j -> j + 1)
                            .limit(5)
                            .forEach(j -> {
                                System.out.print(j);
                            });
                    System.out.println();
                });
    };
    new Thread(r2).start();
}

Just put your statement in your outer forEach in curly brackets {} and add a println statement:

public static void main(String[] args) {
    Runnable r2 = () -> {
        IntStream.iterate(1,i-> i+1)
                .limit(5)
                .forEach(i-> {
                    IntStream.iterate(1, j -> j + 1)
                            .limit(5)
                            .forEach(j -> {
                                System.out.print(j);
                            });
                    System.out.println();
                });
    };
    new Thread(r2).start();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文