YouTip LogoYouTip

Java Vector Setelementat

# Java Vector setElementAt() Method [![Image 3: Java Vector](#) Java Vector](#) * * * `setElementAt()` is a method provided by the `Vector` class in Java, used to modify the element at a specified position in the vector. This method allows you to directly update a specific element in the vector without having to delete the old element first and then insert a new one. **Method Description**: public void setElementAt(E obj, int index) ### Parameter Description **obj** * Type: E (generic, consistent with the type defined by Vector) * Description: The new element to be set * Example: Can be any object, such as String, Integer, etc. **index** * Type: int * Description: The position (index) of the element to be modified * Note: Index starts counting from 0 ### Return Value This method has no return value (void type), it simply modifies the element at the specified position in the vector. * * * ## Usage Example ## Example import java.util.Vector; public class VectorExample { public static void main(String[] args){ // Create a Vector containing strings Vector fruits =new Vector(); fruits.add("Apple"); fruits.add("Banana"); fruits.add("Orange"); System.out.println("Vector before modification: "+ fruits); // Use setElementAt() to modify the second element fruits.setElementAt("Grape", 1); System.out.println("Vector after modification: "+
← Java Vector SetsizeJava Vector Removerange β†’