package com.me.service;
import com.me.domain.Book;
import com.me.utils.InUtil;
import com.me.view.BookView;
import java.util.Vector;
public class BookService {
private Vector<Book> books = new Vector();
// private Book book = new Book(1, "周樹人", "《故鄉》", "北京出版社", new BigDecimal(30));
/**
* 獲取所有的書
* @return
*/
public Vector getAllBook() {
return books;
}
/**
* 添加圖書
* @param book
*/
public void addBook(Book book) {
books.add(book);
}
public void delectbook (Book book) {
for (int i = 0; i < books.size(); i++ )
{
if (books.size() == 0){
}else {
return;
}
}
books.remove(book);
}
public void SearchBook (Book book) {
books.elementAt(0);
//輸入引數.equal([下標].getBookId)
}
}
remove和 elementAT該怎么使用
uj5u.com熱心網友回復:
1、首先定義一個學生類package com.smart.call;
import java.util.Arrays;
/**
* @author : cuantianhou 2020/4/30
*/
public class Student {
public String getName() {
return name;
}
public void 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.getClass()) return false;
Student student = (Student) o;
return this.name.equals(student.getName());
}
@Override
public int hashCode() {
return Arrays.hashCode(new Object[]{name});
}
}
2、 定義一個Vector操作類
package com.smart.call;
import java.util.Vector;
/**
* @author : cuantianhou 2020/5/6
*/
public class VectorTry {
private Vector<Student> students = new Vector<Student>();
public Vector getAll(){
return students;
}
public void add(Student student){
students.add(student);
}
public Student searchReturnObject(Student student){
if(students.isEmpty()) return null;
for(Student element: students){
if(element.equals(student)){
return student;
}
}
return null;
}
public int searchReturnIndex(Student student){
if(students.isEmpty()) return -1;
for(int i = 0; i < students.size(); 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.out.println(student);
}
}
}
3、在定義一個測驗類:
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.setName(String.valueOf(i));
vectorTry.add(student);
}
vectorTry.print();
Student student = new Student();
student.setName("1");
vectorTry.deleteByIndex(student);
System.out.println("------------------------------------");
vectorTry.print();
System.out.println("------------------------------------");
student.setName("2");
vectorTry.deleteByStudent(student);
vectorTry.print();
}
}
4、測驗結果
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.jar;D:\jdk1.8.0_171\jre\lib\ext\access-bridge-64.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;D:\jdk1.8.0_171\jre\lib\plugin.jar;D:\jdk1.8.0_171\jre\lib\resources.jar;D:\jdk1.8.0_171\jre\lib\rt.jar;E:\公司專案架構圖\springmvc-demo-project\target\test-classes;E:\公司專案架構圖\springmvc-demo-project\target\classes;D:\maven\repository\org\springframework\spring-webmvc\5.0.1.RELEASE\spring-webmvc-5.0.1.RELEASE.jar;D:\maven\repository\org\springframework\spring-aop\5.0.1.RELEASE\spring-aop-5.0.1.RELEASE.jar;D:\maven\repository\org\springframework\spring-beans\5.0.1.RELEASE\spring-beans-5.0.1.RELEASE.jar;D:\maven\repository\org\springframework\spring-context\5.0.1.RELEASE\spring-context-5.0.1.RELEASE.jar;D:\maven\repository\org\springframework\spring-core\5.0.1.RELEASE\spring-core-5.0.1.RELEASE.jar;D:\maven\repository\org\springframework\spring-jcl\5.0.1.RELEASE\spring-jcl-5.0.1.RELEASE.jar;D:\maven\repository\org\springframework\spring-expression\5.0.1.RELEASE\spring-expression-5.0.1.RELEASE.jar;D:\maven\repository\org\springframework\spring-web\5.0.1.RELEASE\spring-web-5.0.1.RELEASE.jar;D:\maven\repository\org\springframework\spring-tx\4.3.13.RELEASE\spring-tx-4.3.13.RELEASE.jar;D:\maven\repository\org\projectlombok\lombok\1.16.20\lombok-1.16.20.jar;D:\maven\repository\javax\servlet\servlet-api\2.5\servlet-api-2.5.jar;D:\maven\repository\jstl\jstl\1.2\jstl-1.2.jar com.smart.call.VectorMain
Student{name='0'}
Student{name='1'}
Student{name='2'}
Student{name='3'}
Student{name='4'}
Student{name='5'}
Student{name='6'}
Student{name='7'}
Student{name='8'}
Student{name='9'}
------------------------------------
Student{name='0'}
Student{name='2'}
Student{name='3'}
Student{name='4'}
Student{name='5'}
Student{name='6'}
Student{name='7'}
Student{name='8'}
Student{name='9'}
------------------------------------
Student{name='0'}
Student{name='3'}
Student{name='4'}
Student{name='5'}
Student{name='6'}
Student{name='7'}
Student{name='8'}
Student{name='9'}
Process finished with exit code 0
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/77599.html
標籤:Java相關
