为指定对象设置值

发布于 2024-12-04 12:51:52 字数 586 浏览 0 评论 0原文

我认为这些都是基础知识。

我创建了 3 个这样的对象:

    for (int j = 1; j < 4; j++) {
        int parkingSlot= 1 + rd.nextInt(3);
        AircraftCarrier ac= new AircraftCarrier (fc, j, parkingSlots, parkingSlots);

    }

基于类 AircraftCarrier(它的构造函数):

public AircraftCarrier (FlightControl fc, int idC, int parkingSlots, int freeParkingSlots) {
    this.kontrolaLotow = fc;
    this.id = idC;
    this.ps = parkingSlots;
    this.fps = freeParkingSlots;        
}

所以我有 3 艘航空母舰,对吧?假设我需要更改 id =2 的运营商的 freeParkingSLots 值。我该怎么做?

I assume those are bacics.

I've created 3 objects like this:

    for (int j = 1; j < 4; j++) {
        int parkingSlot= 1 + rd.nextInt(3);
        AircraftCarrier ac= new AircraftCarrier (fc, j, parkingSlots, parkingSlots);

    }

Based on class AircraftCarrier (it's constructor):

public AircraftCarrier (FlightControl fc, int idC, int parkingSlots, int freeParkingSlots) {
    this.kontrolaLotow = fc;
    this.id = idC;
    this.ps = parkingSlots;
    this.fps = freeParkingSlots;        
}

So i have 3 Aircraft Carriers, right? Let's assume I need to change freeParkingSLots value for a carrier with id =2. How do I do that?

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

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

发布评论

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

评论(4

荒岛晴空 2024-12-11 12:51:52

您创建了三个实例,但由于您没有维护对其中任何一个实例的引用,因此您不再拥有它们。它们已被送往垃圾收集站。

您需要将每个实例存储在某个集合中以供以后访问。

 List<AircraftCarrier> myList = new ArrayList<AircraftCarrier>();
 for (int j = 1; j < 4; j++) {
    int parkingSlot= 1 + rd.nextInt(3);
    AircraftCarrier ac= new AircraftCarrier (fc, j, parkingSlots, parkingSlots);
    myList.add(ac);
}

You created three instances but since you didn't maintain the reference to any of them, you no longer have them. They have been sent to garbage collection.

You need to store each instance in some collection for later access.

 List<AircraftCarrier> myList = new ArrayList<AircraftCarrier>();
 for (int j = 1; j < 4; j++) {
    int parkingSlot= 1 + rd.nextInt(3);
    AircraftCarrier ac= new AircraftCarrier (fc, j, parkingSlots, parkingSlots);
    myList.add(ac);
}
漫漫岁月 2024-12-11 12:51:52

您必须将运营商存储在数组中:

AircraftCarrier[] carriers = new AircreaftCarrier[3];
for (int j = 0; j < carriers.length; j++) {
    int parkingSlot= 1 + rd.nextInt(3);
    AircraftCarrier ac = new AircraftCarrier (fc, j + 1, parkingSlots, parkingSlots);
    carriers[i] = ac;
}

现在您可以访问它们:

carriers[1].fps = 6; // You wanted id=2. Since we count from zero in Java, use 1

You have to store the Carriers in an array:

AircraftCarrier[] carriers = new AircreaftCarrier[3];
for (int j = 0; j < carriers.length; j++) {
    int parkingSlot= 1 + rd.nextInt(3);
    AircraftCarrier ac = new AircraftCarrier (fc, j + 1, parkingSlots, parkingSlots);
    carriers[i] = ac;
}

Now you are able to access them:

carriers[1].fps = 6; // You wanted id=2. Since we count from zero in Java, use 1
软糯酥胸 2024-12-11 12:51:52

您创建了每个实例,但没有保存它们。所以在循环之后,对象就“消失了”。
使用

List<AircraftCarrier> carriers = new ArrayList<AircraftCarrier>();
for (int j = 1; j < 4; j++) {
  int parkingSlot = 1 + rd.nextInt(3);
  AircraftCarrier ac = new AircraftCarrier (fc, j, parkingSlots, parkingSlots);
  carriers.add(ac);
}

由于您将 id 2 分配给了第二个元素,因此您现在可以使用 riers.get(1) 访问它(get(0) 将为您提供第一个元素)。

You created each of the instances, but saved them nowhere. So after the loop, the objects are "gone".
Use

List<AircraftCarrier> carriers = new ArrayList<AircraftCarrier>();
for (int j = 1; j < 4; j++) {
  int parkingSlot = 1 + rd.nextInt(3);
  AircraftCarrier ac = new AircraftCarrier (fc, j, parkingSlots, parkingSlots);
  carriers.add(ac);
}

Since you assigned id 2 to the second element, you can now access it using carriers.get(1) (get(0) would give you the first element).

半仙 2024-12-11 12:51:52

您必须将载体“放置”在某处,以便稍后可以访问它们:

Map<Integer, AircraftCarrier> carriers = new HashMap<Integer, AircraftCarrier>();

for (int j = 1; j < 4; j++) {
    int parkingSlot= 1 + rd.nextInt(3);
    AircraftCarrier ac= new AircraftCarrier (fc, j, parkingSlots, parkingSlots);
    carriers.put(j, ac);
}

/* update carrier with ID 2 */
carriers.get(2).fps = 1;

You must 'place' the carriers somewhere so that you can access them later:

Map<Integer, AircraftCarrier> carriers = new HashMap<Integer, AircraftCarrier>();

for (int j = 1; j < 4; j++) {
    int parkingSlot= 1 + rd.nextInt(3);
    AircraftCarrier ac= new AircraftCarrier (fc, j, parkingSlots, parkingSlots);
    carriers.put(j, ac);
}

/* update carrier with ID 2 */
carriers.get(2).fps = 1;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文