vector的add方法_Java Vector add()方法与示例

vector的add方法

向量类的add()方法 (Vector Class add() method)

Syntax:

句法:

    public boolean add(Element ele);
    public boolean add(int indices, Element ele);

  • add() method is available in java.util package.

    add()方法在java.util包中可用。

  • add(Element ele) method is used to add the given element (ele) at the last of this vector.

    add(Element ele)方法用于在此向量的最后添加给定的元素(ele)。

  • add(int indices, Element ele) method is used to add the given element at the given indices.

    add(int index,Element ele)方法用于在给定索引处添加给定元素。

  • These methods may throw an exception at the time of adding an element.

    这些方法在添加元素时可能会引发异常。

    IndexOutOfBoundsException: This exception may throw when the given parameter is not in a range.

    IndexOutOfBoundsException :如果给定参数不在范围内,则可能引发此异常。

  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.

    这些是非静态方法,可通过类对象访问,如果尝试使用类名访问这些方法,则会收到错误消息。

Parameter(s):

参数:

  • In the first case, add(Element ele),

    在第一种情况下, add(Element ele)

    Element ele – represents the element is to be inserted in this vector.

    元素ele –表示要在此向量中插入的元素。

  • In the first case, add(int indices, Element ele),

    在第一种情况下, add(int index,Element ele)

    • int indices – represents the position of inserted element.
    • int索引 –表示插入元素的位置。
    • Element ele – represents the element is to be inserted in this vector.
    • 元素ele –表示要在此向量中插入的元素。

Return value:

返回值:

In the first case, the return type of the method is boolean, it returns true when the given element is to be added successfully otherwise it returns false.

在第一种情况下,该方法的返回类型为boolean ,如果要成功添加给定元素,则返回true,否则返回false。

In the second case, the return type of the method is void, it returns nothing.

在第二种情况下,该方法的返回类型为void ,它什么也不返回。

Example:

例:

// Java program to demonstrate the example 
// of add() method of Vector

import java.util.*;

public class AddOfVector {
    public static void main(String[] args) {
        // Instantiates a vector object     
        Vector < String > v = new Vector < String > (10);

        // By using add() method is to add
        // the elements in vector
        v.add("C");
        v.add("C++");
        v.add("SFDC");
        v.add("JAVA");

        //Display Vector
        System.out.println("v: " + v);

        // By using add(object) method is used
        // to add the given object at the last
        // of this Vector
        v.add("PHP");

        // Display Vector
        System.out.println("v.add(PHP): " + v);

        // By using add(object,indices) method is used
        // to add the given object at the given indices
        // of this Vector
        v.add(2, "COBOL");

        //Display Vector
        System.out.println("v.add(2,COBOL): " + v);
    }
}

Output

输出量

v: [C, C++, SFDC, JAVA]
v.add(PHP): [C, C++, SFDC, JAVA, PHP]
v.add(2,COBOL): [C, C++, COBOL, SFDC, JAVA, PHP]


翻译自: https://www.includehelp.com/java/vector-add-method-with-example.aspx

vector的add方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值