Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无效的数组显示_Java_Arrays_Inheritance_Methods_Interface - Fatal编程技术网

Java 无效的数组显示

Java 无效的数组显示,java,arrays,inheritance,methods,interface,Java,Arrays,Inheritance,Methods,Interface,你好,我有以下任务: 我的问题是第四项的第一项作业。我规定用随机对象填充数组,但是,当我想在屏幕上显示这个数组时,我总是得到不同的结果。 我的代码: 接口本身: interface VehicleAndWorkers { String cars(); } 实现接口的第一个类: public class TaxiStation implements VehicleAndWorkers{ TrucksLogistic trucks = new TrucksLogistic();

你好,我有以下任务:

我的问题是第四项的第一项作业。我规定用随机对象填充数组,但是,当我想在屏幕上显示这个数组时,我总是得到不同的结果。 我的代码:

接口本身:

interface VehicleAndWorkers
{
  String cars();
}
实现接口的第一个类:

   public class TaxiStation implements VehicleAndWorkers{
   TrucksLogistic trucks = new TrucksLogistic();

   public String[] cars = {"KIA", "Hyundai", "Volkswagen", "Lada", "Datsun", "Skoda"};
   int quantCars = 150;
   int workers = 300;

   public String cars() {
   System.out.println("List of models of our taxi fleet: ");
      for (int i = 0; i < cars.length; i++) {
           System.out.println(cars[i]);
       }
           return null;
   }

   @Override
     public boolean equals(Object obj)
     {
       return trucks.quantTrucks == this.quantCars;
     }

    @Override
      public int hashCode() {
         return Objects.hash(trucks, quantCars);
      }

    @Override
      public String toString() {
           return cars();
       }
     }
public class TrucksLogistic implements VehicleAndWorkers {

String[] trucks = {"Kenworth W900", "Volvo FH16", "Scania R730", "MAN TGS 18.400", "МАЗ 6430", 
"КАМАЗ-5490 НЕО 2"};

int quantTrucks = 40;
int workers = 100;

public String cars()
{
    System.out.println("Our company model list: ");

    for (int i = 0; i < trucks.length; i++)
    {
        System.out.println(trucks[i]);
    }
    System.out.println("Amount of workers: " + workers);
    System.out.println("Number of trucks: " + quantTrucks);
    System.out.println("The ratio of the number of truck models to the number of trucks: " + 
      Math.round(trucks.length / quantTrucks));

    return null;
}

@Override
public String toString() {

    return cars();
 }
}
公共类出租车站实施车辆作业{
卡车物流卡车=新卡车物流();
公共字符串[]汽车={“起亚”、“现代”、“大众”、“拉达”、“达特桑”、“斯柯达”};
int-quantCars=150;
国际工人=300;
公共小型车(){
System.out.println(“我们的出租车车队模型列表:”);
对于(int i=0;i
实现接口的第二个类:

   public class TaxiStation implements VehicleAndWorkers{
   TrucksLogistic trucks = new TrucksLogistic();

   public String[] cars = {"KIA", "Hyundai", "Volkswagen", "Lada", "Datsun", "Skoda"};
   int quantCars = 150;
   int workers = 300;

   public String cars() {
   System.out.println("List of models of our taxi fleet: ");
      for (int i = 0; i < cars.length; i++) {
           System.out.println(cars[i]);
       }
           return null;
   }

   @Override
     public boolean equals(Object obj)
     {
       return trucks.quantTrucks == this.quantCars;
     }

    @Override
      public int hashCode() {
         return Objects.hash(trucks, quantCars);
      }

    @Override
      public String toString() {
           return cars();
       }
     }
public class TrucksLogistic implements VehicleAndWorkers {

String[] trucks = {"Kenworth W900", "Volvo FH16", "Scania R730", "MAN TGS 18.400", "МАЗ 6430", 
"КАМАЗ-5490 НЕО 2"};

int quantTrucks = 40;
int workers = 100;

public String cars()
{
    System.out.println("Our company model list: ");

    for (int i = 0; i < trucks.length; i++)
    {
        System.out.println(trucks[i]);
    }
    System.out.println("Amount of workers: " + workers);
    System.out.println("Number of trucks: " + quantTrucks);
    System.out.println("The ratio of the number of truck models to the number of trucks: " + 
      Math.round(trucks.length / quantTrucks));

    return null;
}

@Override
public String toString() {

    return cars();
 }
}
公共级卡车物流实施车辆工人{
字符串[]卡车={“Kenworth W900”、“沃尔沃FH16”、“斯堪尼亚R730”、“曼恩TGS 18.400”、“МА3 6430”,
"КАМАЗ-5490 НЕО 2"};
int-quantTrucks=40;
国际工人=100;
公共小型车()
{
System.out.println(“我们公司的型号列表:”);
对于(int i=0;i<1.length;i++)
{
系统输出打印LN(卡车[i]);
}
System.out.println(“工人数量:+工人”);
System.out.println(“卡车数量:+quantTrucks”);
System.out.println(“卡车型号与卡车数量的比率:”+
数学圆(卡车长度/数量卡车);
返回null;
}
@凌驾
公共字符串toString(){
归还车辆();
}
}
我试图在其中实现使用ArrayList(填充、显示等)的类:

公共类接口阵列{
ArrayList数组=新的ArrayList();
公共void setArray(){
int randomLength=(int)数学地板(random()*10);
for(int i=0;i
那么,应用上一个类中的方法的类:

public class ConsoleInterface {
 void doChoice()
  {
    InterfaceArray interfaceArray = new InterfaceArray();

printOptions();

Scanner scan = new Scanner(System.in);
int choice = scan.nextInt();

while (choice >= 0 && choice < 5) {
    if (choice == 0) {
        interfaceArray.setArray();
        interfaceArray.getArray();
        printOptions();
        choice = scan.nextInt();
    }
    else if (choice == 1) {
        interfaceArray.setArray();
        printOptions();
        choice = scan.nextInt();
    }
    else if (choice == 2) {
        interfaceArray.sameElements();
        printOptions();
        choice = scan.nextInt();
    }
    else if (choice == 3) {
        interfaceArray.sameTypeElements();
        printOptions();
        choice = scan.nextInt();
    }
    else if (choice == 4) {
        interfaceArray.getArray();
        printOptions();
        choice = scan.nextInt();
    }
 }
}

void printOptions()
{
    System.out.println("Choose an action: \n");
    System.out.println("0 - Fill the array with random elements and display it on the screen. \n");
    System.out.println("1 - Fill the array with random elements. \n");
    System.out.println("2 - Find objects in the array,\n" +
            "      whose functional method returns the same result. \n");
    System.out.println("3 - Split the original array into two arrays, \n" +
                    "      which will store the same type of elements. \n");
    System.out.println("4 - Display the array(-s) to the screen.\n");
    System.out.println("Any key - Exit the application.\n");
 }
}
公共类控制台界面{
void docoice()
{
InterfaceArray InterfaceArray=新InterfaceArray();
打印选项();
扫描仪扫描=新扫描仪(System.in);
int choice=scan.nextInt();
而(选项>=0&&choice<5){
如果(选项==0){
interfaceArray.setArray();
interfaceArray.getArray();
打印选项();
choice=scan.nextInt();
}
else if(选项==1)