NicholasAdamou

Removing an Element from an Array

Sep 1st, 2019
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. /**
  2. * Program designed to showcase how to search then remove an element from a generic array of objects in Java.
  3. * Assume 'Student' is defined.
  4. * @author Nick Adamou
  5. * @date 9/1/2019
  6. */
  7.  
  8. public class Main {
  9.     // Function to obtain a specific student's position in an array of students
  10.     public static int search(Student[] students, String firstName, String lastName) {
  11.         // Loop over every student in the array of students.
  12.         for (int i = 0; i < students.length; i++) {
  13.             // Get the current student at this position in the array.
  14.             Student student = students[i];
  15.            
  16.             // Check if student is null
  17.             if (student == null) {
  18.                 // If student is null, then continue.
  19.                 conttinue;
  20.             }
  21.  
  22.             // Check if this student is the one we are looking for.
  23.             if (student.getFirstName().equalsIgnoreCase(firstName) &&
  24.                 student.getlastName().equalsIgnoreCase(lastName)) {
  25.                 // If it is the student we are looking for, return it's position in the array.
  26.                 return i;
  27.             }
  28.         }
  29.        
  30.         // If the student could not be found, return -1.
  31.         return -1;
  32.     }
  33.    
  34.     // Function used to remove a student from an array of students.
  35.     public static Student removeStudent(Student[] students, firstName, lastName) {
  36.         // Indicate to the user that we are removing a student.
  37.         System.out.printf("Removing student (%s, %s)", firstName, lastName);
  38.  
  39.         // Search for the student in the array and get its position in the array.
  40.         int index = search(students, firstName, lastName);
  41.        
  42.         // Store a blank version of the targetted student
  43.         Student student = new Student()
  44.        
  45.         // Check if the student was found.
  46.         if (index != -1) {
  47.             // Indicate that the student was found.
  48.             System.out.printf("FOUND Student (%s, %s) @ students[%d]", firstName, lastName, index);
  49.  
  50.             // Store the student we want to remove in 'student'.
  51.             student = students[index]
  52.  
  53.             // If the student was found, start looping over the array of students starting at
  54.             // that students position.
  55.             for (int i = index; i < students.length; i++) {
  56.                 // Shift each student's position by 1.
  57.                 students[i] = students[i + 1];
  58.             }
  59.  
  60.             // Decrease the index by 1. (Assume 'index' is defined by your class).
  61.             this.index--;
  62.  
  63.             // Display the resulting array of students.
  64.             for (int i = 0; i < students.length; i++) {
  65.                 // Store the student at this index.
  66.                 Student student = students[i];
  67.  
  68.                 // Only display non-null students.
  69.                 if (student != null) {
  70.                     System.out.println(students[i]);
  71.                 }
  72.             }
  73.         } else {
  74.             // Indicate that the student was not found.
  75.             System.out.printf("COULD NOT FIND Student (%s, %s)", firstName, lastName);
  76.         }
  77.        
  78.         // Return the resulting array of students.
  79.         return student;
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment