All pastes #30834 Raw Edit

Something

public java v1 · immutable
#30834 ·published 2005-11-24 02:22 UTC
rendered paste body
package amandapkg; //make sure u import thisimport java.util.ArrayList;  public class DMVDatabase {    private ArrayList<Vehicle> vehicles;  private int size;  //size of the array taken in through constructor  private int numberVehicles;  //actual number of vehicles in array    public DMVDatabase(int size) {    this.size = size;  //intialize size    vehicles = new ArrayList<Vehicle> (size);  //intializes arrayLIST    numberVehicles = vehicles.size();  /*.size() returns actual # elements in arrayLIST (method)*/  }    //accessor method for private attribute  public int getSize() {    return size;  }   /***************************************************************************/  public int getNumberVehicles() {    return vehicles.size();  }   /***************************************************************************/  public String toString() {    String result = "";    for (int i = 0; i < vehicles.size(); i++) {      result += "vehicles " + vehicles.get(i).toString() + "\n";    }    return result;  }   /***************************************************************************/   public void add(Vehicle v) {/*ok here is what i wanted this code to do, basically i was thinking if i put only the string license plates into an arraylist i could copy those into a comparable array and then use the insertionSort method to make sure it got properly inserted in the right place.  that is soooooooooooo not happening. :(*/     ArrayList temp = new ArrayList(vehicles.size() + 1);    //if the arraylist is empty then just add it and increment count    if (vehicles.isEmpty()) {      vehicles.add(v);      numberVehicles++;    }    //otherwise    else {       //create a new array to store comparables in      vehicles.add(v);  /*add what uwant to add now before u sort it*/      numberVehicles++;  /*make one more element*/      Comparable[] list = new Comparable[0];  /*comparable array*/       //creat a for loop to put all of the license plates into the array      ArrayList plates = new ArrayList(vehicles.size() + 1);      for (int t = 0; t < vehicles.size() + 1; t++) {        plates.add(t, vehicles.get(t).getLicensePlateNumber());      }      //put all of the license plates into the list comparable array      list = (Comparable[]) (plates.toArray(list)); //look at this part       //using another loop keep doing this till all the items have been sorted      for (int i = 1; i < vehicles.size() + 1; i++) {        //keeps track of what ur sorting        /*key is the second item in the array*/        Comparable key = list[i];        int pos = i;         //while theres more things and key is smaller, move everyting over        /*we assume that the first item inthe array is in the corret order, after that we check to see if if the item after that is in the right order, if it is then i is incrememented to the next number, if its not and the item to the left is larger then we trade spaces with both*/        while (pos > 0 && key.compareTo(list[pos - 1]) < 0) {          list[pos] = list[pos - 1];          pos--;         }         list[pos] = key;       }  /*this is when the loop is finished and the array has been sorted*//*can i transfer the comparables back into another array and finally put it back into the vehicles array?  keeping in mind that list holds on licenseplates?  helpppppppppppppppppp! :))*/      /* for (int s = 0; s < vehicles.size() + 1; s++) {         temp = (Vehicle) (plates.toArray(list));*/     }  } }