Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,011 questions

51,958 answers

573 users

How to resize a vector in Java

1 Answer

0 votes
import java.util.Vector;

public class ResizeVector {
    public static void main(String[] args) {
        Vector<Integer> vec = new Vector<>();
        vec.add(1);
        vec.add(2);
        vec.add(3);
        vec.add(4);

        // Resize up to 5 elements, filling with zeros
        while (vec.size() < 5) {
            vec.add(88);
        }
        
        System.out.println(vec); 

        // Resize down to 2 elements
        while (vec.size() > 2) {
            vec.remove(vec.size() - 1);
        }

        System.out.println(vec); 
    }
}



/*
run:

[1, 2, 3, 4, 88]
[1, 2]

*/

 



answered Oct 13, 2025 by avibootz

Related questions

1 answer 127 views
3 answers 265 views
265 views asked Jun 28, 2017 by avibootz
4 answers 87 views
87 views asked Oct 23, 2025 by avibootz
2 answers 53 views
4 answers 547 views
3 answers 67 views
1 answer 42 views
...