The use of an unchecked or unsafe operation solution occurs at Java compile time _java

Source: Internet
Author: User

The problem I encountered when I wrote a Java file with EditPlus.

Copy Code code as follows:

Import java.util.*;
Class collection{
public static void Main (string[] args) {
Collection c1=new ArrayList (25);

C1.add (New String ("one"));
C1.add (New String ("two"));
String s= "three";
C1.add (s);
For (iterator I=c1.iterator (); I.hasnext ();)
{

System.out.println (I.next ());
}
}
}

Then look for the following reasons, which are transferred from someone else's.


This problem may occur when you are using jdk1.5 or above when compiling Java source files. (Use an unchecked or unsafe operation; recompile with-xlint:unchecked.) )
The reason is that there are some differences between the creation of the collection class in jdk1.5 and the jdk1.4, mainly the addition of generics in jdk1.5, which means that the data in the collection can be checked. Before jdk1.5, if the parameter type is not specified, the JDK 1.5 compiler reports unchecked warning because it cannot check if the given parameter is required, which does not affect running. As prompted, compilation is a specified parameter to cancel such a warning. or set the type parameters for it.

Copy Code code as follows:

List temp = new ArrayList ();
Temp.add ("1");
Temp.add ("2");

Amended to

Copy Code code as follows:

List <String> temp = new ArrayList <String> ();
Temp.add ("1");
Temp.add ("2");


Then modify the code to

Copy Code code as follows:

Import java.util.*;
Class collection{
public static void Main (string[] args) {
Collection<string> c1=new arraylist<string> (25);

C1.add (New String ("one"));
C1.add (New String ("two"));
String s= "three";
C1.add (s);
For (iterator I=c1.iterator (); I.hasnext ();)
{

System.out.println (I.next ());
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.