【问题标题】:Java: Dividing a single array by a object element value in to 2 arraysJava:将单个数组除以对象元素值到 2 个数组
【发布时间】: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

【问题讨论】:

  • 到目前为止,我已经设法用数组中第一个元素的重复条目填充普通数组
  • 当您找到并覆盖空条目时,您需要退出循环迭代。尝试使用returnbreak 来做到这一点。或者更好的是,跟踪您随时间记录的大型和普通车辆的数量,并且您不必在 loadVehicle 内部进行任何循环。
  • 你可以使用linkedList吗?听起来更好
  • 谢谢 Ruz,我很欣赏 joe 的建议,我认为这有点超出预期的范围,但我确实喜欢遇到新技术,一旦我把这些古老的数组抛在脑后,它们就会对我有所帮助。跨度>

标签: java arrays element divide


【解决方案1】:

似乎你也不能使用内置的 LinkedList 类,然后这样做:

在您的 Vehicle 类中添加以下代码:

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();

同样的情况也适用于普通车辆。 有什么问题吗?

【讨论】:

  • 我的查询需要这么多代码,我非常感谢您的努力,我正在实施
  • 非常欢迎@Philip,如果代码有任何问题,请告诉我。
【解决方案2】:

你可以这样写你的循环:

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;

【讨论】:

  • 我喜欢单行布尔检查和赋值。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-23
  • 2020-12-27
  • 2019-12-22
相关资源
最近更新 更多