Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中更改类的字段_Java_List - Fatal编程技术网

在java中更改类的字段

在java中更改类的字段,java,list,Java,List,所以,我仍在学习java和编码,所以解决方案可能很明显,但我就是看不到。 我正在为大学作业写一个关于星星和星座的代码 package com.company; import java.util.*; public class Main { static public class Constellation { public List<Star> constellation; public String nameOfConstellatio

所以,我仍在学习java和编码,所以解决方案可能很明显,但我就是看不到。 我正在为大学作业写一个关于星星和星座的代码

package com.company;
import java.util.*;


public class Main {

    static public class Constellation {
        public List<Star> constellation;
        public String nameOfConstellation;

        public Constellation(List<Star> constellation, String nameOfConstellation) {
            this.constellation = constellation;
            this.nameOfConstellation = nameOfConstellation;
        }

        public List<Star> getConstellation() {
            return constellation;
        }
    }

    static public class Star {

       // private String categoryName;
        private Constellation constellation;
        private String nameOfConstelation;

        public String getCategoryName() {
            int index = constellation.getConstellation().indexOf(this);
            String categoryName;
            return categoryName = GreekLetter.values[index] + " " + this.constellation.nameOfConstellation;
        }


        public void deleteStar(Star x) {
            this.constellation.constellation.remove(x);


        }
    }
    public enum GreekLetter {

        alfa,
        beta,
        gamma,
        delta,
        epsilon,
        dzeta,
        eta;

        static public final GreekLetter[] values = values();
    }

    public static void main(String[] args)
    {
        Star x = new Star();
        List<Star> fishCon = new ArrayList<>();
        Constellation Fish = new Constellation(fishCon, "Fish");
        x.constellation=Fish;
        fishCon.add(x);
        x.getCategoryName();


        Star y = new Star();
        y.constellation=Fish;
        fishCon.add(y);

        y.getCategoryName();
        x.deleteStar(x);


        for (Star w : Fish.constellation)
        {
            System.out.println(w.getCategoryName());
        }

    }
}

package.com公司;
导入java.util.*;
公共班机{
静态公共类星座{
公共列表星座;
组合的公共字符串名称;
公共星座(列表星座、字符串星座名称){
这个星座=星座;
this.nameofconstillation=nameofconstillation;
}
公共列表getConstellation(){
返回星座;
}
}
静态公共类明星{
//私有字符串categoryName;
私人星座;
关联的私有字符串名称;
公共字符串getCategoryName(){
int index=constellation.getConstellation().indexOf(this);
字符串类别名称;
return categoryName=GreekLetter.values[index]+“”+this.constellation.nameOfConstellation;
}
公共星号(星号x){
这个。星座。星座。移除(x);
}
}
公共枚举GreekLetter{
阿尔法,
贝塔,
伽马,
三角洲,
ε,
泽塔,
希腊字母表的第7个字母
静态公共最终GreekLetter[]值=值();
}
公共静态void main(字符串[]args)
{
星x=新星();
List fishCon=new ArrayList();
星座鱼=新星座(fishCon,“鱼”);
x、 星座=鱼;
fishCon.添加(x);
x、 getCategoryName();
星形y=新的星形();
y、 星座=鱼;
fishCon.添加(y);
y、 getCategoryName();
x、 deleteStar(x);
对于(w星:鱼。星座)
{
System.out.println(w.getCategoryName());
}
}
}

我的观点是在删除一个星号后更新字段categoryName。categoryName值是按添加另一个星号的顺序设置的。例如,我有第一颗星-名称将是Alfa+nameofconstration。第二颗星-Beta+关联名称。当我调用方法deleteStar()时,我想更新constration中我的stars的所有categoyName。在deleteStar()中调用方法可能由于setCategoryName中的add()而无法工作。如果有任何提示,我将不胜感激

因为这似乎是个家庭作业,所以我不是在这个答案中发布代码,而是给出一些建议,帮助您创建自己的可行代码:

  • 创建一个名为Constellation的类,该类将星星保存在
    List starList=new ArrayList()中
  • 为星座提供一个
    公共列表getStarList()
    方法
  • 给每颗恒星一个星座场,以容纳包含该恒星的星座
  • 给每颗星一个
    getCategoryName()
    方法,该方法获取星座对象,使用for循环迭代其星列表,直到找到
    这颗星,然后根据列表中该星的索引返回相应的名称
  • 因此,如果一颗星从星表中删除,该星座持有的所有其他恒星的类别名称将自动动态更新
而且

  • 您可以为星座提供一个
    public void deleteStar(Star-Star)
    方法,在该方法中,星座从其starList中删除Star参数
  • 您还可以为Star提供一个
    public void deleteFromConstellation()
    方法,在该方法中,它检查其星座字段、星座,如果不为null,则调用
    Constellation.deleteStar(this)
    然后将星座字段设置为空
  • 去掉
    私有字符串categoryName星型字段。这应该是一个计算字段,这意味着
    公共字符串getCategoryName()
    不返回字段,而是基于代码的字符串(如上所述)
  • 它首先检查恒星的星座场是否为空
  • 然后它在星座的星表中获取恒星的索引(我已经给出了我的星座类a
    public int getIndexOfStar(恒星)
    方法)
  • 然后,它使用GreekLetter类和
    constellation.getName()
    方法创建一个要返回的字符串
完成了

既然您已经了解了这一点,那么这是另一种编码方式:

public class SkyMain {
    public static void main(String[] args) {
        Constellation fish = new Constellation("Fish");

        Star x = new Star();
        Star y = new Star();
        fish.addStar(x);
        fish.addStar(y);
        
        System.out.println("before removing x");
        System.out.println("x category name: " + x.getCategoryName());
        System.out.println("y category name: " + y.getCategoryName());
        System.out.println("fish constellation: " + fish);
        
        fish.removeStar(x);
        System.out.println();

        System.out.println("after removing x");        
        System.out.println("x category name: " + x.getCategoryName());
        System.out.println("y category name: " + y.getCategoryName());
        System.out.println("fish constellation: " + fish);
    }
}
import java.util.ArrayList;
导入java.util.Iterator;
导入java.util.List;
公共类星座实现了Iterable{
私有字符串名称;
private List starList=new ArrayList();
公共星座(字符串名称){
this.name=名称;
}
公共字符串getName(){
返回名称;
}
公共列表getStarList(){
返回星表;
}
公共空间添加星号(星号){
starList.add(star);
恒星星座(本);
}
公共空间移除器(星型){
if(星型列表包含(星型)){
删除(星号);
恒星星座(空);
}
}
公共int getIndexOfStar(星型){
返回starList.indexOf(star);
}
@凌驾
公共迭代器迭代器(){
返回starList.iterator();
}
@凌驾
公共字符串toString(){
返回“星座[name=“+name+”,starList=“+starList+””;
}
}
public enum GreekLetter
{
阿尔法(“阿尔法”,0),
贝塔(“贝塔”,1),
伽马(“伽马”,2),
三角洲(“三角洲”,3),
ε(“ε”,4),
泽塔(“泽塔”,5),
埃塔(“埃塔”,6);
私有字符串名称;
私有整数索引;
私有GreekLetter(字符串名,int索引){
this.name=名称;
这个指数=指数;
}
公共字符串getName(){
返回名称;
}
public int getIndex(){
收益指数;
}
公共静态欢迎器
public class Star {
    private Constellation constellation;
    
    public void setConstellation(Constellation constellation) {
        this.constellation = constellation;
    }
    
    public void removeFromConstellation() {
        if (constellation != null) {
            constellation.removeStar(this);
        }
    }
    
    public String getCategoryName() {
        if (constellation != null) {
            int index = constellation.getIndexOfStar(this);
            return GreekLetter.getGreekLetter(index).getName() + " " + constellation.getName();
        } else {
            return "";
        }
    }
    
    @Override
    public String toString() {
        return getCategoryName();
    }
}
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Constellation implements Iterable<Star> {
    private String name;
    private List<Star> starList = new ArrayList<>();
    
    public Constellation(String name) {
        this.name = name;
    }
    
    public String getName() {
        return name;
    }
    
    public List<Star> getStarList() {
        return starList;
    }
    
    public void addStar(Star star) {
        starList.add(star);
        star.setConstellation(this);
    }
    
    public void removeStar(Star star) {
        if (starList.contains(star)) {
            starList.remove(star);
            star.setConstellation(null);
        }
    }
    
    public int getIndexOfStar(Star star) {
        return starList.indexOf(star);
    }

    @Override
    public Iterator<Star> iterator() {
        return starList.iterator();
    }

    @Override
    public String toString() {
        return "Constellation [name=" + name + ", starList=" + starList + "]";
    }
    
    
}
public enum GreekLetter
{
    ALPHA("alpha", 0),
    BETA("beta", 1),
    GAMMA("gamma", 2),
    DELTA("delta", 3),
    EPSILON("epsilon", 4),
    ZETA("zeta", 5),
    ETA("eta", 6);
    
    private String name;
    private int index;
    
    private GreekLetter(String name, int index) {
        this.name = name;
        this.index = index;
    }
    
    public String getName() {
        return name;
    }
    
    public int getIndex() {
        return index;
    }
    
    public static GreekLetter getGreekLetter(int index) {
        if (index < 0 || index > values().length) {
            throw new IllegalArgumentException("for index " + index);
        } else {
            return values()[index];
        }
    }
}