使用-Xlint重新编译Java:未选中以获取详细信息
我有一大堆代码,我明白了使用-Xlint重新编译Java:未选中以获取详细信息,java,generics,Java,Generics,我有一大堆代码,我明白了 Note: GenericSet.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 我真的不想发布所有的代码,但如果你们能告诉我一种编译和发现错误的方法,那就太好了。我知道它必须处理泛型,我只是不知道如何在代码中找到问题发生的地方 import java.util.ArrayList; import java.util.Collect
Note: GenericSet.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
我真的不想发布所有的代码,但如果你们能告诉我一种编译和发现错误的方法,那就太好了。我知道它必须处理泛型,我只是不知道如何在代码中找到问题发生的地方
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class GenericSet<T> implements ExtendedSet<T> {
private ArrayList<T> myList;
public GenericSet() {
myList = new ArrayList<T>(50);
}
public ArrayList<T> getList() {
return this.myList;
}
public void addThis(T item) {
this.myList.add(item);
}
public ExtendedSet<T> intersection(ExtendedSet<T> set) {
ArrayList<T> compareThis = ((GenericSet<T>)set).getList();
GenericSet<T> finalVal = new GenericSet();
for (T item : compareThis) {
if (this.myList.contains(item)) {
finalVal.addThis(item);
}
}
return finalVal;
}
public ExtendedSet<T> difference(ExtendedSet<T> set) {
ArrayList<T> compareThis = ((GenericSet<T>)set).getList();
GenericSet<T> finalVal = new GenericSet();
for (T item : compareThis) {
if (!(this.myList.contains(item))) {
finalVal.addThis(item);
}
}
return finalVal;
}
public ExtendedSet<T> union(ExtendedSet<T> set) {
ArrayList<T> base = this.getList();
ArrayList<T> compareThis = ((GenericSet<T>)set).getList();
GenericSet<T> finalVal = new GenericSet();
for (T item : compareThis) {
if (!(this.myList.contains(item))) {
finalVal.addThis(item);
}
}
for (T item: base) {
finalVal.addThis(item);
}
return finalVal;
}
public ExtendedSet<T> symmetricDifference(ExtendedSet<T> set) {
GenericSet<T> diffSet = new GenericSet();
ArrayList<T> compareThis = ((GenericSet<T>)set).getList();
ArrayList<T> base = this.getList();
for (T item : compareThis) {
if (!(this.myList.contains(item))) {
diffSet.addThis(item);
}
}
for (T item : base ) {
if (!(compareThis.contains(item))) {
diffSet.addThis(item);
}
}
return diffSet;
}
public ExtendedSet<ExtendedSet<T>> powerSet() {
GenericSet<ExtendedSet<T>> powerSet = new GenericSet();
for (int i = 0; i<(this.myList.size()); i++) {
GenericSet<T> newSet = new GenericSet();
for (int k = 0; k < i; k++) {
newSet.addThis(this.myList.get(k));
}
powerSet.addThis(newSet);
}
return powerSet;
}
public ExtendedSet<Tuple<T>> product(ExtendedSet<T> set) {
GenericSet<Tuple<T>> product = new GenericSet();
Object aList = this.myList.clone();
ArrayList<T> bList = ((GenericSet<T>)set).getList();
for (int i = 0; i < this.myList.size(); i++) {
GenericTuple addMe = new GenericTuple(((ArrayList<T>)aList).get(i), bList.get(i));
product.addThis(addMe);
}
return product;
}
public <E> ExtendedSet<E> map(LMap<T, E> map) {
GenericSet<E> finalVal = new GenericSet();
for (T item: this.myList) {
E ans = map.map(item);
finalVal.addThis(ans);
}
return finalVal;
}
public T reduce(LReduce<T> reduce) {
ArrayList<T> run = this.getList();
T end = run.get(0);
for (T item: this.myList) {
if (item != run.get(0)) {
end = reduce.reduce(end, item);
}
}
return end;
}
public ExtendedSet<T> filter(LFilter<T> filter) {
GenericSet<T> finalVal = new GenericSet();
for (T item: this.myList) {
if (filter.filter(item)) {
finalVal.addThis(item);
}
}
return finalVal;
}
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof GenericSet)) {
return false;
}
GenericSet<T> testMe = (GenericSet<T>) o;
boolean ans = true;
ArrayList<T> test = this.getList();
ArrayList<T> compare = testMe.getList();
for (int i = 0; i < test.size(); i++) {
if (test.get(i) != compare.get(i)) {
return false;
}
}
return true;
}
public void clear(){
this.myList.clear();
}
public boolean removeAll(Collection<?> c) {
boolean ans = this.myList.removeAll(c);
return ans;
}
public boolean retainAll(Collection<?> c) {
boolean ans = this.myList.retainAll(c);
return ans;
}
public boolean addAll(Collection<? extends T> c) {
boolean ans = this.myList.addAll(c);
return ans;
}
public boolean containsAll(Collection<?> c) {
boolean ans = this.myList.containsAll(c);
return ans;
}
public int size() {
int ans = this.myList.size();
return ans;
}
public boolean isEmpty() {
boolean ans = this.myList.isEmpty();
return ans;
}
public boolean contains(Object o) {
boolean ans = this.myList.contains(o);
return ans;
}
public Iterator<T> iterator() {
Iterator<T> ans = this.myList.iterator();
return ans;
}
public Object[] toArray() {
Object[] ans = this.myList.toArray();
return ans;
}
public <T> T[] toArray(T[] a) {
T[] ans = this.myList.toArray(a);
return ans;
}
public boolean add(T e) {
boolean ans = this.myList.add(e);
return ans;
}
public boolean remove(Object o) {
boolean ans = this.myList.remove(o);
return ans;
}
public int hashCode() {
int ans = this.myList.hashCode();
return ans;
}
}
import java.util.ArrayList;
导入java.util.Collection;
导入java.util.Iterator;
公共类GenericSet实现了ExtendedSet{
私人ArrayList myList;
公共泛型集(){
myList=新阵列列表(50);
}
公共ArrayList getList(){
返回此.myList;
}
公共无效添加此(T项){
this.myList.add(项目);
}
公共扩展集交叉点(扩展集){
ArrayList compareThis=((GenericSet)set).getList();
GenericSet finalVal=新的GenericSet();
对于(T项:比较此项){
if(this.myList.contains(item)){
最终添加此(项目);
}
}
返回最终值;
}
公共扩展集差异(扩展集){
ArrayList compareThis=((GenericSet)set).getList();
GenericSet finalVal=新的GenericSet();
对于(T项:比较此项){
如果(!(this.myList.contains(item))){
最终添加此(项目);
}
}
返回最终值;
}
公共扩展集并集(扩展集){
ArrayList base=this.getList();
ArrayList compareThis=((GenericSet)set).getList();
GenericSet finalVal=新的GenericSet();
对于(T项:比较此项){
如果(!(this.myList.contains(item))){
最终添加此(项目);
}
}
对于(T项:基础){
最终添加此(项目);
}
返回最终值;
}
公共扩展集对称性差异(扩展集){
GenericSet diffSet=新的GenericSet();
ArrayList compareThis=((GenericSet)set).getList();
ArrayList base=this.getList();
对于(T项:比较此项){
如果(!(this.myList.contains(item))){
diffSet.addThis(项);
}
}
对于(T项:基础){
如果(!(比较此.包含(项))){
diffSet.addThis(项);
}
}
返回扩散集;
}
公共扩展集powerSet(){
GenericSet powerSet=新的GenericSet();
对于(int i=0;i c){
布尔ans=this.myList.retainal(c);
返回ans;
}
公共布尔addAll(集合c){
布尔ans=this.myList.containsAll(c);
返回ans;
}
公共整数大小(){
int ans=this.myList.size();
返回ans;
}
公共布尔值为空(){
布尔ans=this.myList.isEmpty();
返回ans;
}
公共布尔包含(对象o){
布尔ans=this.myList.contains(o);
返回ans;
}
公共迭代器迭代器(){
迭代器ans=this.myList.Iterator();
返回ans;
}
公共对象[]toArray(){
Object[]ans=this.myList.toArray();
返回ans;
}
公共T[]toArray(T[]a){
T[]ans=this.myList.toArray(a);
返回ans;
}
公共布尔加法(TE){
布尔ans=this.myList.add(e);
返回ans;
}
公共布尔删除(对象o){
布尔ans=this.myList.remove(o);
返回ans;
}
公共int hashCode(){
int ans=this.myList.hashCode();
返回ans;
}
}
在代码的某些方面,您似乎忘记了进行一般性声明,如下所示:
GenericSet newSet=newgenericset();
应该是:
GenericSet newSet=newgenericset();
或在Java7或更高版本中:
GenericSet newSet=newgenericset();
修复此声明,警告就会消失。您需要替换以下代码:
GenericSet<ExtendedSet<T>> powerSet = new GenericSet();
GenericSet powerSet=new GenericSet();
与
GenericSet powerSet=new GenericSet();
如果您使用像eclipse这样的IDE,那么带有警告的代码将以黄色下划线,从而使查找问题变得简单
GenericSet<ExtendedSet<T>> powerSet = new GenericSet<ExtendedSet<T>>();