Sinor Bodl
Sinor Bodl

Reputation: 73

How to remove an object from an array of objects in java

I need to make a method which deletes all objects in an array of objects that have the variable grade=1 and return the "resized" array of objects.

the objects looks like this:

public class Exam {
   private Course course; // Course is a class
   private Student student; // Student is a class
   private Integer grade;
   private LocalDateTime date; }

public class Student{
    private String id;
    private LocalDate birthDate; }

public class Course {
   private String id;
   private String name;
   private Integer ECTS;
   private Profesor subjectBearer;
   private Student[] student;}

the method needs to look something like this:

private Exam[] filterPassedExams(Exam[] exams) { ...}

any help or advice on how to solve the problem without using the lists would be awesome [on the course we didn't learn list yet so we can't really use them (But I would like to know that solution also if its faster for the future usage)].

Upvotes: 2

Views: 1037

Answers (4)

LuCio
LuCio

Reputation: 5193

My approach is to iterate over exams and at once collect the passed exams in a new array passedExams:

  private Exam[] filterPassedExams(Exam[] exams) {
    Exam[] passedExams = new Exam[exams.length];
    int size = 0;
    for (Exam exam : exams) {
      if (exam.getGrade() != 1) {
        passedExams[size++] = exam;
      }
    }

    return size == exams.length ?
      exams :
      Arrays.copyOf(passedExams, size);
  }

Since all exams might be passed the new array passedExams is initialized with the length of exams. If all exams has been passed, we return the original array. Otherwise we resize the passedExams array using Arrays.copyOfwhich returns a new array with the counted size.

Since an array has a fixed size, it's not possible to delete elements. Elements can be set null but not deleted. Thus it's impossible to resize an array. A new array with the filtered elements has to be created.

Upvotes: 1

The Scientific Method
The Scientific Method

Reputation: 2436

private Exam[] filterPassedExams(Exam[] exams) {    
    int size = 0;
    for(int i=0;i<exams.length;i++){
        if(exams[i].getGrade() != 1)
            size++;
    }
    Exam[] tmp = new Exam[size];
    size=0;
    for(int i=0;i<exams.length;i++){
        if(exams[i].getGrade() != 1)
            tmp[size++] = exams[i];
    }   

    return tmp;
}

Upvotes: 0

shb
shb

Reputation: 6277

Java is always pass-by-value. Your exams array is a copy of the argument you sent. Anyhow, as you are not supposed to use list you can run a loop and check for grades that are not 1, keep a counter. Then create a new array with the size of the counter. Filter the exams array again and this time assign the passed objects to newly created array. return the filtered array.

private Exam[] filterPassedExams(Exam[] exams) {
    int size = 0;
    for(int i=0; i<exams.length; i++)
         if(exam[i].getGrades !=1)
           size++;

    Exam[] filteredExams = new Exam[size]; //create a new array

    size = -1; //lets reuse this

    for(int i=0; i<exams.length; i++)
         if(exam[i].getGrades !=1)
           filteredExams[++size] = exam[i]; // assign the passed exam object to new filtered exam array

    return filteredExams;
}

Upvotes: 0

Mureinik
Mureinik

Reputation: 312259

I'd stream the array, filter the exams you need to retain, and convert the stream back to an array:

private Exam[] filterPassedExams(Exam[] exams) {
    return Arrays.stream(exams).filter(e -> e.grade.intValue() != 1).toArray(Exam[]::new);
}

Upvotes: 1

Related Questions