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

泛型和迭代器的java編譯器錯(cuò)誤

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

問題描述

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

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

當(dāng)我編譯時(shí),它不會自動將 Integer 裝箱為模運(yùn)算,我得到

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

第一種類型:整數(shù)

第二種類型:int

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

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

符號:方法 intValue()

位置:類對象

但是,如果我改變了

 public class MyList...

 公共類 MyList

然后錯(cuò)誤消失.

想到發(fā)生了什么?

謝謝.

解決方案

這里,

public class MyList擴(kuò)展 ArrayList<Integer>{//^ 這里

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

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

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

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.

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

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

相關(guān)文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復(fù)項(xiàng)?)
egit plugin for Eclipse(Eclipse 的 egit 插件)
Gitlab cannot open git-upload-pack error(Gitlab 無法打開 git-upload-pack 錯(cuò)誤)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復(fù)調(diào)用失敗來自服務(wù)器的意外響應(yīng):在 Android 工作室中未經(jīng)授權(quán))
How to add GitLab repository in Eclipse?(如何在 Eclipse 中添加 GitLab 存儲庫?)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
主站蜘蛛池模板: 中文字幕精品一区二区三区精品 | 日韩三级在线观看 | 久久久精品 | 精品欧美一区二区三区久久久 | 久久久男人的天堂 | 国产成人免费在线 | 韩日一区二区三区 | 性色在线 | 亚洲精品一区二区三区中文字幕 | 精品九九 | 国产成人精品久久久 | 欧美日韩视频在线 | 精品国产一区二区三区久久久久久 | 色噜噜色综合 | 国产精品视频一区二区三区 | www.国产.com | 日韩精品一区二区三区中文在线 | 久久精品中文 | 日本网站免费观看 | 欧美日韩高清免费 | 成人av网站在线观看 | 日本精品久久 | 中文字幕在线二区 | 91久色| 91精品国产综合久久久久久漫画 | 视频在线h| 久久久观看| 在线观看成人小视频 | 欧美精选一区二区 | 国产精品99久久久久久动医院 | 一级毛片大全免费播放 | 91精品国产综合久久久久久丝袜 | 99热激情| 成人在线播放网站 | av网站免费 | 黄色大片视频 | 亚洲一区二区三区乱码aⅴ 四虎在线视频 | 自拍偷拍亚洲一区 | 国际精品久久 | 亚洲综合字幕 | 在线免费小视频 |