gpt4 book ai didi

java - 如何初始化数组的对象?

转载 作者:太空宇宙 更新时间:2023-11-04 13:14:42 26 4
gpt4 key购买 nike

我有一个包含 10 个对象的数组,我想为该数组初始化它们。

private Flight[] flight = new Flight[10];

flight[0] = new Flight("AA101", "Boston", "Sacremento", 421.33, 22, 6);
flight[1] = new Flight("AA102", "Lincolin", Jacksonville, 612.08, 22 6);
flight[2] = new Flight("AA103", "Rochester", "Columbia", 510.83, 22, 6);
flight[3] = new Flight("AA104", "Boise", "Aspen", 411.12, 22, 6);
flight[4] = new Flight("AA105", "Fort Collins", "Louiseville", 654.22, 22, 6);
flight{5} = new Flight("AA106", "Seattle", "Dallas" 326.14, 22, 6);
flight{6} = new Flight("AA107", "Phoenix", "Little Rock", 974.22, 22, 6);
flight[7] = new Flight("AA108", "Cheyenne", "Louiseville", 521.43 , 22, 6);
flight[8] = new Flight("AA109", "Louiseville", "Buffalo", 452.21, 22, 6);
flight[9] = new Flight("AA100", "Anchorage" , "Tampa", 874.72, 22, 6);

但我收到大量错误,例如:

ReservationGUI.java:31: error: ']' expected

ReservationGUI.java:31: error: illegal start of type

ReservationGUI.java:31: error: ';' expected

ReservationGUI.java:31: error: expected

共有 100 个。

最佳答案

对于 5 和 6 个元素,使用方括号代替大括号

关于java - 如何初始化数组的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33654201/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com