プロが教えるわが家の防犯対策術!

Javaの配列について質問です。
配列の各要素に別の配列を格納するような配列を作成したいのですが、
上手く行かなくて困っています。
float型の配列circle
   {x座標,y座標,x方向の速さ,y方向の速さ}
を要素に持つcirclesという配列を作りたいと思っています。

例えば
circles[0]には{0,0,10,10}
circles[1]には{3,3,10,10}

circleは1つ1つの円のパラメータ、
circlesにはその円をそれぞれ格納したいと思っています。
下記の様に定義しようとしたのですが、circleが型に解決出来ませんと
エラーが出てしまいます。
どうすれば良いのか解らず、困っています。
どなたか正しい書き方を教えて下さい。
宜しくお願いいたします。
-----------------------------------------------------------
//float型の要素を持つ配列circle
//{x座標,y座標,x方向の速さ,y方向の速さ}
float[] circle;

//各球のオブジェクトを入れておく為の配列circles
//各要素はcircle型
circle circles[];
circles = new circle[10];

//circlesの各要素に各々newしたcircle配列を入れてやる。
circles[0] = new circle[4];
circles[1] = new circle[4];

A 回答 (1件)

配列の配列ならこんな感じ。


//--------------------------------------
class Q7261738a {
public static void main(String[] args) {
float[][] circle = {{0, 0, 10, 10}, {3, 3, 20, 20}};
for (int i = 0; i < circle.length; i++) {
for (int j = 0; j < circle[i].length; j++) {
System.out.print(" " + circle[i][j]);
}
System.out.println();
}
}
}
//--------------------------------------

オブジェクトの配列ならこんな感じ。
//--------------------------------------
class Q7261738b {
public static void main(String[] args) {
Circle[] circles = new Circle[2];
circles[0] = new Circle(0, 0, 10, 10);
circles[1] = new Circle(3, 3, 20, 20);
for (int i = 0; i < circles.length; i++) {
Circle c = circles[i];
System.out.println(c.x + " " + c.y + " " + c.xv + " " + c.yv);
}
}
}
class Circle {
float x, y, xv, yv;
Circle(float x, float y, float xv, float yv) {
this.x = x;
this.y = y;
this.xv = xv;
this.yv = yv;
}
}
//--------------------------------------
    • good
    • 1

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!