gpt4 book ai didi

java - Point2D ArrayList打印Cocoles前面的方法

转载 作者:行者123 更新时间:2023-12-02 09:30:03 25 4
gpt4 key购买 nike

我正在尝试制作一个 Point2D 坐标数组列表,然后向用户打印出位于 y=x 线上方、y=x 线下方和 y=x 线上的坐标。我想我有它,但由于某种原因,每当我打印出我的 Point2D 数组列表时,它都会打印如下:

[Point2D.Float[1.0, 2.0], Point2D.Float[3.0, 4.0]]

如何删除 Point2D.Float?

这是我的代码:

Main.java:

package com.lucas.q1;

import java.util.Scanner;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {

public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Coordinate> coords = new ArrayList<Coordinate>();
ArrayList<Float> xvals = new ArrayList<Float>();
ArrayList<Float> yvals = new ArrayList<Float>();
ArrayList<Point2D> coordTest = new ArrayList<Point2D>();

//ArrayList<Float> sortedList = new ArrayList<Float>();
Scanner in = new Scanner(System.in);
float xinput;
float yinput;
Coordinate x;
Coordinate y;
boolean tst = true;
while (tst)
{
System.out.println("Enter X: ");
xinput = in.nextFloat();
if (xinput == 000)
{
break;
}

System.out.println("Enter Y: ");
yinput = in.nextFloat();
if (yinput == 000)
{
break;
}
Point2D p = new Point2D.Float(xinput, yinput);
coordTest.add(p);
}

CompareCoords c = new CompareCoords(coordTest);
System.out.println(c.printAboveLine());
}
}

CompareCoords.java:


package com.lucas.q1;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class CompareCoords {

ArrayList<Point2D> onLine = new ArrayList<Point2D>();
ArrayList<Point2D> belowLine = new ArrayList<Point2D>();
ArrayList<Point2D> aboveLine = new ArrayList<Point2D>();

public CompareCoords(List<Point2D> coordTest)
{
for (int i = 0; i < coordTest.size(); i++)
{
if (coordTest.get(i).getX() == coordTest.get(i).getY())
{
onLine.add(coordTest.get(i));
}

if (coordTest.get(i).getX() < coordTest.get(i).getY())
{
aboveLine.add(coordTest.get(i));
}

if (coordTest.get(i).getX() > coordTest.get(i).getY())
{
belowLine.add(coordTest.get(i));
}
}
System.out.println(aboveLine.get(0));
}


public ArrayList<Point2D> printAboveLine()
{
return aboveLine;

}
}


最佳答案

作为一种可能的解决方案,您可以实现方法 void printPoints(List<Point2D> points)将 Point2D 对象列表打印到控制台。它可能看起来像这样

void printPoints(List<Point2D> points) {
for(Point2D p:points) {
System.out.println("[" + p.getX() + ", " + p.getY() + "]");
}
}

关于java - Point2D ArrayList打印Cocoles前面的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58065526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com