How to clean a vector?

1

I have a size 5 Java vector to allocate 5 first numbers. I have to empty it when it's full, in order to allocate five more numbers. And keep this routine until all numbers are read!

int x[] = new int[5];

x[0] = 1
x[1] = 2
x[2] = 3
x[3] = 4
x[4] = 5
    
asked by anonymous 24.03.2016 / 22:01

2 answers

6

I can not comment yet. I agree with the statement @Piovezan:

  

"Clear" the vector in this case is not exactly possible, because it is saving integers and not references that could receive null value

But I think the code would look more elegant using the Java Array class:

  

Arrays.fill(int[] a, int val)

     

Assigns the specified value to each element of the ints array.

Arrays.fill(x, 0);

a>

    
24.03.2016 / 23:52
2

"Clear" the vector in this case is not exactly possible because it is saving integers and not references that could receive the value null .

The maximum you can do is to assign a neutral value (eg zero) to all elements of the vector, if that helps in your program, and keep an extra variable as the counter of the last available position to write a value ( or the last position where a value was written, whatever is best for your program.)

To assign zero to all elements you do this:

for (int i = 0; i < x.length; i++) {
    x[i] = 0;
}
    
24.03.2016 / 22:27