久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

泛型和迭代器的java編譯器錯誤

java compiler error with generics and an iterator(泛型和迭代器的java編譯器錯誤)
本文介紹了泛型和迭代器的java編譯器錯誤的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有以下代碼(是的,我知道迭代器的實現不正確.這是我正在寫的一道考試題):

public class MyList擴展 ArrayList<Integer>{公共無效noOdds(){MyIterator<整數>iter = this.iterator();而(iter.hasNext()){如果 (iter.next() % 2 == 1)iter.remove();}}public MyIterator<Integer>迭代器(){return new MyIterator<Integer>(this);}公共類 MyIterator<Integer>實現迭代器<整數>{列表<整數>數據;整數大小;整數位置;MyIterator(列表<整數>數據){this.data = 數據;this.size = data.size();this.position = 0;}公共布爾 hasNext() {if (this.position < this.size)返回真;別的返回假;}公共整數下一個(){整數 tmp = this.data.get(this.position);this.position++;返回 tmp;}公共無效刪除(){如果(this.position == 0)throw new IllegalStateException("下一個還沒有被調用");this.data.remove(this.position - 1);}}}

當我編譯時,它不會自動將 Integer 裝箱為模運算,我得到

MyList.java:9:錯誤:二元運算符%"的操作數類型錯誤if (iter.next() % 2 == 1)

第一種類型:整數

第二種類型:int

如果我將 iter.next() 更改為 iter.next().intValue(),我會得到

MyList.java:9:錯誤:找不到符號if (iter.next().intValue() % 2 == 1)

符號:方法 intValue()

位置:類對象

但是,如果我改變了

 public class MyList...

 公共類 MyList

然后錯誤消失.

想到發生了什么?

謝謝.

解決方案

這里,

public class MyList擴展 ArrayList<Integer>{//^ 這里

您正在聲明一個類型變量 Integer,它會影響 java.lang.Integer 類型.

在類型主體中您引用 Integer 的任何位置,您指的是類型變量而不是 java.lang.Integer 類型.

各種數值運算符不適用于隨機類型(這是您的類型變量),它們僅適用于原始數值類型及其包裝類.因此,您不能將它們與類型變量類型的操作數一起使用.

I have the following code (Yes, I know the iterator is implemented incorrectly. This is an exam question I'm writing):

public class MyList<Integer> extends ArrayList<Integer> {
  public void noOdds() {
    MyIterator<Integer> iter = this.iterator();
    while (iter.hasNext()) { 
      if (iter.next() % 2 == 1)
        iter.remove();
    }   
  } 

  public MyIterator<Integer> iterator() {
    return new MyIterator<Integer>(this);
  } 

  public class MyIterator<Integer> implements Iterator<Integer> {
    List<Integer> data;
    int size;
    int position;

    MyIterator(List<Integer> data) {
      this.data = data;
      this.size = data.size();
      this.position = 0;
    } 

    public boolean hasNext() {
      if (this.position < this.size)
        return true;
      else
        return false;
    }   

    public Integer next() {
      Integer tmp = this.data.get(this.position);
      this.position++;
      return tmp;
    }

    public void remove() {
      if (this.position == 0)
        throw new IllegalStateException("next hasn't been called yet");
      this.data.remove(this.position - 1);
    }
  }
}

When I compile, it won't auto box the Integer for modulo op, and I get

MyList.java:9: error: bad operand types for binary operator '%' if (iter.next() % 2 == 1)

first type: Integer

second type: int

If I change iter.next() to iter.next().intValue(), I get

MyList.java:9: error: cannot find symbol if (iter.next().intValue() % 2 == 1)

symbol: method intValue()

location: class Object

However, if I change

 public class MyList<Integer>...

to

 public class MyList

then the errors go away.

Thoughts on what's going on?

Thanks.

解決方案

Here,

public class MyList<Integer> extends ArrayList<Integer> {
                //  ^ here

you are declaring a type variable Integer which shadows the java.lang.Integer type.

Anywhere within the type body where you refer to Integer, you are referring to the type variable rather than the java.lang.Integer type.

The various numerical operators do not apply to random types (which is what your type variable is), they only work with primitive numeric types and their wrapper classes. Therefore you can't use them with operands of the type of your type variable.

這篇關于泛型和迭代器的java編譯器錯誤的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
egit plugin for Eclipse(Eclipse 的 egit 插件)
Gitlab cannot open git-upload-pack error(Gitlab 無法打開 git-upload-pack 錯誤)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
How to add GitLab repository in Eclipse?(如何在 Eclipse 中添加 GitLab 存儲庫?)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
主站蜘蛛池模板: caoporn国产精品免费公开 | 免费国产一区 | 91久久国产综合久久 | 久久黄色精品视频 | 成人九区| 国产乱码精品一品二品 | 夜夜干夜夜操 | 国产久 | 人人干人人草 | 麻豆a级片 | tube国产 | 超碰最新在线 | 免费观看av | 高清欧美性猛交xxxx黑人猛交 | 国产成人精品一区二区三区网站观看 | 日韩欧美在线免费观看视频 | 久久免费精彩视频 | 日韩中文字幕免费在线 | 国产午夜av片 | 欧美一区精品 | www.成人.com| 中文在线亚洲 | 成人国产午夜在线观看 | 欧美高清免费 | 欧美一级黄带 | 精品1区2区3区4区 | 青青久久 | 国产精品日韩在线 | 中文字幕不卡在线88 | 国产精品久久久久久一区二区三区 | 亚洲成人黄色 | 成人免费毛片在线观看 | 国产www成人 | 精品网站999www | 成在线人视频免费视频 | 美女久久久久久久 | 亚洲综合三区 | 尤物在线| 国产精品成人在线 | 国产乱码精品一区二区三区忘忧草 | 波霸ol一区二区 |