获取 Java 中 Pair 类中的两个注释,在编译代码时。

huangapple go评论128阅读模式
英文:

Get two notes in Pair class in java while compiling code

问题

我编写了代码来解释 Java 中的 Pair 类。我得到了我预期的输出,但在编译时我收到了一个注意。

  1. 注意:MyArrayList.java 使用了未经检查或不安全的操作。
  2. 注意:重新编译时请使用 -Xlint:unchecked 以获取详细信息。

我的代码是:

  1. import java.util.ArrayList;
  2. class MyArrayList
  3. {
  4. public static void main(String[] args)
  5. {
  6. Pair<String, Integer> p1 = new Pair("Shubham", 1804017);
  7. Pair<Integer, String> p2 = new Pair(1804025,"Sanket");
  8. Pair<Boolean, String> p3 = new Pair(true,"Sanket");
  9. p1.getInfo();
  10. p2.getInfo();
  11. p3.getInfo();
  12. }
  13. }
  14. class Pair<X,Y>
  15. {
  16. X x;
  17. Y y;
  18. public Pair(X x,Y y)
  19. {
  20. this.x = x;
  21. this.y = y;
  22. }
  23. public void getInfo()
  24. {
  25. System.out.println(x+" "+y);
  26. }
  27. }

那么这个注意是什么意思,我们能避免它吗?

英文:

I write code to clear the doubt about Pair class in java. I got my output what I expected but while compiling I got a note.

  1. Note: MyArrayList.java uses unchecked or unsafe operations.
  2. Note: Recompile with -Xlint:unchecked for details.

My Code is

  1. import java.util.ArrayList;
  2. class MyArrayList
  3. {
  4. public static void main(String[] args)
  5. {
  6. Pair&lt;String, Integer&gt; p1 = new Pair(&quot;Shubham&quot;, 1804017);
  7. Pair&lt;Integer, String&gt; p2 = new Pair(1804025,&quot;Sanket&quot;);
  8. Pair&lt;Boolean, String&gt; p3 = new Pair(true,&quot;Sanket&quot;);
  9. p1.getInfo();
  10. p2.getInfo();
  11. p3.getInfo();
  12. }
  13. }
  14. class Pair&lt;X,Y&gt;
  15. {
  16. X x;
  17. Y y;
  18. public Pair(X x,Y y)
  19. {
  20. this.x = x;
  21. this.y = y;
  22. }
  23. public void getInfo()
  24. {
  25. System.out.println(x+&quot; &quot;+y);
  26. }
  27. }

So What is that note and can we avoid it?

答案1

得分: 1

只需在此处使用钻石操作符。

  1. Pair<String, Integer> p1 = new Pair<>("Shubham", 1804017);
  2. Pair<Integer, String> p2 = new Pair<>(1804025, "Sanket");
  3. Pair<Boolean, String> p3 = new Pair<>(true, "Sanket");

另请参阅 Java 7 中的钻石操作符(<>)有什么用?

英文:

Just use the diamond operator here.

  1. Pair&lt;String, Integer&gt; p1 = new Pair&lt;&gt;(&quot;Shubham&quot;, 1804017);
  2. Pair&lt;Integer, String&gt; p2 = new Pair&lt;&gt;(1804025,&quot;Sanket&quot;);
  3. Pair&lt;Boolean, String&gt; p3 = new Pair&lt;&gt;(true,&quot;Sanket&quot;);

See also What is the point of the diamond operator (<>) in Java 7?

huangapple
  • 本文由 发表于 2020年9月28日 13:50:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64096602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定