日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java创建集合有的不用泛型_为什么在Java泛型右手边的集合类型没有任何影响?...

發布時間:2025/3/21 java 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java创建集合有的不用泛型_为什么在Java泛型右手边的集合类型没有任何影响?... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用Java的Generics功能我創建了一個List對象,在左側我使用的是原始類型List,在右側我使用的是泛型類型ArrayList.

List myList=new ArrayList();

我在list對象中添加了一個int值.

myList.add(101);

我希望我會得到一些編譯錯誤但是這個程序運行正常.但是如果我使用泛型類型List在左側和右側的原始類型ArrayList并嘗試將int值添加到列表中,我按預期收到編譯錯誤.

List myList=new ArrayList();

myList.add(101);//The method add(int, String) in the type List is not applicable for the arguments (int)

為什么在Java泛型右手邊的集合類型沒有任何影響?為什么Java允許我們在沒有任何影響時這樣做.我正在使用Java 1.6.請解釋.

解決方法:

如果您未在左側提供泛型類型參數,則將List聲明為原始類型.這意味著編譯器不知道在該列表中存儲什么是合法的,并且依賴于程序員來執行適當的檢查和轉換實例.

原始類型還具有在它們出現的類中刪除所有泛型類型信息的效果.

To make sure that potential violations of the typing rules are always

flagged, some accesses to members of a raw type will result in

compile-time unchecked warnings. The rules for compile-time unchecked

warnings when accessing members or constructors of raw types are as

follows:

At an assignment to a field: if the type of the left-hand operand is a

raw type, then a compile-time unchecked warning occurs if erasure

changes the field’s type.

At an invocation of a method or constructor: if the type of the class

or interface to search (§15.12.1) is a raw type, then a compile-time

unchecked warning occurs if erasure changes any of the formal

parameter types of the method or constructor.

No compile-time unchecked warning occurs for a method call when the

formal parameter types do not change under erasure (even if the result

type and/or throws clause changes), for reading from a field, or for a

class instance creation of a raw type.

標簽:java,collections,generics,java-6

來源: https://codeday.me/bug/20190829/1764170.html

總結

以上是生活随笔為你收集整理的java创建集合有的不用泛型_为什么在Java泛型右手边的集合类型没有任何影响?...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。