使用-Xlint重新编译Java:取消选中以获取详细信息

时间:2015-04-09 04:17:39

标签: java generics

我有大量的代码,我收到了消息

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

2 个答案:

答案 0 :(得分:0)

似乎在您的代码的某些方面,您忘记执行泛型声明,如: GenericSet<T> newSet = new GenericSet(); 它应该是: GenericSet<T> newSet = new GenericSet<T>();或Java7或更高版本: GenericSet<T> newSet = new GenericSet<>();

修复此声明,警告消失。

答案 1 :(得分:0)

您需要替换以下代码:

GenericSet<ExtendedSet<T>> powerSet = new GenericSet();

使用

GenericSet<ExtendedSet<T>> powerSet = new GenericSet<ExtendedSet<T>>();

在你的所有声明中。如果您使用像eclipse这样的IDE,那么带有警告的代码将以黄色加下划线,这样就很容易找到问题。