如何在循环中创建对象的ArrayList(字符串汽车模型,整数计数,字符串百分比)?

时间:2018-09-05 13:34:17

标签: java object for-loop arraylist hashmap

所以我需要创建一个名为Car的对象,例如 -弦车模型 -整数计数 -字符串百分比

获取此数据后,如何将所有Car对象放入对象的for循环中以获取所有Car对象的Arraylist?

3 个答案:

答案 0 :(得分:0)

您可以这样做:

List<Car> carList = new ArrayList();
for(int i = 0; i < numberOfDesiredCars; i++){
   Car c = new Car(arguments);
   carList.add(c);
}

答案 1 :(得分:0)

我不明白问题是什么?您能具体一点吗?您是说要将所有对象都放入列表中吗?如果是这样,您可以单独添加一个,也可以

std::vector<std::vector<float>> CnnClassifier::evaluateNet(std::vector<float> flattenedImage)
{
    CNTK::FunctionPtr classifier = CNTK::Softmax(m_cnn); // m_cnn is of type CNTK::FunctionPtr

    // Get input variable. The model has only one single input.
    CNTK::Variable inputVar = m_cnn->Arguments()[0];

    // The model has only one output.
    // If the model has more than one output, use modelFunc->Outputs to get the list of output variables.
    CNTK::Variable outputVar = m_cnn->Output();

    CNTK::ValuePtr inputVal = CNTK::Value::CreateBatch(inputVar.Shape(), flattenedImage, m_device);

    std::unordered_map<CNTK::Variable, CNTK::ValuePtr> inputDataMap = { { inputVar, inputVal } };
    std::unordered_map<CNTK::Variable, CNTK::ValuePtr> outputDataMap = { { outputVar, nullptr } };

    classifier->Evaluate(inputDataMap, outputDataMap, m_device);

    CNTK::ValuePtr outputVal = outputDataMap[outputVar];
    std::vector<std::vector<float>> outputData;
    outputVal->CopyVariableValueTo(outputVar, outputData);

    return outputData;
}

我不知道您的意思是在那之后将它们全部放入for循环中。 for循环的作用是什么?您是否只想打印所有这些?如果是这样,就不需要for循环来打印整个列表,就像您只想打印满足特定条件的列表一样。

答案 2 :(得分:0)

您需要为我们提供一些代码,以直接为您提供帮助,但是按照您所说的做,通常最好有两个单独的文件。

在一个文件中:

//Car.java
public class Car{
    private String model;  //instantaneous variables 
    private int count;
    private String percentage;

public Car(){   //default constructor
    model = "whateverModelName";
    count = 123;
    percentage = "75";
}

public Car(model,count,percentage){  //custom Constructor with parameters
}

然后在另一个文件中:CarRunner.java:

// carRunner.java

public class carRunner{
    public int numOfObjects = 0;
    public static void main(String[] args){

        //Creates Car objects and initializes object name to "car1", "car2", etc.
        //Add code that adds 1 to numOfObjects each time an object is created
        getArrayList();  //calls getArrayList method


    }
    public static void getArrayList(){
    //creates and initializes your arrays to the proper sizes
    String[] model = new String[numOfObjects];
    String[] percentage = new String[numOfObjects];
    int[] count = new int[numOfObjects];

    for (i=1;i<=numOfObjects;i++){
        model[i] = car + i + .model;  //not sure how to do this actually, if someone can help write this to get the different variables from different objects

    }
    for (i=1;i<=numOfObjects;i++){
        percentage[i] = car + i + .percentage;  //not sure how to do this actually, if someone can help write this to get the different variables from different objects

    }
    for (i=1;i<=numOfObjects;i++){
        count[i] = car + i + .count;  //not sure how to do this actually, if someone can help write this to get the different variables from different objects

    }

}