How to change a specific parameters of the specific elements in an array-list based on another elements parameters

Arthur :

I am new to java but have been playing with array-lists and am now stuck.

I have an array list created off a class called Car with three parameters one of which is called times moved.

ArrayList:

 private ArrayList<Car> list = new ArrayList<Car>() ;

Car class

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

I am reading input of a file that is supposed to be like a garage and says if a car is "Arriving" or "Departing"

I am trying to write a code using an if statement that says if the status is "Departing" then the current element gets deleted the all elements in front of it add one to their "times moved parameter"

The part I am stuck on is the one where, based on the element getting deleted, all the elements in front of it in the array list add one to their "times moved" parameter.

Would anyone give me some advice on how it would be possible to do that?

I came up with this but it does not seem to work

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);
          }
          break;
       }  
    }

Second method

    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" );
     }
  }

}

LuminousNutria :

Here is an example of what you can do. In it, I assume you are using getters and setters. You can also call the attributes directly assuming you've set the access modifiers in a way that allows you to do so.

All I did was create a new method called incrementTimesMoved() that iterates through your ArrayList and increments all the "moved" attributes in it's elements until it gets to the one with a given index. It removes this, and stops running.

public class MCVE {

public static void main(String[] args) {
   // IMO list isn't very descriptive, so I changed it to carList.
   ArrayList<Car> carList = new ArrayList<>();

   // Add a bunch of values to carList here.

   for(int i = 0; i < carList.size(); i++) {
      if(carList.get(i).getStatus().equals("Departing")) {
         incrementTimesMoved(i, carList);
         return; // stops the method
      }
   }

}

// only static because I am calling in the main() function
private static void incrementTimesMoved(int index, ArrayList<Car> carList) {

   for(int i = 0; i < carList.size(); i++) {

      if(i == index) {
         carList.remove(index);
         return;
      }

      carList.get(i).setMoved(carList.get(i).getMoved() += 1);
   }
}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=148207&siteId=1
Recommended