【发布时间】:2018-09-28 22:14:22
【问题描述】:
编辑。谢谢。
我有一系列“普通”车辆和“大型”车辆。我有一项任务要求我将它们分开,以便为更大的应用程序做出贡献。 一个用于大型车辆的数组,一个用于包含每个元素的所有信息的普通车辆。不允许使用 ArrayList,因为我的老师正在教我们基础知识。
数组样本
27723 4/09/61 large 7337
28507 22-02-1983 large 7055
28558 1/05/70 normal 3518
//On button press
//recieve single item from array from main and test it
//array in main will be looped for all elements.
public String loadVehicle(Vehicle v) {
String res = Constants.OK;
boolean normBool = false;
boolean largeBool = false;
//if both arrays are full , stop the method call in the main form
if (normBool && largeBool){return Constants.ERROR;}
//if vehicle size is normal, fill the normal veh array
if(v.getSize().equals(Constants.NORMAL_SIZE))
{
for(int i = 0; i<normalVehicles.length; i++)
{
//if norm veh array element is null, add the appropriate value to it
if(normalVehicles[i] == null){normalVehicles[i] = v;}
else{normBool = true;}
}
}
//if veh size is large put it in the large veh array
else if(v.getSize().equals(Constants.LARGE_SIZE))
{
for(int iL = 0; iL<largeVehicles.length; iL++)
{
if(largeVehicles[iL] == null){largeVehicles[iL] = v;}
else{largeBool = true;}
}
}
return res;
}//end method
【问题讨论】:
-
到目前为止,我已经设法用数组中第一个元素的重复条目填充普通数组
-
当您找到并覆盖空条目时,您需要退出循环迭代。尝试使用
return或break来做到这一点。或者更好的是,跟踪您随时间记录的大型和普通车辆的数量,并且您不必在loadVehicle内部进行任何循环。 -
你可以使用linkedList吗?听起来更好
-
谢谢 Ruz,我很欣赏 joe 的建议,我认为这有点超出预期的范围,但我确实喜欢遇到新技术,一旦我把这些古老的数组抛在脑后,它们就会对我有所帮助。跨度>
标签: java arrays element divide