Home >Java >javaTutorial >Java Example - Get the index value of a vector element
The following example demonstrates using the sort() method of the Collections class to sort vectors and using the binarySearch() method to obtain the index value of the vector element:
/* author by w3cschool.cc Main.java */import java.util.Collections;import java.util.Vector;public class Main { public static void main(String[] args) { Vector v = new Vector(); v.add("X"); v.add("M"); v.add("D"); v.add("A"); v.add("O"); Collections.sort(v); System.out.println(v); int index = Collections.binarySearch(v, "D"); System.out.println("元素索引值为 : " + index); }}
The output result of running the above code is:
[A, D, M, O, X] 元素索引值为 : 1
The above is the Java example - Get the content of the index value of the vector element. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!