【问题标题】:Iterator returns an Object and not the desired object迭代器返回一个对象而不是所需的对象
【发布时间】:2013-03-21 22:58:11
【问题描述】:
public class Facture {
private Client client = new Client();;
private float Paiement;
private float soustotal;
private float tps;
private float tvq;
private float ttc;
private List<LigneFacture> lignesFac = new ArrayList<LigneFacture>();

public Facture(){
    this.Paiement=0;
    this.soustotal=0;
    this.tps=0;
    this.tvq=0;
    this.ttc=0;

}
public Client getClient() {
    return client;
}

public void setClient(Client client) {
    this.client = client;
}

public float getPaiement() {
    return Paiement;
}

public void setPaiement(float Paiement) {
    this.Paiement = Paiement;
}

public float getSoustotal() {
    return soustotal;
}

public void setSoustotal(float soustotal) {
    this.soustotal = soustotal;
}

public float getTps() {
    return tps;
}

public void setTps(float tps) {
    this.tps = tps;
}

public float getTvq() {
    return tvq;
}

public void setTvq(float tvq) {
    this.tvq = tvq;
}

public float getTtc() {
    return ttc;
}

public void setTtc(float ttc) {
    this.ttc = ttc;
}

public List<LigneFacture> getLignesFac() {
    return lignesFac;
}
public void addLignesFacture(LigneFacture ligneFac){
    this.lignesFac.add(ligneFac);
    Iterator iter_lignesFact = lignesFac.iterator();

    while(iter_lignesFact.hasNext()){
       LigneFacture lignefac_cur =  iter_lignesFact.next();
    }
}

}

嗨,我有这个类,问题出在最后一个方法中,Java 告诉我 iter_lignesFact 返回一个 Object 值而不是 LigneFacture 值,因此他希望我将它转换为 LigneFacture,这是为什么呢?我在 LigneFacture 列表上定义了我的迭代器。

【问题讨论】:

    标签: java object iterator


    【解决方案1】:

    您在这里使用了原始类型:

    Iterator iter_lignesFact = lignesFac.iterator();
    

    你想使用通用形式:

    Iterator<LigneFacture> iter_lignesFact = lignesFac.iterator();
    

    【讨论】:

      【解决方案2】:

      您使用了原始类型,但您可以通过使用 foreach 循环避免完全键入的麻烦和大量代码:

      for (LigneFacture lignefac_cur : lignesFac) {
          // do something with lignefac_cur
      }
      

      如果进行迭代,使用 foreach 循环是一种非常简洁的方式。请注意,尽管使用这种循环进行整个迭代,但您可能不会更改集合。具体来说,没有可用的 iterator.remove() 等价物。但是,如果您的循环中不需要这种操作,则 foreach 是首选语法。

      【讨论】:

        【解决方案3】:

        而且,您根本不想使用Iterator。我们的函数在做什么?

        public void addLignesFacture(LigneFacture ligneFac){
            this.lignesFac.add(ligneFac);
            Iterator iter_lignesFact = lignesFac.iterator();
        
            while(iter_lignesFact.hasNext()){
               LigneFacture lignefac_cur =  iter_lignesFact.next();
            }
        } 
        

        首先,它将ligneFac 添加到列表lignesFacligneFac 现在是列表的最后一个成员,除非出现奇怪的线程情况。然后,创建迭代器,依次为每个成员设置lignefac_cur,在最后一个成员处停止,即ligneFac。那么,为什么不简单地将lignefac_cur 设置为ligneFac?但是,你扔掉了lignefac_cur。我假设您已经缩短了您最初编写的方法。

        public void addLignesFacture(LigneFacture ligneFac){
            this.lignesFac.add(ligneFac);
        
            LigneFacture lignefac_cur =  ligneFac;
            // Do things with lignefac_cur.
            // You might want to make it an instance variable instead,
            // or even to have a method currentLigne() that gets the last
            // member of the list. You might even want to use
            // Stack or Queue as being more expressive.
        } 
        

        【讨论】:

          猜你喜欢
          • 2021-01-11
          • 1970-01-01
          • 2013-12-29
          • 2020-05-06
          • 2014-07-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-04-28
          相关资源
          最近更新 更多