Home > Java > javaTutorial > body text

Java Example - Get the index value of a vector element

黄舟
Release: 2017-01-22 16:25:17
Original
1652 people have browsed it

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);
   }}
Copy after login

The output result of running the above code is:

[A, D, M, O, X]
元素索引值为 : 1
Copy after login

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



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template