简体   繁体   English

如何打印ArrayList <Point>

[英]How to print an ArrayList<Point>

Generally ArrayList will print nicely with a primitive type. 通常,ArrayList可以使用基本类型很好地打印。 I need to print out an ArrayList cleanly, like it does with primitive types. 我需要像原始类型一样干净地打印出ArrayList。 Right now it gives me this: 现在,它给了我这个:

[java.awt.Point[x=8,y=8], java.awt.Point[x=7,y=9], java.awt.Point[x=6,y=8] ... [java.awt.Point[x=8,y=8], java.awt.Point[x=7,y=9], java.awt.Point[x=6,y=8] ...

I just need to print it without "java.awt.Point", the details I'm not so worried about. 我只需要在不使用“ java.awt.Point”的情况下进行打印即可,我对此并不担心。 I'm not sure if I can do this with a loop, targeting the index of the arrayList or... 我不确定是否可以针对arrayList的索引或...进行循环

This is the code I have, it makes a "robot" move around a grid: 这是我的代码,它使“机器人”在网格上移动:

ArrayList<Point> moves = new ArrayList<Point>();

// code that moves it one square at a time 
// and then add the move to an array 

moves.add(new Point(this.getX(),this.getY()));  // makes a new point, adds it to the array

And then to print: 然后打印:

System.out.println("Robot 1 moves: "+r1.moves);

I'm totally open to different solutions if anyone has a better idea. 如果有人有更好的主意,我会公开接受不同的解决方案。 Thanks in advance! 提前致谢!

You can use an enhanced for loop ... 您可以使用增强的for loop ...

for (Point p : r1.moves) {
    System.out.println(p.x + "x" + p.y);
}

Take a look at The for Statement (it's towards the bottom) for more details 请查看The for语句 (位于底部)以了解更多详细信息

You'd probably want to either create your own Point object (edit: MadProgrammer fairly chastises me for not pointing out that you should have a bunch of other good reasons to extend or replace a built-in class in addition to just a custom toString() method), in which case you could override the toString() method, or create a helper method that'll create a well-formed string from your collection of Points. 你可能会想创建自己的Point对象(编辑:MadProgrammer相当严厉批评我没有指出你应该有一堆其他的理由来扩展或替换内置类除了刚才自定义的toString( )方法),在这种情况下,您可以覆盖toString()方法,或创建一个帮助程序方法,该方法将从Points集合中创建格式正确的字符串。

For example: 例如:

public string MakeNicePointsString(ArrayList<Point> points)
{
    String pointsString = "";
    for (int i = 0; i < points.length(); i++)
    {
        pointsString += "x=" + points[i].x.toString() + "y=" + points[i].y.toString();
    }

    return pointsString;
}

Java 8 solution: Java 8解决方案:

    moves.stream().forEach((p)-> System.out.print("[" + p.x +"," + p.y + "]"));

Full example: 完整示例:

    List<Point> lst = new ArrayList<Point>();
    lst.add(new java.awt.Point(1,2));
    lst.add(new java.awt.Point(1,8));
    lst.add(new java.awt.Point(7,2));
    lst.add(new java.awt.Point(6,6));
    System.out.print("[");
    lst.stream().forEach((p)-> System.out.print("[" + p.x +"," + p.y + "]"));
    System.out.print("]");

OUTPUT OUTPUT

[[1,2][1,8][7,2][6,6]]

重写Point类的toString方法,以使其打印x&y坐标。

I know it is another kind of extending @MadProgrammer ;) because it is defining an annonymous inner class, but you can define an inline overriding for toString method like this : 我知道这是扩展@MadProgrammer;)的另一种方式,因为它定义了一个匿名内部类,但是您可以像这样定义toString方法的内联重写:

Point p = new Point(10, 20){
     @Override
     public String toString(){
        return (getX() + ", "+ getY()) + "";
     }
};

System.out.println(p); // 10.0, 20.0

this works for this instance only 这仅适用于此实例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM