Home > Back-end >  Java Vector how to find the deleted
Java Vector how to find the deleted

Time:09-26

 
Package com. Me. A service;

The import com. Me. Domain. The Book;
The import com. Me. Utils. InUtil;
The import com. Me. View. BookView;

import java.util.Vector;

Public class BookService {
Private Vector Books=new Vector ();
//private Book Book=new Book (1, "zhou shuren", "" home" ", "Beijing publishing house," the new BigDecimal (30));

/* *
* to get all the books
* @ return
*/
Public Vector getAllBook () {
Return the books;
}

/* *
* add books
* @ param book
*/
Public void same Book (Book) {
Books. The add (book);
}
Public void delectbook (Book Book) {
For (int I=0; i {
If (books. The size ()==0) {


} else {

return;
}


}
Books. Remove (book);

}
Public void SearchBook (Book Book) {
Books. ElementAt (0);
//input parameters. Equal ([index]. GetBookId)

}
}


Remove and elementAT what to use

CodePudding user response:

1, first, define a class student
 package com. Smart. Call; 

import java.util.Arrays;

/* *
* @ author: cuantianhou 2020/4/30
*/

Public class Student {

Public String getName () {
Return the name;
}

Public void elegantly-named setName (String name) {
this.name=name;
}

@ Override
Public String toString () {
Return "Student {+"
"Name='" + name +' \ '+'
'} ';
}

private String name;

@ Override
Public Boolean equals (Object o) {
If (this==o) return true;
If (o==null | | getClass ()!=o.g etClass ()); return false
Student Student=(Student) o;
Return this. The name. The equals (student getName ());
}

@ Override
Public int hashCode () {
Return Arrays. HashCode (new Object [] {name});
}
}


2, the definition of a Vector action class
 package com. Smart. Call; 

import java.util.Vector;

/* *
* @ author: cuantianhou 2020/5/6
*/
Public class VectorTry {

Private Vector Students=new Vector (a);

Public Vector getAll () {
Return students;
}

Public void the add (Student Student) {
Students. The add (student);
}

Public Student searchReturnObject Student (Student) {
Return null if (students. IsEmpty ());
For (Student element: students) {
If (element) equals (student)) {
Return the student;
}
}
return null;
}


Public int searchReturnIndex Student (Student) {
If (students. IsEmpty ()) return - 1;
for(int i=0; i If (students) get (I)) equals (student)) {
return i;
}
}
return -1;
}

Public void deleteByStudent Student (Student) {
If (searchReturnObject (student)!=null) {
Students. Remove (student);
}
}

Public void deleteByIndex Student (Student) {
Int index=searchReturnIndex (student);
If (index!=1) {
Students. RemoveElementAt (index);
}
}

Public void print () {
For (Student Student: students) {
System. The out. Println (student);
}
}
}

3, in defining a test class:
 package com. Smart. Call; 

/* *
* @ author: cuantianhou 2020/5/6
*/
Public class VectorMain {

Public static void main (String [] args) {
VectorTry VectorTry=new VectorTry ();
for(int i=0; i<10; I++) {
Student Student=new Student ();
Student. Elegantly-named setName (String. The valueOf (I));
VectorTry. Add (student);
}
VectorTry. Print ();
Student Student=new Student ();
Student. Elegantly-named setName (" 1 ");
VectorTry. DeleteByIndex (student);
System. The out. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - ");
VectorTry. Print ();
System. The out. Println (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - ");
Student. Elegantly-named setName (" 2 ");
VectorTry. DeleteByStudent (student);
VectorTry. Print ();
}
}



4, the test result
 D: \ jdk1.8.0 _171 \ bin \ Java exe "- javaagent: D: \ IntelliJ IDEA 2018.1.1 \ lib \ idea_rt jar=59612: D: \ IntelliJ IDEA 2018.1.1 \ bin" - Dfile. Encoding=utf-8 - classpath D: \ jdk1.8.0 _171 \ jre \ lib \ charsets jar; D: \ jdk1.8.0 _171 \ jre \ lib \ deploy the jar. D: \ jdk1.8.0 _171 \ jre \ lib \ ext \ access - bridge - 64. The jar. D: \ jdk1.8.0 _171 \ jre \ lib \ ext \ cldrdata jar; D: \ jdk1.8.0 _171 \ jre \ lib \ ext \ DNSNS jar; D: \ jdk1.8.0 _171 \ jre \ lib \ ext \ jaccess jar; D: \ jdk1.8.0 _171 \ jre \ lib \ ext \ JFXRT jar; D: \ jdk1.8.0 _171 \ jre \ lib \ ext \ localedata jar; D: \ jdk1.8.0 _171 \ jre \ lib \ ext \ nashorn jar; D: \ jdk1.8.0 _171 \ jre \ lib \ ext \ sunec jar; D: \ jdk1.8.0 _171 \ jre \ lib \ ext \ sunjce_provider jar; D: \ jdk1.8.0 _171 \ jre \ lib \ ext \ sunmscapi jar; D: \ jdk1.8.0 _171 \ jre \ lib \ ext \ sunpkcs11 jar; D: \ jdk1.8.0 _171 \ jre \ lib \ ext \ zipfs jar; D: \ jdk1.8.0 _171 \ jre \ lib \ javaws jar; D: \ jdk1.8.0 _171 \ jre \ lib \ jce jar; D: \ jdk1.8.0 _171 \ jre \ lib \ JFR jar; D: \ jdk1.8.0 _171 \ jre \ lib \ JFXSWT jar; D: \ jdk1.8.0 _171 \ jre \ lib \ jsse jar; D: \ jdk1.8.0 _171 \ jre \ lib \ management - agent jar; nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull           
  • Related