好的,抱歉不清楚,这是我的完整代码。
我正在尝试创建一个for循环(靠近底部)以删除向量列表中所有奇数索引的所有元素。在代码行上,'if(vector.get(i)%2 == 1);' 我收到错误'二进制%的错误操作数类型,首先在NetBeans中键入java.lang.Object第二类型int'。我没有找到任何专门处理'%'操作数的问题。谢谢。
import java.util.*;
import java.util.Vector;
public class Lab1
{
public static void test(String[] args)
{
Vector<Object> vector = new Vector<Object>();
int primitiveInt = 241;
Integer wrapperInt = new Integer(1234);
String str = "<Your Name>";
vector.add(primitiveInt);
vector.add(wrapperInt);
vector.add(str);
vector.add(2, new Integer(2138));
System.out.println("The elements of vector: " + vector); System.out.println("The size of vector is: "
+ vector.size());
System.out.println("The elements at position 2 is: "
+ vector.elementAt(2));
System.out.println("The first element of vector is: "
+ vector.firstElement());
System.out.println("The last element of vector is: "
+ vector.lastElement());
vector.removeElementAt(1);
System.out.println("The elements of vector: " + vector); System.out.println("The size of vector is: "
+ vector.size());
System.out.println("The elements at position 2 is: "
+ vector.elementAt(2));
System.out.println("The first element of vector is: "
+ vector.firstElement());
System.out.println("The last element of vector is: "
+ vector.lastElement());
vector.clear();
System.out.println("All numbers: ");
for (int i = 0; i < args.length; ++i)
{
vector.add(Integer.parseInt(args[i]));
System.out.println(vector.get(i));
}
System.out.println("Even Numbers: ");
for (int i = vector.size() - 1; i > 0; --i)
{
System.out.println(vector.get(i));
if (vector.get(i) % 2 == 1);
{
vector.removeElementAt(i);
}
}
for (int i = vector.size() - 1; i > 0; --i)
{
System.out.println(vector.get(i));
}
}
}
答案 0 :(得分:0)
不应使用类型声明Vector对象。这曾经是早期java的默认类型,但是现在你可以添加自己的类型。它应该是你的代码的外观类型。因此,您知道向量中的所有对象都是整数类型。你似乎在那里添加字符串和整数,这不是一个好习惯。