Java: деление одного массива по значению элемента объекта на 2 массива - PullRequest
0 голосов
/ 29 сентября 2018

Изменить.спасибо.

У меня есть множество «обычных» и «больших» автомобилей.У меня есть задание, требующее, чтобы я разделил их, чтобы внести свой вклад в гораздо большее приложение.Один массив для больших транспортных средств, один для обычных транспортных средств, содержащий всю информацию для каждого элемента.ArrayLists не разрешены, так как мой инструктор учит нас основам.

Образец массива

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

Ответы [ 2 ]

0 голосов
/ 29 сентября 2018

Кажется, вы не можете использовать встроенный класс LinkedList , затем сделайте это:

Добавьте следующий код в свой класс транспортного средства:

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

И та же история относится и к обычным транспортным средствам.Есть вопросы?

0 голосов
/ 29 сентября 2018

Вы можете написать свои циклы так:

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;
...