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

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

Get two notes in Pair class in java while compiling code

问题

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

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

我的代码是:

import java.util.ArrayList;

class MyArrayList
{
   public static void main(String[] args)
     {
        Pair<String, Integer> p1 = new Pair("Shubham", 1804017);
	Pair<Integer, String> p2 = new Pair(1804025,"Sanket");
	Pair<Boolean, String> p3 = new Pair(true,"Sanket");

	p1.getInfo();
	p2.getInfo();
	p3.getInfo();
     }
}

class Pair<X,Y>
{
    X x;
    Y y;
    
    public Pair(X x,Y y)
      {
         this.x = x;
         this.y = y;
      }
    public void getInfo()
       {
         System.out.println(x+" "+y);  
       }
}

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

英文:

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.

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

My Code is

import java.util.ArrayList;
 
class MyArrayList
{
   public static void main(String[] args)
     {
        Pair&lt;String, Integer&gt; p1 = new Pair(&quot;Shubham&quot;, 1804017);
	Pair&lt;Integer, String&gt; p2 = new Pair(1804025,&quot;Sanket&quot;);
	Pair&lt;Boolean, String&gt; p3 = new Pair(true,&quot;Sanket&quot;);

	p1.getInfo();
	p2.getInfo();
	p3.getInfo();
     }
}

class Pair&lt;X,Y&gt;
{
    X x;
    Y y;
    
    public Pair(X x,Y y)
      {
         this.x = x;
         this.y = y;
      }
    public void getInfo()
       {
         System.out.println(x+&quot; &quot;+y);  
       }
}

So What is that note and can we avoid it?

答案1

得分: 1

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

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

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

英文:

Just use the diamond operator here.

Pair&lt;String, Integer&gt; p1 = new Pair&lt;&gt;(&quot;Shubham&quot;, 1804017);
Pair&lt;Integer, String&gt; p2 = new Pair&lt;&gt;(1804025,&quot;Sanket&quot;);
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:

确定