根据先前的ELEMENTS

时间:2019-01-20 03:57:13

标签: java for-loop if-statement arraylist

我是Java新手,但是一直在使用数组列表,现在卡住了。

我从一个名为Car的类创建了一个数组列表,该数组具有三个参数,其中一个称为 times (移动次数)。

主类

public class GarageTester {

/**
 * @param args the command line arguments
 */
  public static void main(String[] args) throws IOException{
   // create Bank object
   Garage bashimGarage = new Garage() ;


  // create Scanner object associated with input file
  Scanner fileScan = new Scanner(new 
   File("C:\\Users\\jamison\\Desktop\\GarageData.txt")) ;

  // read BankAccount data from file, create objects, and add to list

   while ( fileScan.hasNextLine())      // while not eof
   {

     String fullText = fileScan.nextLine();
     // Split the acquired string into 2 based on the whitespace
     String[] splitText = fullText.split("\\s+");
     // String before whitespace split
     String licensePlate = splitText[0];
     // String after whitespace split
     String status = splitText[1];
     // create Car object
     Car newCar = new Car(licensePlate, status , 0) ;  
     // add to list   
     bashimGarage.addCar( newCar ) ;   

   }


  /*
    *Calculates the number of times car was temporary moved before departure
  */
  bashimGarage.carDepart();


  /*
    *Prints list of car license plates
    * Admits or declines a car to the garage
    * Prints if a car departs the Garage
    * When a car departs also prints the number of times it was moved
  */
   bashimGarage.moveCarInGarage();

汽车课

public class Car {

   private String licensePlate;    // License Plate Number
   private String status ;         // Status: Arivved or Departed
   private int moved;              /* How many times the car 
                                  got moved out of the garage
                                  */


public Car ( String licenseNum, String carStatus , int timesMoved)
 {   
    licensePlate = licenseNum ;
    status = carStatus ;
    moved = timesMoved;
 }


public String getLicenseNum()
{   
  return licensePlate;
}


public String getStatus()
{   
  return status;
}



 public int getTimesMoved()
{   
  return moved;
}



public int setTimesMoved(int times){
    moved = moved + times;
    return moved;
  }

}

车库班

public class Garage {
   private ArrayList<Car> list ;  // a list of BankAccount objects
   public int maxCars = 10;       // max number of cars allowed in garage
   public int currentCars = 0;    // current number of cars in garage


public Garage()
{
  list = new ArrayList<Car>() ;
}


 public void addCar(Car newCar)
 {
   list.add(newCar) ;        // calls "add" method of ArrayList class
 } 


  public void carDepart() {
    for (int i = 0; i < list.size(); i++) {
    Car current = list.get(i);   // get next car
    if (current.getStatus().equals("DEPART")) {
        int pos = list.indexOf(current);

        for (int j = 0; j < pos; j++) {
            list.get(j).setTimesMoved(1 + current.getTimesMoved());
        }

        list.remove(i);
        return;
     }

    }
 }



  public void moveCarInGarage()
  {
    for ( int i = 0 ; i < list.size() ; i++ )
    {
     Car current = list.get( i ) ;   // get next car
     if (current.getStatus().equals("ARRIVE"))
     {
         currentCars++; //Increments current # of cars in garage

       if (currentCars <= 10) // Checks if there is space in garage
       {
           //Prints license plate and arrival status to screen
           System.out.println("Car with license plate" + 
            current.getLicenseNum() + " has arrived "
                   + "and been moved into the garage");
       }
       else 
       {
           // Prints garage is full to screen
           System.out.println("The garage is full at this " +
           "time so come back later");
        }
     }
      else  
      {

         currentCars--; // Decrements current # of cars in garage
         /* 
         Prints license plate, departure status, 
         and number of times moved to screen
         */
         System.out.println("Car with license plate" + 
           current.getLicenseNum() + " is departing and has been moved " 
                 + current.getTimesMoved() + " times" );

       }
    }
 }

我正在读取应该是车库的文件的输入,并说汽车是“到达” 还是“离开”

我正在尝试使用一条if语句编写代码,该语句表示状态为“正在离开”,则当前元素将被删除,其前面的所有元素都将添加到其“时间移动的参数”中

我停留的部分是根据删除的元素,将其在数组列表中位于其前面的所有元素添加到其“移动时间” 参数中的部分。< / p>

我想到了这一点,但是它似乎不起作用,因为当我调用第二个方法时,它总是对移动的时间说0。

public void carDepart()
   {
     for ( int i = 0 ; i < list.size() ; i++ )
        {
         Car current = list.get( i ) ;   // get next car
            if (current.getStatus().equals("DEPART"))
            {
              int pos = list.indexOf(i);

                for ( int j = 0 ; pos < j ; j++)
                {
                 current.setTimesMoved(1 + current.getTimesMoved());
                }

                 list.remove(i);
                 return;
          }

       }  
    }

第二种方法

    public void moveCarInGarage()
    {
      for ( int i = 0 ; i < list.size() ; i++ )
    {
       Car current = list.get( i ) ;     // get next car
       if (current.getStatus().equals("ARRIVE"))
     {
         currentCars++;

       if (currentCars <= 10) 
       {
           System.out.println("Car with license plate" + 
            current.getLicenseNum() + " has been moved into the garage");
       }
       else 
       {
           System.out.println("The garage is full at this " +
           "time so come back later");
       }
   }
     else 
     {
         currentCars--;
         System.out.println("Car with license plate" + 
           current.getLicenseNum() + " is departing and has been moved " 
                 + current.getTimesMoved() + " times" );
     }
  }

}

2 个答案:

答案 0 :(得分:0)

第一种方法,第8行。 当您在pos中提取i时,为什么要尝试提取它。 int pos = list.indexOf(i);将为-1。

此外,在此循环中

for ( int j = 0 ; pos < j ; j++)
    {
          current.setTimesMoved(1 + current.getTimesMoved());
    }

您总是指向arraylist中的相同元素。

相反,您可能希望像下面这样编程:

for ( int j = 0; j<i; j++)
    {
          Car car = list.get(j);
          car.setTimesMoved(1 + car.getTimesMoved());
    }

答案 1 :(得分:0)

您的方法存在以下问题,

  • 首先,您获得的是整数而不是对象的索引。
  • 您的内部for循环条件错误。
  • 您总是在增加当前对象的值,而不是 以前的。

我已按照您的方法更正了它们。在一个以下使用

public void carDepart() {
        for (int i = 0; i < list.size(); i++) {
            Car current = list.get(i);   // get next car
            if (current.getStatus().equals("DEPART")) {
                /* You can remove below line and replace pos with i in your inner loop. 
                Since the current object position will be same as i */
                int pos = list.indexOf(current);

                for (int j = 0; j < pos; j++) {
                    list.get(j).setTimesMoved(1 + current.getTimesMoved());
                }

                list.remove(i);
                return;
            }

        }
    }