gpt4 book ai didi

java - 按名称删除数组并关闭任何空间隙的代码

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

我在尝试在 Planet 类中提出 boolean 方法来按名称删除数组并移动值以消除任何空间隙时遇到问题。有人可以帮忙吗?以下是我的月球和行星类(class)...

月球级:-

public class Moon
{
private float angle=0.01;
// add class member variables here
private String name;
private float radius;
private float distance;
private float speed;
private int orbitalPeriod;


// add constructor here
public Moon(String name, float radius, float distance, float speed, int orbitalPeriod)
{
this.name=name;
this.radius=radius;
this.distance=distance;
this.speed=speed;
this.orbitalPeriod=orbitalPeriod;
}


// add other methods here
public String getName()
{
return name;
}

public void setName(String name)
{
this.name = name;
}

public float getRadius()
{
return radius;
}

public float getDistance()
{
return distance;
}

public float getSpeed()
{
return speed;
}

public float getAngle()
{
return angle;
}

public int getOrbitalPeriod()
{
return orbitalPeriod;
}

public void setOrbitalPeriod(int orbitalPeriod)
{
this.orbitalPeriod = orbitalPeriod;
}

public String toString()
{
return "Moon:"+name+" - (orbit= "+orbitalPeriod+")";
}

// This will display the moon when other code is completed.
public void display()
{
angle=angle+(0.01*speed);
pushMatrix();
rotate(angle);
translate(distance, 0);
fill(149, 149, 149);
ellipse(0, 0, radius*2, radius*2);
popMatrix();
}
}

行星类别:-

public class Planet
{
private float angle=0.01;
// add class member variables here
private float radius, distance, speed;
private String name;
private Moon [] moons = new Moon [5];
private int numOfMoons = 0;


// add constructor here
public Planet(String name, float radius, float distance, float speed)
{
this.name=name;
this.radius=radius;
this.distance=distance;
this.speed=speed;
}

// add other methods here
public String getName()
{
return name;
}

public float getRadius()
{
return radius;
}

public void setRadius(float radius)
{
this.radius = radius;
}

public float getDistance()
{
return distance;
}

public float getSpeed()
{
return speed;
}

public Moon[] getMoons()
{
return moons;
}

public String toString()
{
return "Planet:"+name+" (r= "+radius+ "d= "+distance+") has "+moons.length+" moon(s)";
}

public void printMoons()
{
for (Moon moon : getMoons())
println(moon);
}

public void addMoon(Moon moon)
{
moons[numOfMoons] = moon;
++numOfMoons;
}

public boolean removeMoonByName(String moonName)
{
for (Moon moon : getMoons())
if(moon!=null)
moon.removeMoonByName();

}


// This will display the moon when other code is completed. You don't need to understand this code.
public void display()
{
angle=angle+(0.01*speed);
pushMatrix();
rotate(angle);
translate(distance, 0);
fill(255, 255, 255);
ellipse(0, 0, radius*2, radius*2);

for (Moon moon : getMoons())
if(moon!=null)
moon.display();

popMatrix();
}
}

最佳答案

您的代码看起来卫星数量可能经常变化,因此数组可能不是您想要执行的操作的最佳选择。

考虑使用大小灵活的 ArrayList。这是您想要执行的操作:

private ArrayList<Moon> moons = new ArrayList<>();

public void addMoon(Moon moon){
moons.add(moon);
}

public boolean removeMoonByName(String moonName){
Moon remove;
for(Moon m : moons){
if(m.getName().equals(moonName)){
remove = m;
}
}
if(remove != null) return moons.remove(remove);
return false;
}

请注意,如果您按名称删除月亮,情况会更加复杂,因为您必须遍历列表,获取所需的月亮,然后将其删除。

关于java - 按名称删除数组并关闭任何空间隙的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40042947/

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