java-通过2D坐标流/ IntRange?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-通过2D坐标流/ IntRange?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1210字,纯文字阅读大概需要2分钟。
内容图文
![java-通过2D坐标流/ IntRange?](/upload/InfoBanner/zyjiaocheng/668/9efe8ec0ccbb49fb879670d569e21efd.jpg)
我有以下课程:
public class Rectangle
{
public int width, height;
public Point start;
public Rectangle(Point start, int width, int height)
{
this.start = start;
this.width = width;
this.height = height;
}
}
public class Point
{
public int x, y;
public Point(int x, int y)
{
this.x = x;
this.y = y;
}
}
我想做一个Rectangle类的方法,以便返回Point类型的Java 8 Stream,该流将在矩形内流式传输索引位置.例如
// This is inside the rectangle class.
public Stream<Point> stream(int hstep, int vstep)
{
// if the rectangle started at 0,0
// and had a width and height of 100
// and hstep and vstep were both 1
// then the returned stream would be
// 0,0 -> 1,0 -> 2,0 -> ... -> 100,0 -> 0,1 -> 1,1 -> ...
// If vstep was 5 and h step was 25 it would return
// 0,0 -> 25,0 -> 50,0 -> 75,0 -> 100,0 -> 0,5 -> 25,5 -> ...
return ...
}
我已经使用了很多IntStream,map,filter等,但是这比我尝试过的要复杂得多.我不知道该怎么做.有人可以引导我正确的方向吗?
解决方法:
您可以使用嵌套的IntStream生成每个Point,然后展平结果流:
public Stream<Point> stream(int hstep, int vstep) {
return IntStream.range(0, height / vstep)
.mapToObj(y -> IntStream.range(0, width / hstep)
.mapToObj(x -> new Point(start.x + x * hstep, start.y + y * vstep)))
.flatMap(Function.identity());
}
内容总结
以上是互联网集市为您收集整理的java-通过2D坐标流/ IntRange?全部内容,希望文章能够帮你解决java-通过2D坐标流/ IntRange?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。
来源:【匿名】