Java类变量个数

发布于 2024-12-10 09:58:38 字数 275 浏览 0 评论 0原文

我正在学习java,现在我有这个问题。

我创建了一个名为“Driver”的类,它将保存驾驶员的信息(姓名和生日)。

要创建一个新的驱动程序,我只需要执行以下操作:

Driver d1 = new Driver("John", "01/01/1980");

现在假设我有一个程序可以从文件中读取 x 驱动程序信息。如何创建 x 驱动程序?

我的问题是我认为我需要 x 驱动程序的 x 变量,但变量只能由我硬编码......

I'm learning java and now I have this question.

I created a class named "Driver" and it will hold a driver's information (name and birthday).

To create a new driver I just need to do:

Driver d1 = new Driver("John", "01/01/1980");

Now imagine I have a program that will read x drivers information from a file. How can I create x drivers?

My problem is that i'm thinking I need x variables for x drivers but variables can only be hard-coded by me...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你的笑 2024-12-17 09:58:38

或者您可以使用数组或集合(ListSet):

List<Driver> drivers = new ArrayList<Driver>();
drivers.add(new Driver(..));
drivers.add(new Driver(..));

从文件读取时,通常使用循环。因此,在每次迭代时将对象添加到列表中。

Or you can use an array or collection (List or Set):

List<Driver> drivers = new ArrayList<Driver>();
drivers.add(new Driver(..));
drivers.add(new Driver(..));

When reading from a file you usually use a loop. So on each iteration add the object to the list.

相权↑美人 2024-12-17 09:58:38

本质上,您需要的是逐一读取每个驱动程序,并将它们存储在集合中。 Java 中有多个集合类,但 ArrayList 应该适合您的情况:

ArrayList<Driver> drivers = new ArrayList<Driver>();

Driver d1 = new Driver("John", "01/01/1980");
drivers.add(d1); 

What you need, essentially, is to read each driver one by one, and store them in a collection. There are multiple collection classes in Java, but ArrayList should do just fine in your case:

ArrayList<Driver> drivers = new ArrayList<Driver>();

Driver d1 = new Driver("John", "01/01/1980");
drivers.add(d1); 
明媚如初 2024-12-17 09:58:38

您将创建一个List(或其他一些集合,例如Map)来存储所有驱动程序。

you would create a List<Driver> (or some other collection, e.g. Map) in which to store all the Drivers.

捶死心动 2024-12-17 09:58:38

使用简单数组:

Driver[] drivers = {new Driver("John", "01/01/1980"),
                    new Driver("Smith", "02/02/1990")};

// or

Driver[] drivers = new Driver[2];
drivers[0] = new Driver("John", "01/01/1980");
drivers[1] = new Driver("Smith", "02/02/1990");

但是数组一旦创建就有固定的大小。因此,您可以使用 ArrayList 代替:

List<Driver> drivers = new ArrayList<Driver>();
drivers.add(new Driver("John", "01/01/1980"));
drivers.add(new Driver("Smith", "02/02/1990"));
// ...

Use simple array:

Driver[] drivers = {new Driver("John", "01/01/1980"),
                    new Driver("Smith", "02/02/1990")};

// or

Driver[] drivers = new Driver[2];
drivers[0] = new Driver("John", "01/01/1980");
drivers[1] = new Driver("Smith", "02/02/1990");

But array has fixed size once you create it. So, you can use ArrayList instead:

List<Driver> drivers = new ArrayList<Driver>();
drivers.add(new Driver("John", "01/01/1980"));
drivers.add(new Driver("Smith", "02/02/1990"));
// ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文