Home >Java >javaTutorial >Java Example - Get the index value of a vector element

Java Example - Get the index value of a vector element

黄舟
黄舟Original
2017-01-22 16:25:171683browse

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)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn