Java:将单个数组除以一个对象元素值分成2个数组
Java: Dividing a single array by a object element value in to 2 arrays
编辑。谢谢。
我有一组 'normal' 辆车和 'large' 辆车。我有一项任务要求我将它们分开以贡献更大的应用程序。
一个数组用于大型车辆,一个数组用于包含每个元素的所有信息的普通车辆。不允许使用 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
你可以这样写你的循环:
for(int i = 0; i < normalVehicles.length; i++)
{
if(normalVehicles[i] == null)
{
normalVehicles[i] = v;
break;
}
}
// if last slot isn't null then it's full
normBool = normalVehicles[normalVehicles.length-1] != null;
似乎你不能使用内置的 LinkedList class,那么这样做:
在您的载具中添加以下代码class:
class Vehicle(){
//YOUR OTHER PIECES OF CODES ...
private static Vehicle firstLargeVehicle;
private Vehicle nextLargeVehicle;
private int index;
public void setIndex(int index){
this.index = index;
if(index == 0) Vehicle.firstLargeVehicle = this;
}
public int getIndex(){
return index;
}
public void setNextLargeVehicle(Vehicle nextLargeVehicle){
this.nextLargeVehicle = nextLargeVehicle;
}
public Vehicle getNextLargeVehicle(){
return nextLargeVehicle;
}
public addLargeVehicle(Vehicle newVehicle){
this.nextLargeVehicle = newVehicle;
newVehicle.setIndex(index + 1);
}
public getListSize(){
Vehicle lastOne = this;
while (lastOne.getNextLargeVehicle() != null){
lastOne = lastOne.getNextLargeVehicle();
}
return lastOne.getIndex() + 1;
}
public static Vehicle[] largeVehiclesToArray(){
Vehicle[] result = new Vehicle[firstLargeVehicle.getListSize()]();
Vehicle pointer = firstLargeVehicle;
for (int counter = 0; pointer != null; counter ++){
result[counter] = pointer;
pointer = pointer.getNextLargeVehicle();
}
return result;
}
}
然后在您的主循环中,执行类似于以下代码的操作:
Vehicle vehicle = null;
for(Vehicle newVehicle : allVehicles) {
if (newVehicle.isLarge()){
if (vehicle == null) {
vehicle = newVehicle;
vehicle.setIndex(0);
}else{
vehicle.addLargeVehicle(newVehicle));
}
}
}
Vehicle[] largeVehicles = Vehicle.largeVehiclesToArray();
普通车辆也是如此。
有什么问题吗?
编辑。谢谢。
我有一组 'normal' 辆车和 'large' 辆车。我有一项任务要求我将它们分开以贡献更大的应用程序。 一个数组用于大型车辆,一个数组用于包含每个元素的所有信息的普通车辆。不允许使用 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
你可以这样写你的循环:
for(int i = 0; i < normalVehicles.length; i++)
{
if(normalVehicles[i] == null)
{
normalVehicles[i] = v;
break;
}
}
// if last slot isn't null then it's full
normBool = normalVehicles[normalVehicles.length-1] != null;
似乎你不能使用内置的 LinkedList class,那么这样做:
在您的载具中添加以下代码class:
class Vehicle(){ //YOUR OTHER PIECES OF CODES ... private static Vehicle firstLargeVehicle; private Vehicle nextLargeVehicle; private int index; public void setIndex(int index){ this.index = index; if(index == 0) Vehicle.firstLargeVehicle = this; } public int getIndex(){ return index; } public void setNextLargeVehicle(Vehicle nextLargeVehicle){ this.nextLargeVehicle = nextLargeVehicle; } public Vehicle getNextLargeVehicle(){ return nextLargeVehicle; } public addLargeVehicle(Vehicle newVehicle){ this.nextLargeVehicle = newVehicle; newVehicle.setIndex(index + 1); } public getListSize(){ Vehicle lastOne = this; while (lastOne.getNextLargeVehicle() != null){ lastOne = lastOne.getNextLargeVehicle(); } return lastOne.getIndex() + 1; } public static Vehicle[] largeVehiclesToArray(){ Vehicle[] result = new Vehicle[firstLargeVehicle.getListSize()](); Vehicle pointer = firstLargeVehicle; for (int counter = 0; pointer != null; counter ++){ result[counter] = pointer; pointer = pointer.getNextLargeVehicle(); } return result; } }
然后在您的主循环中,执行类似于以下代码的操作:
Vehicle vehicle = null; for(Vehicle newVehicle : allVehicles) { if (newVehicle.isLarge()){ if (vehicle == null) { vehicle = newVehicle; vehicle.setIndex(0); }else{ vehicle.addLargeVehicle(newVehicle)); } } } Vehicle[] largeVehicles = Vehicle.largeVehiclesToArray();
普通车辆也是如此。 有什么问题吗?