0

I am creating a Vector class for educational purposes. This class has standard getters & setters, and now I want to add support for adding two vectors.

When I try to call result.setVectorValue, I get the error cannot resolve method setVectorValue. How can I overcome my trouble?

Here is the full code of my class:

public class Vector <T1> {
    private T1[] vectorArray;

    public Vector(){
    }

    public Vector(T1[] a){
        this.vectorArray = a;
    }

    public void setVector(T1[] a){
        this.vectorArray = a;
    }

    public void setVectorValue(T1 value, int index){
        this.vectorArray[index] = value;
    }

    public T1[] getVector(){
        return this.vectorArray;
    }

    public T1 getVectorValue(int index){
        return this.vectorArray[index];
    }

    public int getVectorLength(){
        return this.vectorArray.length;
    }

    public String toString() {
        if (vectorArray == null)
            return null;
        return vectorArray.getClass().getName() + " " + vectorArray;
    }

    public T1[] plus(T1[] inputVector, T1[] whatToPlusVector){
        Vector <T1> result = new Vector<T1>();

        int index=0;
        for(T1 element : inputVector){
            result.setVectorValue(element, index);
            index++;
        }
        for(T1 element : whatToPlusVector){
            result.setVectorValue(element, index);
            index++;
        }
        return result;
    }
}
J0e3gan
  • 8,287
  • 9
  • 48
  • 76
dark
  • 157
  • 6
  • 17

3 Answers3

2

Your no-arg constructor (the constructor that takes zero parameters) does not initialize the internal vectorArray. If the no-arg constructor is used, your vectorArray will remain null. In your plus() method you use the no-arg constructor so you can't set any elements of this result vector. You should create it with an initial array having length of:

int length = inputVector.getVectorLength() + whatToPlusVector.getVectorLength();

Since the array has to be of the generic type T1 that's tricky. You can't just write new T1[length].

For generic array creation, see this: How to create a generic array in Java?

So in your plus() method you should do like this:

int length = inputVector.getVectorLength() + whatToPlusVector.getVectorLength();
// You need the class of T1 to be able to create an array of it:
Class<?> clazz = inputVector.getVector().getClass().getComponentType();

T1[] array=(T[])Array.newInstance(clazz, length);
Vector <T1> result = new Vector<>(array);
// And the rest of your plus() method.

And last: your plus() method is declared to return a T1[] so either return result.getVector() or declare it to return Vector<T1> and then you can return the result local variable whose type is Vector<T1>.

Community
  • 1
  • 1
icza
  • 289,344
  • 42
  • 658
  • 630
  • Thanks a lot. But can you explain me, why I cannot call, for example, "inputVector.getVectorLength()" from a plus() method? IDEA doesn't see this method and gives an error "cannot resolve method". From "main" I can call it correctly. – dark Aug 07 '14 at 13:04
  • Your original code has been edited multiple times. I don't know how your code looks like now, so I can't say for sure. – icza Aug 07 '14 at 13:19
1

For the error 'cannot resolve method setVectorValue', may be the class was confused with java.util.Vector.

Change the class name 'Vector' to 'VectorExample' or something, then try again.

Terrence
  • 101
  • 7
0

I expect NullPointerException as vectorArray is not initialized within default constructor and in the plus() method default constructor is called and setVector() is not. So in setVectorValue() you will get NPE.

Also there is an issue with the return type mismatch in plus(). It should be Vector <T1> instead of T1[].

Sample code test the NPE

public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    String[] v1 = {"Str1"};
    String[] v2 = {"Str2"};
    v = v.plus(v1, v2);
    System.out.println(v);
}
Ambrish
  • 3,437
  • 2
  • 22
  • 38