久久久久久久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 加密,解密文件中有多余的垃圾字符)
主站蜘蛛池模板: 久久99久久久 | 少妇激情视频 | 韩国三级av| 成人网页 | 亚洲午夜18毛片在线看 | 国产在线观看一区 | 欧美精品日韩少妇 | 午夜成人影视 | 成人在线免费观看视频 | 中文av网站| 国产精品毛片久久久久久久 | 亚洲综合视频在线观看 | 欧美视频区 | 国产一级免费观看 | 亚洲欧美视频在线观看 | 国产一级片免费看 | 国产一级片网站 | 免费视频一区二区 | 激情久久网 | 在线一区二区三区 | 午夜精品久久久久久久久久久久 | 日韩精品三区 | 亚洲欧美一区二区三区在线 | 成人在线视频观看 | 亚洲免费成人 | 天天干天天操天天爽 | 国产精品黄色 | 成av人片在线观看www | 99精品免费视频 | 国产在线资源 | 欧美一区二区三区在线观看 | 国产区视频在线观看 | 国产91热爆ts人妖系列 | 欧美国产精品一区二区 | 日韩视频在线观看免费 | 最新国产在线视频 | 91国内精品 | 一本久 | 日韩在线欧美 | 97cao| 不卡视频在线 |