2

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)].

6
  • 1
    Have you thought about a solution? Tried something? What's the concrete problem you're facing? Note that the signature of your method contradicts the title and the text of your question: the method creates another array, containing only the passed exams. it doesn't remove anything from the original array. That should guide you to the solution. Commented Oct 20, 2018 at 20:12
  • 1
    This problem has been answered and implemented many, many times. Assuming you have done some minimal research it's not clear what the problem is. Commented Oct 20, 2018 at 20:14
  • @JBNizet i would make a loop that itterats through field to find the exam[i].getGrade that =1 but i dont know how to "delete that whole object " to resize the field. I would like to copy the objects 1 by 1 but i dont have the dynamic field so im stuck . Commented Oct 20, 2018 at 20:24
  • Again, you shouldn't delete or resize anything. You should create another array, of the right length, and copy all the exams with the accepted grade to that new array. Commented Oct 20, 2018 at 20:25
  • so i should first itterate through the field and count how many grades are >1. then crate new array of that size and then itterate again through field and if grade>1 copy it? Commented Oct 20, 2018 at 20:29

4 Answers 4

1

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);
}
Sign up to request clarification or add additional context in comments.

5 Comments

This solution is too complicated for a beginner.
I'd do that too, but if Lists are forbidden because they haven't been learnt yet, I doubt Streams have.
To use Streamat least Java 8 is needed. But then unboxing will be applied too: no need for .intValue()
OP is probably using Java 8, since LocalDate is used.
@MCEmperor It's just a hint that if the answer uses Java 8, then it could also let the compiler do the int unboxing.
1

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.

Comments

0

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

Comments

0
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;
}

1 Comment

how to solve the problem withouth using the lists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.