I am trying to use the get function in a vector to try to add an element in a vector to an integer. When I do this it gives me an error saying an integer and an object cannot be added together. How can I add a vector element to an integer? Thanks :)
Printable View
I am trying to use the get function in a vector to try to add an element in a vector to an integer. When I do this it gives me an error saying an integer and an object cannot be added together. How can I add a vector element to an integer? Thanks :)
First -- what does your Vector hold? Is it Integer objects? If so, and if you have to use Java 4 or before, you'll have to get the int value of the Integer. If it's something else, you'll still need to translate it into a number before Java will allow you to add it to an int.
The vector contains integers. I am using Eclipse 3.3.2 I am not sure if that is what you are looking for. I am trying to get an integer that is in the vector at position x, then add it to another integer.
The problem may be that the Vector holds Integer objects and you want to add it to a primitive int. If the Vector is not a generic Vector, in other words not declared Vector<Integer>, you will need to do the conversion yourself. Something like this may work:
Code:int vectorInt = ((Integer)myVector.get(i)).intValue(); // cast object returned from Vector to Integer and call intValue() to convert to int primitive
int sum = vectorInt + otherInt;
That worked. Thanks. One more quick question. If I have a two classes in different packages how can I access methods in one class from one in a different package.
I believe you must import the class to be used, and the method to be used must be public.
I don't know how to import a class. Could you tell me how to do this?
Thanks a bunch.
I assumed that you already know how to import the Vector class. You'd just do the same for the one you're trying to use. A quick Google search gave me this link: Java: Packages and Import
I think we might be thinking of different things. In my program I have created two packages: cards and participant. I need a method from a class in the cards package, in a class in the participant package. This is unrelated to the vector question I had earlier.
Another way of wording it might be like this.
I have class A in package X and class B in package Y. How can i make an A object in the B class?
Thanks
A anA = new A(); // make an A object
You'll need an import X.A; to get the A class definition. And you'll need the classpath set to the folder containing the X folder.
Nevermind Thanks for the help :)