0

I have code as below

private Vector<Vector<Object>> barCode;

private boolean isBarCode(String temp) {
        for (Vector<Object> link : barCode) {
            if (link.get(0).toString().equals(temp)) {
                return true;
            }
        }
        return false;
    }

I am getting below error.

Caused by: java.lang.ClassCastException: java.util.ArrayList incompatible with java.util.Vector

at the for loop line.

Any input please?

1

2 Answers 2

1
private List<List<Object>> barCode;

private boolean isBarCode(String temp) {
    for (List<Object> link : barCode) {
        if (link.get(0).toString().equals(temp)) {
            return true;
        }
    }
    return false;
}

Use the interface, not the implementation. Vector and ArrayList both implement the List interface, so this allows instances of either (and any other implementation). I would also recommend standardizing on creating ArrayList instances in the rest of your code, except where you're sure you need the features of other implementations.

Sign up to request clarification or add additional context in comments.

Comments

0

Your barCode contains ArrayList instead of Vector.

Someone put wrong container. It is not enough data in your example to find out where and why.

You can replace for (Vector<Object> link : barCode) to for (ArrayList<Object> link : barCode) and Vector<Vector<Object>> barCode to Vector<ArrayList<Object>> barCode, but I strongly suggest you not to do it before your find out why barCode contains ArayList.

It is happened because somewhere in your code raw generics are used (Vector without generic parameters).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.