将 x 和 y 数组转换为点
Convert x and y array to Point
我有 2 个数组,即 x
和 y
,我希望将它们转换成一个 Point
数组。
这是我试过的方法,但似乎不起作用。
public class Point {
public static void main(String[] args) throws Exception {
Point[] objectPoints;
double x[] = {3,4,5};
double y[] = {4,5,6};
for (int i = 0;i < 2; i++) {
objectPoints[i] = new Point(x[i],y[i]);
}
}
}
我该如何实现?
你还没有说出你认为的错误,但这里有一些明显的问题:
您还没有为 Point
定义构造函数,也没有在 Point
中定义任何实例变量来保存 x 和 y 坐标,并且
for (int i=0;i<2;i++)
应该是
for (int i=0;i<x.length;i++)
您的方向是正确的,但您缺少一些基础知识。
所以,最后您的代码应该如下所示:
class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public static void main(String[] args) throws Exception {
double x[] = {3, 4, 5};
double y[] = {4, 5, 6};
Point[] objectPoints = new Point[x.length];
for (int i = 0; i < 2; i++) {
objectPoints[i] = new Point(x[i], y[i]);
}
}
}
- 你的
Point
类型数组应该有一个大小来为要插入的值创建索引,数组的大小应该是已知的。
- 在传递
double
值时,您只能将 int
值传递给 Point
构造函数。
不要使用 class 名称 Point
,因为您在程序中使用内置 java Point
。
public class ThePoint {
public static void main(String[] args) throws Exception {
double x[] = {3, 4, 5};
double y[] = {4, 5, 6};
Point[] objectPoints = new Point[x.length];
for (int i = 0; i < x.length; i++) {
objectPoints[i] = new Point((int) x[i], (int) y[i]);
}
System.out.println(objectPoints[0].x);
}
}
你的 Point
class 没有构造函数,你的 objectPoints
数组从未初始化,你的循环只从 0 到 1。你想要的是这样的:
public class Point {
private double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public static void main(String[] args) {
Point[] objectPoints = new Point[3];
double x[] = {3,4,5};
double y[] = {4,5,6};
for (int i = 0; i < objectPoints.length; i++) {
objectPoints[i] = new Point(x[i], y[i]);
}
}
}
您需要检查两个数组的长度是否相同。其次,您不能将项目命名为 Point
,因为此名称已被 Point
现有的 class.
使用
public class MyPoint {
public static void main(String[] args) throws Exception {
Point[] objectPoints;
double x[] = {3,4,5};
double y[] = {4,5,6};
if(x.length != y.length)
throw new //MyTypeError
else//the x and y array share the same length
{
objectPoints = new Point[x.length];
for (int i=0; i < x.length; i ++)
{
objectPoints[i] = new Point(x[i],y[i]);
}
}
}
public class Point {
//you forgot variables to hold values
private double x;
private double y;
//you forgot constructor
public Point (double x, double y) {
this.x = x;
this.y = y;
}
public static void main(String[] args) throws Exception {
double x[] = {3, 4, 5};
double y[] = {4, 5, 6};
//just a check that x and y size should be equal
if (x.length != y.length) {
throw new Exception("points x array is not equal to points y array");
}
Point[] objectPoints = new Point[x.length];
for (int i = 0; i < objectPoints.length; i++) {
objectPoints[i] = new Point(x[i], y[i]);
}
}
}
您不应将数组与您需要使用 ArrayList 的输出一起使用。而且我认为你不应该使用 class 名称点,这会给 class
的功能带来一些麻烦
import java.lang.*;
/***********************************/
ArrayList<Point> objectPoints = new ArrayList<Point>();
double x[] = {3,4,5};
double y[] = {4,5,6};
if (!(x.Length == y.Length))
// YOU SHOULD DO THIS EVEN IF YOU'RE SURE THAT THEY ARE THE SAME
for (int i = 0;i < x.Length; i++) {
objectPoints.Add(new Point((int)x[i],(int)y[i]));
}
抱歉,我不记得是添加还是插入,因为我同时使用多种语言:v :v
我有 2 个数组,即 x
和 y
,我希望将它们转换成一个 Point
数组。
这是我试过的方法,但似乎不起作用。
public class Point {
public static void main(String[] args) throws Exception {
Point[] objectPoints;
double x[] = {3,4,5};
double y[] = {4,5,6};
for (int i = 0;i < 2; i++) {
objectPoints[i] = new Point(x[i],y[i]);
}
}
}
我该如何实现?
你还没有说出你认为的错误,但这里有一些明显的问题:
您还没有为 Point
定义构造函数,也没有在 Point
中定义任何实例变量来保存 x 和 y 坐标,并且
for (int i=0;i<2;i++)
应该是
for (int i=0;i<x.length;i++)
您的方向是正确的,但您缺少一些基础知识。
所以,最后您的代码应该如下所示:
class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public static void main(String[] args) throws Exception {
double x[] = {3, 4, 5};
double y[] = {4, 5, 6};
Point[] objectPoints = new Point[x.length];
for (int i = 0; i < 2; i++) {
objectPoints[i] = new Point(x[i], y[i]);
}
}
}
- 你的
Point
类型数组应该有一个大小来为要插入的值创建索引,数组的大小应该是已知的。 - 在传递
double
值时,您只能将int
值传递给Point
构造函数。 不要使用 class 名称
Point
,因为您在程序中使用内置 javaPoint
。public class ThePoint { public static void main(String[] args) throws Exception { double x[] = {3, 4, 5}; double y[] = {4, 5, 6}; Point[] objectPoints = new Point[x.length]; for (int i = 0; i < x.length; i++) { objectPoints[i] = new Point((int) x[i], (int) y[i]); } System.out.println(objectPoints[0].x); } }
你的 Point
class 没有构造函数,你的 objectPoints
数组从未初始化,你的循环只从 0 到 1。你想要的是这样的:
public class Point {
private double x, y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public static void main(String[] args) {
Point[] objectPoints = new Point[3];
double x[] = {3,4,5};
double y[] = {4,5,6};
for (int i = 0; i < objectPoints.length; i++) {
objectPoints[i] = new Point(x[i], y[i]);
}
}
}
您需要检查两个数组的长度是否相同。其次,您不能将项目命名为 Point
,因为此名称已被 Point
现有的 class.
public class MyPoint {
public static void main(String[] args) throws Exception {
Point[] objectPoints;
double x[] = {3,4,5};
double y[] = {4,5,6};
if(x.length != y.length)
throw new //MyTypeError
else//the x and y array share the same length
{
objectPoints = new Point[x.length];
for (int i=0; i < x.length; i ++)
{
objectPoints[i] = new Point(x[i],y[i]);
}
}
}
public class Point {
//you forgot variables to hold values
private double x;
private double y;
//you forgot constructor
public Point (double x, double y) {
this.x = x;
this.y = y;
}
public static void main(String[] args) throws Exception {
double x[] = {3, 4, 5};
double y[] = {4, 5, 6};
//just a check that x and y size should be equal
if (x.length != y.length) {
throw new Exception("points x array is not equal to points y array");
}
Point[] objectPoints = new Point[x.length];
for (int i = 0; i < objectPoints.length; i++) {
objectPoints[i] = new Point(x[i], y[i]);
}
}
}
您不应将数组与您需要使用 ArrayList 的输出一起使用。而且我认为你不应该使用 class 名称点,这会给 class
的功能带来一些麻烦import java.lang.*;
/***********************************/
ArrayList<Point> objectPoints = new ArrayList<Point>();
double x[] = {3,4,5};
double y[] = {4,5,6};
if (!(x.Length == y.Length))
// YOU SHOULD DO THIS EVEN IF YOU'RE SURE THAT THEY ARE THE SAME
for (int i = 0;i < x.Length; i++) {
objectPoints.Add(new Point((int)x[i],(int)y[i]));
}
抱歉,我不记得是添加还是插入,因为我同时使用多种语言:v :v