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

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

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

Disclosure: My content contains affiliate links.

43,239 questions

56,142 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

4 answers 280 views
280 views asked Oct 23, 2025 by avibootz
2 answers 148 views
4 answers 716 views
1 answer 218 views
3 answers 355 views
355 views asked Jun 28, 2017 by avibootz
3 answers 171 views
1 answer 127 views
...