<转>java编译问题:使用了未经检查或不安全的操作

本文解决在使用Java集合类时出现的未经检查或不安全操作警告问题,并提供代码修正示例。

使用了未经检查或不安全的操作

在本人用editplus写java文件时碰到的问题。 


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());
        }
    }
}
然后查找到以下原因,这是转自别人的。

在编译java源文件时,你使用的是jdk1.5或以上时,可能出现这个问题。(使用了未经检查或不安全的操作;请使用 -Xlint:unchecked 重新编译。) 
原因是jdk1.5里的集合类的创建和jdk1.4里有些区别,主要是jdk1.5里增加了泛型,也就是说可以对集合里的数据进行检查。在jdk1.5以前,如果没有指定参数类型,则 JDK 1.5 编译器由于无法检查 给出的参数是否合乎要求,而报告 unchecked 警告,这并不影响运行。按照提示,编译是指定参数即可取消这样的警告。或者为其制定类型参数。
List temp = new ArrayList ();
  temp.add("1");
  temp.add("2");
修改为
List <String> temp = new ArrayList <String> ();
  temp.add("1");
  temp.add("2");

然后修改代码为

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());
        }
    }
}


Dependency 'org.apache.flink:flink-connector-redis_2.12:1.1.5' not found <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>scala_test</artifactId> <version>1.0-SNAPSHOT</version> <!-- 仓库配置(保留原配置,确保依赖下载速度) --> <repositories> <repository> <id>central</id> <url>https://repo1.maven.org/maven2</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>false</enabled></snapshots> </repository> <repository> <id>aliyun-public</id> <url>https://maven.aliyun.com/repository/public</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>false</enabled></snapshots> </repository> <repository> <id>apache-hbase</id> <url>https://repository.apache.org/content/repositories/releases/</url> </repository> <!-- 新增:Cloudera仓库(解决Flink-Redis连接器下载问题) --> <repository> <id>cloudera-repos</id> <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>false</enabled></snapshots> </repository> <repository> <id>aliyun-spring</id> <url>https://maven.aliyun.com/repository/spring</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>false</enabled></snapshots> </repository> <!-- 新增:Maven中央仓库镜像(双重保险) --> <repository> <id>maven-central-mirror</id> <url>https://repo.maven.apache.org/maven2/</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>false</enabled></snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>aliyun-plugin</id> <url>https://maven.aliyun.com/repository/public</url> <releases><enabled>true</enabled></releases> <snapshots><enabled>false</enabled></snapshots> </pluginRepository> </pluginRepositories> <!-- 版本属性(新增Redis/Flink-HBase/Gson版本,确保兼容性) --> <properties> <scala.version>2.12.12</scala.version> <scala.binary.version>2.12</scala.binary.version> <flink.version>1.14.0</flink.version> <hadoop.version>3.1.3</hadoop.version> <hbase.version>2.4.9</hbase.version> <slf4j.version>1.7.30</slf4j.version> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <shade-plugin.version>3.1.1</shade-plugin.version> <scala-maven-plugin.version>3.2.2</scala-maven-plugin.version> <kafka-clients.version>2.8.1</kafka-clients.version> <commons-collections4.version>4.4</commons-collections4.version> <!-- 新增1:Flink-Redis连接器版本(适配Flink 1.14.0) --> <flink-redis.version>1.1.5</flink-redis.version> <!-- 新增2:Flink-HBase连接器版本(适配HBase 2.x + Flink 1.14.0) --> <flink-hbase.version>1.14.0</flink-hbase.version> <!-- 新增3:Gson版本(代码中解析JSON用,与Flink兼容) --> <gson.version>2.8.9</gson.version> <!-- 新增4:Redis客户端版本(适配Flink-Redis) --> <jedis.version>2.9.3</jedis.version> </properties> <dependencies> <!-- 1. Scala 基础依赖 --> <dependency> <groupId>org.scala-lang</groupId> <artifactId>scala-library</artifactId> <version>${scala.version}</version> <scope>compile</scope> </dependency> <!-- 2. Flink 核心依赖(集群提供,设为provided) --> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-streaming-scala_${scala.binary.version}</artifactId> <version>${flink.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-clients_${scala.binary.version}</artifactId> <version>${flink.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-core</artifactId> <version>${flink.version}</version> <scope>provided</scope> </dependency> <!-- 3. Flink 连接器基础包 --> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-connector-base</artifactId> <version>${flink.version}</version> <scope>compile</scope> </dependency> <!-- 4. Flink-Kafka 连接器(排除自带kafka-clients) --> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-connector-kafka_${scala.binary.version}</artifactId> <version>${flink.version}</version> <scope>compile</scope> <exclusions> <exclusion> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients</artifactId> </exclusion> <exclusion> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> </exclusion> </exclusions> </dependency> <!-- 5. Kafka 客户端 --> <dependency> <groupId>org.apache.kafka</groupId> <artifactId>kafka-clients</artifactId> <version>${kafka-clients.version}</version> <scope>provided</scope> </dependency> <!-- 6. commons-collections4(解决版本冲突) --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>${commons-collections4.version}</version> <scope>compile</scope> </dependency> <!-- 7. Jackson JSON 解析 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.7</version> <scope>compile</scope> </dependency> <!-- 8. 日志依赖 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-to-slf4j</artifactId> <version>2.14.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.lz4</groupId> <artifactId>lz4-java</artifactId> <version>1.8.0</version> <scope>compile</scope> </dependency> <!-- 9. Hadoop 依赖 --> <dependency> <groupId>org.apache.hadoop</groupId> <artifactId>hadoop-client</artifactId> <version>${hadoop.version}</version> <scope>provided</scope> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> </exclusion> </exclusions> </dependency> <!-- 10. HBase 依赖 --> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> <version>${hbase.version}</version> <scope>compile</scope> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>log4j</groupId> <artifactId>log4j</artifactId> </exclusion> <exclusion> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.hbase</groupId> <artifactId>hbase-common</artifactId> <version>${hbase.version}</version> <scope>compile</scope> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> <exclusion> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> </exclusion> </exclusions> </dependency> <!-- 新增1:Flink-HBase 连接器(UV/PV写入HBase关键依赖) --> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-connector-hbase-2.2_${scala.binary.version}</artifactId> <version>${flink-hbase.version}</version> <scope>compile</scope> <exclusions> <exclusion> <groupId>org.apache.hbase</groupId> <artifactId>hbase-client</artifactId> </exclusion> <exclusion> <groupId>org.apache.hbase</groupId> <artifactId>hbase-common</artifactId> </exclusion> </exclusions> </dependency> <!-- 新增2:Flink-Redis 连接器(GMV写入Redis关键依赖) --> <dependency> <groupId>org.apache.flink</groupId> <artifactId>flink-connector-redis_${scala.binary.version}</artifactId> <version>${flink-redis.version}</version> <scope>compile</scope> </dependency> <!-- 新增3:Redis客户端(适配Flink-Redis) --> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>${jedis.version}</version> <scope>compile</scope> </dependency> <!-- 新增4:Gson(代码中解析JSON数据,样题建议) --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>${gson.version}</version> <scope>compile</scope> </dependency> </dependencies> <build> <sourceDirectory>src/main/scala</sourceDirectory> <testSourceDirectory>src/test/scala</testSourceDirectory> <plugins> <!-- Scala 编译插件 --> <plugin> <groupId>net.alchim31.maven</groupId> <artifactId>scala-maven-plugin</artifactId> <version>${scala-maven-plugin.version}</version> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> <configuration> <args> <arg>-nobootcp</arg> <arg>-target:jvm-1.8</arg> </args> </configuration> </execution> </executions> </plugin> <!-- Java 编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>8</source> <target>8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- 打包插件:Shade(修正主类配置) --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>${shade-plugin.version}</version> <executions> <execution> <phase>package</phase> <goals><goal>shade</goal></goals> <configuration> <artifactSet> <excludes> <exclude>org.apache.flink:*</exclude> <exclude>org.apache.hadoop:*</exclude> <exclude>org.apache.kafka:kafka-clients</exclude> </excludes> </artifactSet> <filters> <filter> <artifact>*:*</artifact> <excludes> <exclude>META-INF/versions/**</exclude> <exclude>module-info.class</exclude> <exclude>META-INF/*.SF</exclude> <exclude>META-INF/*.DSA</exclude> <exclude>META-INF/*.RSA</exclude> </excludes> </filter> </filters> <transformers> <!-- 修正:主类需与实时指标计算代码的主类一致(后续替换为实际主类) --> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <!-- 示例:若UV/PV主类是 flink.UVPVMain,GMV主类是 flink.GMVMain,需分别打包指定一个入口 --> <mainClass>flink.UVPVMain</mainClass> </transformer> <transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/> </transformers> </configuration> </execution> </executions> </plugin> <!-- 源码目录识别 --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <id>add-source</id> <phase>generate-sources</phase> <goals><goal>add-source</goal></goals> <configuration> <sources><source>src/main/scala</source></sources> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>这是pom文件和报错
最新发布
09-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值