Advertisement
Samuel_Berkat_Hulu

ClassDataArray

Mar 29th, 2021
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 KB | None | 0 0
  1.  
  2. /**
  3.  * Write a description of class ClassDataArray here.
  4.  *
  5.  * @author (your name)
  6.  * @version (a version number or a date)
  7.  */
  8. class ClassDataArray
  9.  {
  10.  private Person[] a; // reference to array
  11.  private int nElems; // number of data items
  12.  public ClassDataArray(int max) // constructor
  13.  {
  14.  a = new Person[max]; // create the array
  15.  nElems = 0; // no items yet
  16.  }
  17.  public Person find(String searchName)
  18.  { // find specified value
  19.  int j;
  20.  for(j=0; j<nElems; j++) // for each element,
  21.  if( a[j].getLast().equals(searchName) ) // found item?
  22.  break; // exit loop before end
  23.  if(j == nElems) // gone to end?
  24.  return null; // yes, can't find it
  25.  else
  26.  return a[j]; // no, found it
  27.  } // end find()
  28.  public void insert(String last, String first, int age)
  29.  {
  30.  a[nElems] = new Person(last, first, age);
  31.  nElems++; // increment size
  32.  }
  33.  public boolean delete(String searchName)
  34.  { // delete person from array
  35.  int j;
  36.  for(j=0; j<nElems; j++) // look for it
  37.  if( a[j].getLast().equals(searchName) )
  38.  break;
  39.  if(j==nElems) // can't find it
  40.  return false;
  41.  else // found it
  42.  {
  43.  for(int k=j; k<nElems; k++) // shift down
  44.  a[k] = a[k+1];
  45.  nElems--; // decrement size
  46.  return true;
  47.  }
  48.  } // end delete()
  49.  public void displayA() // displays array contents
  50.  {
  51.  for(int j=0; j<nElems; j++) // for each element,
  52.  a[j].displayPerson(); // display it
  53.  }
  54.  } // end class ClassDataArray
  55.  
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement