日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

java indexof_Java Vector indexOf()方法与示例

發(fā)布時(shí)間:2025/3/11 java 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java indexof_Java Vector indexOf()方法与示例 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

java indexof

向量類(lèi)indexOf()方法 (Vector Class indexOf() method)

Syntax:

句法:

public int indexOf(Object ob);public int indexOf(Object ob, int indices);
  • indexOf() method is available in java.util package.

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

  • indexOf(Object ob) method is used to return the index of the first occurrence of the given element.

    indexOf(Object ob)方法用于返回給定元素首次出現(xiàn)的索引。

  • indexOf(Object ob, int indices) method is used to find the index of the first occurrence of the given object in this Vector and searching starts at the given indices.

    indexOf(Object ob,int index)方法用于查找此Vector中給定對(duì)象首次出現(xiàn)的索引,并從給定索引開(kāi)始搜索。

  • These methods may throw an exception at the time of returning an index.

    這些方法在返回索引時(shí)可能會(huì)引發(fā)異常。

    IndexOutOfBoundsException: This exception may throw when the given parameter is not in a range (indices<0).

    IndexOutOfBoundsException :當(dāng)給定參數(shù)不在范圍內(nèi)(索引<0)時(shí),可能引發(fā)此異常。

  • 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.

    這些是非靜態(tài)方法,可通過(guò)類(lèi)對(duì)象訪問(wèn),如果嘗試使用類(lèi)名訪問(wèn)這些方法,則會(huì)收到錯(cuò)誤消息。

Parameter(s):

參數(shù):

  • In the first case, indexOf(Object ob),

    在第一種情況下, indexOf(Object ob) ,

    Object ob – represents the object for which index to be returned.

    對(duì)象ob –代表要為其返回索引的對(duì)象。

  • In the first case, indexOf(Object ob, int indices),

    在第一種情況下, indexOf(Object ob,int index) ,

    • Object ob – represents the index of searching starts.
    • 對(duì)象ob –表示搜索開(kāi)始的索引。
    • int indices – represents the object for which index to be returned.
    • int index –代表要為其返回索引的對(duì)象。

Return value:

返回值:

In both the cases, the return type of the method is int,

在這兩種情況下,方法的返回類(lèi)型均為int 。

  • In the first case, it gets the index of the first occurrence of the given object when it exists otherwise it returns -1.

    在第一種情況下,它在給定對(duì)象存在時(shí)獲取給定對(duì)象首次出現(xiàn)的索引,否則返回-1。

  • In the second case, it returns the index of the first occurrence of the given object when exists.

    在第二種情況下,它返回給定對(duì)象首次出現(xiàn)時(shí)的索引。

Example:

例:

// Java program to demonstrate the example // of indexOf() method of Vectorimport java.util.*;public class IndexOfVector {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 vectorv.add("C");v.add("C++");v.add("SFDC");v.add("JAVA");v.add("C++");//Display VectorSystem.out.println("v: " + v);// By using indexOf(object) method is used// to return the index of first occurrence of the// given objectSystem.out.println("v.indexOf(C++): " + v.indexOf("C++"));// By using indexOf(object, indices) method is used// to return the index of first occurrence of the// given object and searching starts from the // given indicesSystem.out.println("v.indexOf(C++,2): " + v.indexOf("C++", 2));} }

Output

輸出量

v: [C, C++, SFDC, JAVA, C++] v.indexOf(C++): 1 v.indexOf(C++,2): 4

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

java indexof

總結(jié)

以上是生活随笔為你收集整理的java indexof_Java Vector indexOf()方法与示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。