Home > database >  (Java) When trying to return a new subvector of custom Vector class, my method replaces the last ele
(Java) When trying to return a new subvector of custom Vector class, my method replaces the last ele

Time:08-15

I am just learning how to work with classes and objects, and am writing different methods to work with the Vector class I created.

I have written two working methods to add and insert elements, and am now trying to use either of those in a new method, the aim of which is to create a new Vector subvector object, that only contains elements from index a to index b-1, but has the same capacity as the original.

I have tried using both insert and add methods, the insert method seems to break the loop too soon and only returns the first element, and the add method almost returns the correct result, but replaces the last element with zero.

My first approach was to copy and insert elements like you do with arrays, but that returns the same result. Why is my code returning such a result?

(English is not my first language, so most of the names are in Slovene, the most important ones are translated for clarity)

This is what the class looks like:

    public class VektorInt{
    private int[] elementi;
    private int stElementov;

private static final int START_CAPACITY = 10;
//konstanta, lahko jo večkrat uporabimo

public VektorInt(int startCapacity){
    this.elementi = new int[startCapacity];
    this.stElementov = 0;
}
public VektorInt() {
    this(START_CAPACITY);
}

and these are the methods used to manipulate the objects:

public void set(int index, int value){
    if(index > this.elementi.length){
        throw new IndexOutOfBoundsException
    }
    this.elementi[index] = value;
}
public void add(int value){
    this.expandIfNeeded();
    this.elementi[this.stElementov] = value;
    this.stElementov  ;
}
private void expandIfNeeded(){
    if (this.stElementov >= this.elementi.length){
        int[] stariElementi = this.elementi;
        this.elementi = new int[2*stariElementi.length];
        for(int i = 0; i < this.stElementov; i  ){
            this.elementi[i] = stariElementi[i];
        }
        
    }  
}
public void insert(int index, int value){
    this.expandIfNeeded();
    for(int i = this.stElementov - 1; i >= index; i--){
        this.elementi[i   1] = this.elementi[i];
    }
    this.elementi[index] = value;
    this.stElementov  ;
}

and this is the method in question:

public VektorInt subvector(int start, int end){
    VektorInt subvector = new VektorInt(this.elementi.length);
    for(int i = start; i <= (end) - 1; i  ){
        subvector.add(elementi[i])
    }
    subvector.stElementov  ;
    return subvector;
}

which for System.out.println(vektor.subvector(0, 6)) returns [2, 3, 4, 5, 6, 1, 0].

What is the reason for the subvector method stopping after the first element, if I exchange subvector.add(elementi[i]) for subvector.insert(elementi[i])? If I initialize a vector with a big enough start capacity, shouldn't this work as well?

CodePudding user response:

Adding the elements to subvector with subvector.add(elementi[i]); correctly changes the subvectors stElementov.

After that you write subvector.stElementov ; which increases the size of the subvector without actually adding an element.

That means that the actual size of subvector is now one bigger than the number of elements added.

For this extra element the value is whatever the default value in an int[] is - which happens to be zero (0). This is the extra zero that you see when you print out the subvector.

  • Related