来自另一个类的数组

时间:2016-11-29 00:01:45

标签: java arrays

我正在研究一种方法:“ public boolean hasModelName(String modelName)”。我试图从另一个类和数组调用方法getModelName()但无法弄清楚如何做到这一点。我在下面发布了这个课程。当我尝试编译它时,“找不到符号 - 方法getModelName()”。那是在兰博基尼类和数组中。该方法是一个foreach循环,您可以在那里看到LamborghiniCarLot类。那有阵列。这个课没有。底部的最后一个方法是我被困*(String hasModelName = lambo.getModelName(); *)。我如何拥有它,我可以以某种方式使用getModelName()或者我必须以另一种方式这样做吗?

public class LamborghiniDealershipGroup
{

    private String dealershipGroupName; //e.g. Taylor's Auto Group


    private ArrayList<LamborghiniCarLot> carLots; //A collection of LamborghiniCarLots

/**
 * Creates carLots ArrayList object
 * Does NOT add any LamborghiniCarLot objects to carLots ArrayList
 */

public LamborghiniDealershipGroup()
{
    carLots = new ArrayList<LamborghiniCarLot>();

}

/**
 * Creates carLots ArrayList object
 * Creates the following objects
 */

public LamborghiniDealershipGroup(String dealershipGroupName)
{
    carLots = new ArrayList<LamborghiniCarLot>();

    //Create the first car lot
    LamborghiniCarLot carlot1 = new LamborghiniCarLot();

    carlot1.setLotName("Jason's vintage lambos.");
    carlot1.addLamborghini(new Lamborghini(1965, "350GT", 365.3, true));
    carlot1.addLamborghini(new Lamborghini(1966, "400GT 2+2", 366.3, false));
    carlot1.addLamborghini(new Lamborghini(1967, "MIURA", 367.3, true));
    carlot1.addLamborghini(new Lamborghini(1968, "espada", 368.3, false));
    carlot1.addLamborghini(new Lamborghini(1969, "islero", 369.3, true));
    carlot1.addLamborghini(new Lamborghini(1970, "jarama", 370.3, false));
    carlot1.addLamborghini(new Lamborghini(1971, "urraco", 371.3, false));
    carlot1.addLamborghini(new Lamborghini(1971, "urraco", 371.3, false));
    carlot1.addLamborghini(new Lamborghini(1973, "countache", 373.3, false)); 
    carlot1.addLamborghini(new Lamborghini(1974, "silhouette", 374.3, false));
    carlot1.addLamborghini(new Lamborghini(1975, "jalpa", 375.3, true));
    carlot1.addLamborghini(new Lamborghini(1976, "silhouette", 476.3, false));
    carlot1.addLamborghini(new Lamborghini(1977, "silhouette", 377.3, true));
    carlot1.addLamborghini(new Lamborghini(1978, "countache", 478.3, false));
    carlot1.addLamborghini(new Lamborghini(1978, "countache", 578.3, false));
    carlot1.addLamborghini(new Lamborghini(1978, "countache", 378.3, true));
    carlot1.addLamborghini(new Lamborghini(1979, "silhouette", 479.3, false));

    //Create the second car lot
    LamborghiniCarLot carlot2 = new LamborghiniCarLot();

    carlot2.setLotName("Taylor's brand new lambos.");

    carlot2.addLamborghini(new Lamborghini(2006, "gallardo", 506.3, true));
    carlot2.addLamborghini(new Lamborghini(2007, "gallardo", 507.3, true));
    carlot2.addLamborghini(new Lamborghini(2008, "reventon", 308.3, false));
    carlot2.addLamborghini(new Lamborghini(2008, "reventon", 508.3, true));
    carlot2.addLamborghini(new Lamborghini(2009, "gallardo", 409.3, true));
    carlot2.addLamborghini(new Lamborghini(2010, "murcielago", 310.3, false));
    carlot2.addLamborghini(new Lamborghini(2011, "aventador", 411.3, false));
    carlot2.addLamborghini(new Lamborghini(2012, "sesto elemento", 512.3, true));

    //Add them to the arraylist
    carLots.add(carlot1);
    carLots.add(carlot2);

}

/**
 * accessor method to get car lots
 * @return car lots
 */ 

public ArrayList<LamborghiniCarLot> getCarLots()
{
    return carLots;

}   

/**
 * mutator method to set car lots
 * @param car lots
 */ 

public void setCarLots(ArrayList<LamborghiniCarLot> setCarLots)
{
    this.carLots = carLots;

}

/**
 * accessor method that returns dealership group name
 * @return dealership group name
 */

public String getDealershipGroupName()
{
    return dealershipGroupName; 

}

/**
 * mutator method that sets dealership group name
 * @param - dealershipGroupName
 */

public void setDealershipGroupName(String dealershipGroupName)
{
    this.dealershipGroupName = dealershipGroupName;
}

/**
 * Adds the carLot provided to the carLots ArrayList
 * Does not add to the carLots ArrayList if carLot is null
 */

public void addCarLot(LamborghiniCarLot carLot)
{
    if(carLot != null)
    {
        carLots.add(carLot);

    } 

}

/**
 * Using a foreach loop, gets the total inventory of car lots
 * Returns 0 if carLots ArrayList is null
 */

public int getTotalInventoryCount()
{
    int totalIC = 0;
    int cars = 0;

    for(LamborghiniCarLot l : carLots){
        if(l.getInventory().equals(getCarLots())){
            l.getInventory().size();
            cars++;
        }
    }
    return cars;

}

/**
 * Returns true if the model name exists in ANY of the car lots
 * Returns false if the model name cannot be found, or if the carLots ArrayList is null
 * Uses a case-insensitive search(e.g. I can pass in "diablo", or "DIABLo", and still
 * find the same, correct model name)
 */

public boolean hasModelName(String modelName)
{

    boolean found = false;
    for(LamborghiniCarLot lambo : carLots)
    {

        String hasModelName = lambo.getModelName();
        if(hasModelName.equalsIgnoreCase(modelName)) 
        {
            found = true;
            break;
        }
    }
        return found;

}

2 个答案:

答案 0 :(得分:0)

从一开始就考虑这一点。你有一个LamborghiniDealershipGroup。该组在其LamborghiniCarLot数组中有多个carLots个对象。每个LamborghiniCarLot对象在某个数组中都有一些Lamboughini个对象。您想获得那些Lamboughini对象的模型名称。因此,首先迭代该组的车辆,并遍历每个车辆的车辆。

public boolean hasModelName(String modelName)
{
    for(LamborghiniCarLot carLot : carLots)
    {
        for(Lamborghini lambo : carLot.getInventory()) //TODO: substitute in the right name
        {
            if(lambo.getModelName().equalsIgnoreCase(modelName)) return true;
        }
    }

    return false;
}

答案 1 :(得分:0)

假设您在private ArrayList<Lamborghini> inventory;课程中定义了LamborghiniCarLot,并且已经定义了一个getter

public ArrayList<Lamborghini> getInventory() {
     return inventory;
}

现在,这是您的问题的解决方案:

public boolean hasModelName(String modelName) {
    for(LamborghiniCarLot carLot : carLots) {
        for(Lamborghini lambo : carLot.getInventory()) {
            if(lambo.getModelName().equalsIgnoreCase(modelName)) {
                return true;
            }
        }
    }
    return false;
}

当然,必须在public String getModelName()类中定义方法Lamborghini