Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.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_Class_Object - Fatal编程技术网

从主服务器访问Java类

从主服务器访问Java类,java,class,object,Java,Class,Object,我似乎无法理解如何引用类/类对象数组的特定部分 车辆的类是用get/set定义的,所以我没有包含代码 public class mainTest { public static void main(String[] args) throws Exception { Scanner KB = new Scanner(System.in); String VehiclesFile = "Vehicles.txt"; File file1 =

我似乎无法理解如何引用类/类对象数组的特定部分 车辆的类是用get/set定义的,所以我没有包含代码

public class mainTest {
    public static void main(String[] args) throws Exception {

        Scanner KB = new Scanner(System.in);
        String VehiclesFile = "Vehicles.txt";

        File file1 = new File(VehiclesFile);
        Scanner infile1 = new Scanner(VehiclesFile);
        Vehicle[] Vehicles = new Vehicle[0];

        try {
            Scanner scanner = new Scanner(file1);
            int lineCount = 0;
            while (scanner.hasNextLine()) {
                lineCount++;
                scanner.nextLine();
            }

            Vehicles = new Vehicle[lineCount];

            scanner = new Scanner(file1);
            int VehicleCount = 0;
            while (scanner.hasNextLine()) {
                String[] temp1 = scanner.nextLine().split(",");

                // file has been read into temp1[] now to use Vehicles
                // class type

                Vehicles[VehicleCount] = new Vehicle();
                Vehicles[VehicleCount].setregistration(temp1[0]);
                Vehicles[VehicleCount].setmake(temp1[1]);
                Vehicles[VehicleCount].setModel(temp1[2]);
                Vehicles[VehicleCount].setyear(temp1[3]);
                Vehicles[VehicleCount].setodometer(temp1[4]);
                Vehicles[VehicleCount].setowner(temp1[5]);
                VehicleCount++;

            }
        } catch (IOException e) {
            // Print out the exception that occurred
            System.out.println("Unable to find ");
        }

//*******This is where I need to access the class to print****************
        System.out.println (Vehicle.class.getClasses());

    }
}
确保vehicle类已重写toString()方法。否则它只会打印出引用

见:

如果要打印车辆对象的数据,必须循环该数组并调用前面提到的getter方法。 应该是这样的

System.out.println(Arrays.toString(Vehicles));

在我看来,你把类和对象的概念搞混了。类是分类的缩写,所以类是某种东西的一种类型。对象是类的单个实例。所以它是某种类型的单个项目

您拥有的是一个对象数组,而不是类,您希望打印每个对象的信息。假设阵列中有五辆车,则必须调用函数
System.out.println(/*data to print*/)
五次。数组中的每个元素一个

要省略重复,可以使用循环:

for(Vehicle v : Vehicles)
{
     System.out.print(v.getYear() + " " + v.getMake() + " " + v.getModel());
}
for(int index=0;index
您是想告诉我们您想要打印Vehicles数组的内容吗?谢谢,我仍在努力理解如何正确使用类,在阅读面向对象的概念时可能会感到困惑。您的代码导致打印空值。我的代码写入Vehicles数组的方式可能有问题吗?它是打印
null
s和正确数据的混合,还是打印一些数据,然后再打印所有
null
s?如果是第一种情况,则可能是您的
setX()
方法没有正确实现,或者您正在读取和拆分的字符串有问题。在第二种情况下,这是因为数组大于存储在其中的项目数量。假设您有一个大小为10的阵列,但只有5辆车。您将得到5辆车,并打印出5辆
null
s。如果您使用示例输出更新您的问题,我可以为您提供更多详细信息。所有实例打印空值(35行空值)。数组大小由lineCount设置,lineCount在文件中每一新行的内嵌和lineCount++之后声明。当我使用System.out.println(Arrays.toString(temp1))时;车辆之前[车辆计数]=新车()。。。所有的[]都得到了。。。。它可以很好地打印,但是我需要在while(scanner.hasNextLine())关闭后访问并打印“}”我不是100%确定,但我唯一能想到的是第二个while循环
scanner.hasNextLine()
可能会给您一个
false
,这会导致数组中的每个元素保持全部
null
s
for (int index = 0; index < Vehicles.length; ++i) {
    System.out.println(Vehicle[index].getMake());
    // do the same to print other attributes of the Vehicle class
}