Finding Vector

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Finding Vector

This Java example program shows how to find Vector.

[edit] Java Source Code

  • Package: example.vector
  • File: FindVector2.java
package example.vector;
 
import java.util.Vector;
 
public class FindVector2 {
    static String members[] =
	    { "Ralph", "Waldo", "Emerson", "Henry", "David", "Thoreau", "Walden", "Pond", "Thoreau", "Institute" };
 
    public static void main(String args[]) {
	Vector v = new Vector();
	for (int i = 0, n = members.length; i < n; i++) {
	    v.add(members[i]);
	}
	System.out.println(v);
	System.out.println("Contains Society?: " + v.contains("Society"));
	System.out.println("Contains Waldo?: " + v.contains("Waldo"));
	System.out.println("Where's Waldo?: " + v.indexOf("Waldo"));
	System.out.println("Where's Thoreau?: " + v.indexOf("Thoreau"));
	System.out.println("Where's Thoreau from end?: " + v.lastIndexOf("Thoreau"));
	int index = 0;
	int length = v.size();
	while ((index < length) && (index >= 0)) {
	    index = v.indexOf("Thoreau", index);
	    if (index != -1) {
		System.out.println(v.get(index));
		index++;
	    }
	}
    }
}

[edit] What Result You Can Get

Run the program, you will get:

[Ralph, Waldo, Emerson, Henry, David, Thoreau, Walden, Pond, Thoreau, Institute]
Contains Society?: false
Contains Waldo?: true
Where's Waldo?: 1
Where's Thoreau?: 5
Where's Thoreau from end?: 8
Thoreau
Thoreau

[edit] Required External Libraries and/or Files for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.



[edit] Question & Answer

Any question?

Click edit and post your question or answer here.


Personal tools