我正在将向量用作函数中的参数...我不知道如何解决这些操作员错误请帮助我解决这些错误
stt.java:8: error: bad operand types for binary operator '*'
product=ar.get(i)*ar.get(j);
^
first type: Object
second type: Object
stt.java:16: error: bad operand types for binary operator '>'
if(ar.get(i)>ar.get(ind1))
^
first type: Object
second type: Object
stt.java:20: error: bad operand types for binary operator '>'
if((ar.get(i)!=ar.get(ind1)) && (ar.get(i)>ar.get(ind2)))
^
first type: Object
second type: Object
stt.java:23: error: bad operand types for binary operator '*'
return ar.get(ind1)*ar.get(ind2);
^
first type: Object
second type: Object
5 errors
代码是:"https://cutpaste.online/notes/3pmwpK3jdOQWtMiWKduo"
如何解决错误?
问题出在这里:
if(product <((ar.get(i))*(ar.get(j))) )
product=ar.get(i)*ar.get(j);
和
if((ar.get(i)!=ar.get(ind1)) && (ar.get(i) > ar.get(ind2)))
ind2=i;
编译器不知道向量的内容类型。在您的方法中,参数类型仅为Vector
,与参数化类型Vector<Integer>
相比,在Java中将其称为“原始”类型。这使编译器假定错误输出中显示的内容为Object
类型。改为使用参数化类型,内容类型为Integer
,由于Java中的基本“自动拆箱”功能,可以在其上进行乘法和比较。