Java 需要将未指定的数字读入数组
好的,所以我需要根据我在程序的另一部分中指定的数字用整数填充数组。这就是我到目前为止所得到的:
public abstract class Polygon implements Shape {
//numSides is the number of sides in a polygon
int numSides;
//Vertices[] is the array of vertices, listed in counter-clockwise sense
Point[] Vertices;
public Polygon(Point[] Vertices){
//THIS IS WHERE I NEED THE HELP, DONT KNOW WHAT TO PUT IN HERE
}
public Point getPosition(){
return this.Vertices[0];
}
}
提前谢谢您。
抱歉缺少信息。是的,这是一个班级。我可以看出 ArrayList 可能会更好。该程序本身有一个接口 Shape,它由类 Point 和类 Polygon 实现,它们有一个扩展该接口的类,即矩形。这个想法是将顶点数量放入一个数组中,并返回 Vertices[0] 处的值作为多边形的位置。此类的其余部分如下所示:
public abstract class Polygon implements Shape {
//numSides is the number of sides in a polygon
int numSides;
Point[] Vertices;
public Polygon(Point[] Vertices){
//your code
}
public Point getPosition(){
return this.Vertices[0];
}
}
不确定您是否需要查看程序的其余部分。 再次感谢您。
ok so i need to fill an array with integers based on a number that i specify in another part of the program. This is what I have so far:
public abstract class Polygon implements Shape {
//numSides is the number of sides in a polygon
int numSides;
//Vertices[] is the array of vertices, listed in counter-clockwise sense
Point[] Vertices;
public Polygon(Point[] Vertices){
//THIS IS WHERE I NEED THE HELP, DONT KNOW WHAT TO PUT IN HERE
}
public Point getPosition(){
return this.Vertices[0];
}
}
Thank you in advanced.
Sorry for the lack of information. Yes this is for a class. I can see how an ArrayList would probably be better. The program its self has an interface, Shape, which gets implemented by a class Point and a class Polygon, which them has a class that extends that, rectangle. The idea is to put the number of vertices into an array and return the value at Vertices[0] as the position of the polygon. The rest of this class looks like this:
public abstract class Polygon implements Shape {
//numSides is the number of sides in a polygon
int numSides;
Point[] Vertices;
public Polygon(Point[] Vertices){
//your code
}
public Point getPosition(){
return this.Vertices[0];
}
}
Not sure if you need to see the rest of the program.
thank you again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您不太清楚您需要什么,但我想您需要有关构造函数的帮助:
You are not being very clear in what you need, but I guess you want help on your constructor:
也许这会引导您走向正确的方向:
正如其他人所说,问题中的更多细节将为答案带来更详细的信息。而且,这闻起来像家庭作业。如果是这样,您应该这样标记它。
Maybe this will guide you in the right direction:
As the others have said, more detail in the question will bring better detail in answers. Also, this smells like homework. If so, you should tag it as such.
由于您的问题不清楚,我假设您只想用随机整数填充数组。
首先,您需要指定数组的大小,而执行此操作的最佳位置是您的构造函数。
我希望这会有所帮助:)
干杯!!!
As your question is not clear I am assuming that you that you just want to fill your array with random integers.
Here first of all you need to specify size of the array, and the best place to do this is your constructor.
I hope this helps :)
cheers!!!
我认为,您需要一个优雅的数组副本:
之后,您可以迭代数组:
I think, you need an elegant array copy:
After, you can iterate over your array: