检查ArrayList中对象变量的方法

时间:2015-01-19 20:05:25

标签: java arraylist

我在使用ArrayList对象创建以下两个方法时遇到问题:

  1. existsTextbook():检查给定的教科书是否在目录中。 existTextbook()接受标题和作者并返回true或false。如果教科书在目录中,则为True,否则为false。

  2. deleteTexbook():从目录中删除教科书。 deleteTextbook()接受教科书标题作为参数,并删除教科书(如果存在)。

  3. 搜索Java API时,我能找到的第一个方法最接近的方法是contains方法,但是它将一个对象作为参数,而不是像Text或author那样在Textbook对象中使用String对象。对于将ArrayList的对象作为参数的第二种方法的remove方法也是如此。

    有关如何让方法查看每个Textbook对象字符串标题或作者的任何提示,如果找到匹配则返回true,或删除包含Textbook对象的Textbook对象字符串标题或作者?

    到目前为止,这是我的代码:

    教科书课程

    package Ex1;
    
    import java.text.NumberFormat;
    
    public class Textbook 
    {
    private String category, title, author;
    private int year;
    private double price;
    
    public Textbook (String category, String title, String author, int year,
                     double price)
    {
        this.category = category;
        this.title = title;
        this.author = author;
        this.year = year;
        this.price = price;
    }
    
    public String toString()
    {
        NumberFormat fmt = NumberFormat.getCurrencyInstance();
    
        String description;
    
        description = "Category: " + category + "\n";
        description += "Title: " + title + "\n";
        description += "Author: " + author + "\n";
        description += "Year: " + year + "\n";
        description += "Price: " + fmt.format(price) + "\n" + "\n";
    
        return description;
    }
    
    }
    

    目录类

    package Ex1;
    
    import java.util.ArrayList;
    
    public class Catalogue 
    {
    private ArrayList <Textbook> catalogue;
    
    public Catalogue ()
    {
        catalogue = new ArrayList<Textbook>();
    }
    
    public void addTextbook (Textbook t) 
    {
        catalogue.add(t);
    }
    
    public boolean existTextbook(String title, String author)
    {
    
    }
    
    public void deleteTextbook(String title)
    {   
    
    }
    
    public String toString()
    {
        return catalogue.toString(); 
    }
    }
    

    驱动程序类

    package Ex1;
    
    public class Drivermain 
    {
    public static void main(String[] args) 
    {
        Textbook javaBook = new Textbook ("Computer Science", 
                "Java Software Solutions", "Lewis/Loftus", 2015, 163.45);
    
        Textbook dataBook = new Textbook ("Computer Science", 
                "Data Structures and Algorithm Analysis in Java,", 
                "Mark A. Weiss", 2015, 181.90);
    
        Textbook calcBook = new Textbook ("Mathematics", 
                "Calculus Plus NEW MyMathLab", "Briggs/Cochran/Gillett",
                2015, 236.90);
    
        Textbook osBook = new Textbook ("Computer Science", 
                "Operating Systems: Internals and Design Principles",
                "William Stallings", 2015, 205.70);
    
        Textbook historyBook = new Textbook ("History", 
                "History of the Canadian Peoples: Beginnings to 1867, Vol. 1",
                "Conard/Finkel/Fyson", 2015, 96.90);
    
        Catalogue bookCatalogue = new Catalogue();
    
        bookCatalogue.addTextbook(javaBook);
        bookCatalogue.addTextbook(dataBook);
        bookCatalogue.addTextbook(calcBook);
        bookCatalogue.addTextbook(osBook);
        bookCatalogue.addTextbook(historyBook);
    
        System.out.println(bookCatalogue);
    
    
        bookCatalogue.existTextbook("Java Software Solutions", "Lewis/Loftus");
        bookCatalogue.deleteTextbook("Java Software Solutions");
    
        }
    }
    

2 个答案:

答案 0 :(得分:0)

您可以考虑自己循环遍历目录ArrayList并测试当前对象是否与标题(和作者)匹配,而不是直接使用这些方法。 这可能有点矫枉过正,但你可以使教科书实施Comparable或写一个Comparator

答案 1 :(得分:0)

我认为不是使用集合中的方法,而是你自己想要查看你的Arraylist。

我没有为每个循环使用a(只是for循环),因为对于删除它会导致并发修改异常。

 package Ex1;

import java.util.ArrayList;

public class Catalogue 
{
private ArrayList <Textbook> catalogue;

public Catalogue ()
{
    catalogue = new ArrayList<Textbook>();
}

public void addTextbook (Textbook t) 
{
    catalogue.add(t);
 }

public boolean existTextbook(String title, String author)
{
    for(int i =0; i<catalogue.Size(); i++){
       Textbook t = catalogue.get(i);
       //you'll want getter and setter methods
       if(t.author.equals(author)&&t.title.equals(title))
           return truel
    }
}

public void deleteTextbook(String title)
{   
    for(int i =0; i<catalogue.Size(); i++){
       Textbook t = catalogue.get(i);
       if(t.title.equals(title)){
          catalogue.remove(i);
       }
    }

}

public String toString()
{
    return catalogue.toString(); 
}
}

快乐的编码!如果您有任何问题,请发表评论。