如何保存数组中所有对象的被调用方法的结果?

时间:2016-11-11 19:06:54

标签: java

我正在创建一个处理汽车经销商的程序。用户可以通过创建随机的3位数字来在商店中添加汽车。

现在问题是如何根据3位数代码搜索/删除汽车?

我认为我需要汽车必须将其保存在阵列上的所有代码,以便我可以在之后进行搜索和删除。

我已经创建了一个类和某些方法,我还创建了5个对象,我试图看看它是否适用于这5个。

以下是随机数的方法:

我使用metritis变量,因为我无法在数组上正确放置值,因此我必须给出1,2,3,4,5的参数,以便我可以正确地将它们放到数组中。

package antiprosopeia;
import java.util.Random;

public class Antiprosopeia {

    private String company,colour;
    private int model,horsePower,speed,price,specialCode,metritis;
    private int[] codes = new int[]{0,0,0,0,0}; 

    public Antiprosopeia(String company, String colour, int model, int horsePower, int speed, int price) {
        this.company = company;
        this.colour = colour;
        this.model = model;
        this.horsePower = horsePower;
        this.speed = speed; 
        this.price = price;  
    }

    public Antiprosopeia() {  
        company = "";  colour = "";  model = 0; horsePower = 0; speed = 0;          price = 0;   
    }


    public void setRandomNumber(int metritis) { 
        Random rand = new Random();
        int randNum2 = rand.nextInt(900) + 100;
        specialCode = randNum2;

        codes[metritis] = specialCode;
    }

    public void printarray() {
        for(int i=0; i<codes.length; i++) {
            System.out.println(" " + codes[i]);}
    }

    public void Info() {   
        System.out.println("Company : " + company + "\nColour : " + colour + "\nModel : " + model + "\nHorse Power : " + horsePower +
        "\nSpeed : " + speed + "\nPrice : " + price );   
    }


    public static void main(String[] args) {

        Antiprosopeia car1 = new Antiprosopeia("Toyota","red",333,100,2223,8000);
        car1.setRandomNumber(0); 

        Antiprosopeia car2 = new Antiprosopeia("Mercedes","yellow",233,100,2990,9000);
        car2.setRandomNumber(1);

        Antiprosopeia car3 = new Antiprosopeia("Volkswagen","green",153,100,2780,6000);
        car3.setRandomNumber(2);

        Antiprosopeia car4 = new Antiprosopeia("Mitsubisi","white",678,140,2600,7000);
        car4.setRandomNumber(3);

        Antiprosopeia car5 = new Antiprosopeia("Porsche","black",390,1000,2000,30000);
        car5.setRandomNumber(4);    
    }        
}

[编辑]现在当我调用printarray()方法时,似乎在我的数组中只有一个值被保持而所有其他值都是zer0s,因为我在程序开始时定义了数组

2 个答案:

答案 0 :(得分:0)

如果我这样做,我会使用HashMap。这样您就知道您有一个唯一的3位数字,如果您愿意,您还可以存储更多数据。你可以这样做:

HashMap<Integer, Car> cars = new HashMap<Integer, Car>();

此示例允许您将汽车对象添加到地图中。你没有那个,但它是一个选择。如果您不想这样做,您可以这样做:

HashMap<Integer, String> cars = new HashMap<Integer, String>();

然后执行:

cars.put(123, "Description of car"); 

使用HashMap可以在存储数据时提供更多选项。这也会阻止您创建一个包含1000个元素的数组,所有这些元素都是0,直到您有一个值。您可以通过以下方式轻松打印出所有数字:

for(int number : cars.entrySet()){
    System.out.println("My car number: " + number);
}

正如您所做的那样,搜索密钥非常简单:

String description = cars.getKey(123);

如果description为空,您就会知道没有密钥。

答案 1 :(得分:0)

您的问题是每个Antiprosopeia对象都有自己的 codes数组。它们不是共享的。

如果您确实希望每个对象都有一个随机ID,那么在构造函数中指定它。

public class Antiprosopeia {

    private String company,colour;
    private int model,horsePower,speed,price,specialCode,metritis;
    private int randID;


    public Antiprosopeia(String company, String colour, int model, int horsePower, int speed, int price){
        this.company = company;
        this.colour = colour;
        this.model = model;
        this.horsePower = horsePower;
        this.speed = speed; 
        this.price = price; 
        this.randID = new Random().nextInt(900) + 100;
    }

    public Antiprosopeia(){  
        this("", "", 0, 0, 0, 0);
    }

    public int getID() { return this.randID; }

    @Override
    public String toString() { 
         return String.format(
            "Company : %s\n" + 
            "Colour : %s\n" +
            "Model : %s\n" +
            "Horse Power : %d\n" +
            "Speed : %d\n" +
            "Price : %d\n", 
            company, colour, model, horsePower, speed, price 
        );
    }

如果要打印所有这些对象,

public static void main(String[] args) {

    List<Antiprosopeia> cars = new ArrayList<Antiprosopeia>();

    cars.add(new Antiprosopeia("Toyota","red",333,100,2223,8000));
    cars.add(new Antiprosopeia("Mercedes","yellow",233,100,2990,9000));

    for (int i = 0; i < cars.size(); i++) {
        Antiprosopeia c = cars.get(i);
        System.out.println(c.getID());
        System.out.println(c);
    } 

}