How do I make a comparison between the objects in a vector and an int? : Java
I am trying to use an if condition to compare the objects in a vector, which represent integers, with values of type int, so that I can increment those which appear in the vector according to how many times they appear in the vector. I can't for the life of me get it to compile. Here is the code.
package countjava;
import java.util.*;
public class Count {
public static void main(String[] args) {
Vector vect = new Vector();
int[] amounts = new int[100];
for (int i = 0; i <= 9999; i开发者_StackOverflow社区++) {
VectorIntObject intObject =
new VectorIntObject((int)Math.random() * 100);
vect.add(intObject);
}
Collections.sort(vect);
for (int i =0; 1 < 40; i++) {
System.out.println(vect.get(i));
}
for (int i = 0; i < 10; i++) {
for (int j = 0; j <= 99; j++) {
Object ourObject = vect.get(i);
if (ourObject.getVectorIntValue() == j) {
amounts[j]++;
}
}
}
for (int j = 0; j <= 99; j++) {
System.out.println(amounts[j]);
}
}
}
class VectorIntObject {
int value;
public VectorIntObject(int value) {
this.value = value;
}
public int getVectorIntValue() {
return this.value;
}
}
The comparison should happen in the nested for loops about half way down. Any help would be much appreciated. Thanks.
EDIT: The "unreachable" statement problem is because of this
for (int i =0; 1 < 40; i++)
1 is always less than 40, so this is an infinite loop. You meant:
for (int i = 0; i < 40; i++)
The problem is that your ourObject
variable is of type Object
, so you can't call getVectorIntValue
on it - that's not a method declared in Object
.
You should be using the collection in a generic way. That way the compiler will "know" that the collection contains VectorIntObject
references.
Vector<VectorIntObject> vect = new Vector<VectorIntObject>();
// Code as before...
for (int i = 0; i < 10; i++) {
for (int j = 0; j <= 99; j++) {
// Note type of ourObject here, with no need to cast
VectorIntObject ourObject = vect.get(i);
if (ourObject.getVectorIntValue() == j) {
amounts[j]++;
}
}
}
Separately, I'd recommend using ArrayList
instead of Vector
.
If for some reason you can't use Java 1.5 or higher, so can't use generics, you have to cast in your loop instead:
for (int i = 0; i < 10; i++) {
for (int j = 0; j <= 99; j++) {
VectorIntObject ourObject = (VectorIntObject) vect.get(i);
if (ourObject.getVectorIntValue() == j) {
amounts[j]++;
}
}
}
This basically has the same effect at execution-time as the generic version, but the compiler isn't able to give you as much help because it doesn't know what kind of value will be in the collection.
First of all, this will not compile:
Object ourObject = vect.get(i);
if (ourObject.getVectorIntValue() == j) {
amounts[j]++;
}
As you're returning an Object
from a vector and trying to call getVectorIntValue()
without typecasting it to VectorIntObject
.
If you're using JDK 5 and higher, I suggest using Generics, and assign a type to Vector
Vector<VectorIntObject> vect = new Vector<VectorIntObject>();
Then this will be valid:
VectorIntObject ourObject = vect.get(i);
if (ourObject.getVectorIntValue() == j) {
amounts[j]++;
}
Otherwise, typecasting will do
VectorIntObject ourObject = (VectorIntObject)vect.get(i);
if (ourObject.getVectorIntValue() == j) {
amounts[j]++;
}
精彩评论