将两个ArrayLists作为参数的方法

时间:2015-01-22 17:19:30

标签: java arraylist

我正在完成一个练习,要求我编写一个名为totalTax的方法,该方法将两个ArrayLists作为参数,一个包含房屋,另一个包含商店,并返回所有这些属性的应付税款总额。 Shop和House是我课程中的两个班级。每个都有一个以整数形式返回税的方法。

我的解决方案如下,但由于重复,我不确定它是最有效的方法,并且它不能编译!有没有办法实现同样的事情,但避免重复的代码,如我在下面?

private int totalTax(ArrayList<Shop> list1, ArrayList<House> list2) {

    int total;

    for (int a =0; a <= list1.size() -1; a++) {

        total += list1.shopTax();

    }

    for (int a = 0; a<= list2.size() -1; a++) {

        total += list2.houseTax();

    }

    return total;
}

4 个答案:

答案 0 :(得分:2)

您的解决方案无法正常工作,因为您必须访问List中的元素,您才能直接从列表中检索tax,如下所示:

total += list1.shopTax();

应该是

total += list1.get(a).shopTax();

在此类似:

total += list2.houseTax();

应该是:

total += list2.get(a).houseTax();

但是,这种方法使用您的ArrayList作为数组的包装器。另一种方法是将ArrayList视为List,因此您将使用Iterator而不是通过get方法访问元素。有两种方法可以使用Iterator

  1. 声明Iterator并使用List#iterator进行初始化:

    Iterator<Shop> shopIterator = list1.iterator();
    while (shopIterator.hasNext()) {
        Shop shop = shopIterator.next();
        total += shop.shopTax();
    }
    Iterator<House> houseIterator = list2.iterator();
    while (houseIterator.hasNext()) {
        House house = houseIterator.next();
        total += house.shopTax();
    }
    
  2. 使用增强型for循环:

    for (Shop shop : list1) {
        total += shop.shopTax();
    }
    for (House house : list2) {
        total += house.houseTax();
    }
    

答案 1 :(得分:2)

就DRY代码而言,如果改变类层次结构,House和Shop都可以实现共享接口,则可以做得更好。请考虑以下事项:

public interface Taxable{
    public int getTax();
}

public class House implements Taxable{

    //Other code you've written for House

    public int getTax(){
        return houseTax();
    }
}

public class Shop implements Taxable{

    //Other code you've written for Shop

    public int getTax(){
        return shopTax();
    }
}

从那里你可以获得一个Taxables的ArrayList(包含房屋和商店)并累计税款而不用重复。

private int totalTax(ArrayList<? extends Taxable> lst) {
    int total;
    for (Taxable t : lst) {
        total += t.getTax();
    }
    return total;
}

如果你绝对必须将两个ArrayLists作为参数,你仍然不能重复自己创建这个方法,依赖上面的方法作为它的帮助。

private int totalTax(ArrayList<House> list1, ArrayList<Shop> list2){
    return totalTax(list1) + totalTax(list2)
}

答案 2 :(得分:0)

回答代码未编译的具体问题

见下面的评论:

class Shop {
    int tax;
    int shopTax() { return tax; }
}
class House {
    int tax;
    int houseTax() { return tax; }
}
private int totalTax(ArrayList<Shop> list1, ArrayList<House> list2) {
    // Need to initialize total to 0
    int total = 0;

    for (int a =0; a <= list1.size() -1; a++) {
        // Need to get the element from the list first
        Shop shop = list1.get(a);
        total += shop.shopTax();
    }

    for (int a = 0; a<= list2.size() -1; a++) {
        // Need to get the element from the list first
        House house = list2.get(a);
        total += house.houseTax();
    }
    return total;
}

回答“有没有办法实现同样的目的,但避免重复的代码,例如我在下面?”

interface Taxable {
    double getTax();
}

class Shop implements Taxable {
    public double getTax() {
        return 0d;
    }
}

class House implements Taxable {
    public double getTax() {
        return 0d;
    }
}

public double totalTax(List<Taxable> thingsToTax) {
    double total = 0;
    for (Taxable taxable : thingsToTax) {
        total += taxable.getTax();
    }
    return total;
}

<强>更新

我错过了'方法必须采用两个参数'的部分。所以添加这部分:

public double totalTax(List<Taxable> thingsToTax, List<Taxable> otherThingsToTax) {
    return totalTax(thingsToTax) + totalTax(otherThingsToTax);
}

这也使用double代替int,我认为这更合适。否则,如果仍有需要,请随时使用int

答案 3 :(得分:-1)

这样的事情有点效率:

private int totalTax(ArrayList<Shop> list1, ArrayList<House> list2) {

 int total = 0;
 int miSize = Math.min(list1.size(), list2.size());

 for (int a = 0; a < minSize; a++)
    total += list1.get(a).shopTax() + list2.get(a).houseTax();


 if(list1.size() > minSize)
    for (int a = minSize; a < list1.size(); a++)
        total += list1.get(a).shopTax();
 else
    for (int a = minSize; a < list2.size(); a++)
        total += list2.get(a).shopTax();


 return total;
}
相关问题