[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

Clearing an array...

run:

Peter,
Anne,
Mike,

after clearing... should be zero but it gives 3:  <<<<<<<<<<<<<<<<
Number of students in course1: 3 students

BUILD SUCCESSFUL (total time: 0 seconds)

Question: Number of students in course1 is expected to be zero.Why am I getting 3?

see the attached code for course1.clear(); method.

Thank you.
public class TestNewCourse { // Excercise10_9
    public static void main(String[] args){
        Course course1=new Course("Data Structure");
        Course course2=new Course("Database Systems");
        
        course1.addStudent("Peter");
        course1.addStudent("Brian");
        course1.addStudent("Anne");
        course1.addStudent("Mike");
//
        course1.dropStudent("Brian");  //***************************
        
        course2.addStudent("Peter");
        course2.addStudent("Jack");
        
//        System.out.println("Number of students in cours1: "+
//            course1.getNumberOfStudents());
        String[] students=course1.getStudent();
        for(int i=0;i<course1.getNumberOfStudents();i++)
            System.out.println(students[i]+", ");
        
//        System.out.println("");
////                
//        System.out.println("Number of students in cours2: "+
//            course2.getNumberOfStudents()+" students");
        
        course1.clear();
        System.out.println("after clearing... should be zero but it gives 3:");
        System.out.println("Number of students in course1: "+
            course1.getNumberOfStudents()+" students");
        
    }
}

class Course{
    private String courseName;
    private String[] students=new String[2];
    private int numberOfStudents;
    
    public Course(String courseName){
        this.courseName=courseName;
    }
    
    public void clear(){
        String[] students=new String[2];
    }
    
    public void addStudent(String student){
        
        if (numberOfStudents==students.length){
            String[] temp=new String[2*students.length];
            System.arraycopy(students, 0, temp, 0, numberOfStudents);
            students=temp;
        }       
        students[numberOfStudents]=student;
        numberOfStudents++;
    }
    
    public String[] getStudent(){
        return students;
    }
    
    public int getNumberOfStudents(){
        return numberOfStudents;
    }    
    
    public String[] getCourseName(){
        return students;
    }
    
    private int findStudent(String student) {
    for (int p = 0 ; p < students.length; p++) {
       if (students[p].equals(student))    // Consider equalsIgnoreCase here too perhaps
          return p ;
    }
    return -1 ;
    }
    
    public void dropStudent(String student){
        int pos = findStudent(student);
        if (pos == -1) {
            System.err.println("Tried to drop student " + student + " who is not in the list") ;
        }else{
            System.arraycopy(students, pos+1, students, pos, students.length-pos-1);          
        }
        numberOfStudents-- ;
   }     
}

Open in new window

0
Mike Eghtebas
Asked:
Mike Eghtebas
  • 6
1 Solution
 
for_yanCommented:
Your clear method craetes a local students array
instead it shoule clear th instance array

remove Student[] from inside the methpd like this:
 public void clear(){
         students=new String[2];
    }
   
0
 
for_yanCommented:



class Course {
Student [] students;  // this is instance vraiable - array 

void clear() {
Student [] students = new Student[2];  // this is arrays local to the method - it does nolt affect uyour instance variable
}

}
 

Open in new window

0
 
for_yanCommented:
class Course {
Student [] students;  // this is instance vraiable - array 

void clear() {
students = new Student[2];  // this will re-create your instance array
}

}
 

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
for_yanCommented:

Besisde even after you really clear instance array of students,
you'll still not get different number from this method:

 
    public int getNumberOfStudents(){
        return numberOfStudents;
    }    


you nedd to say:

  public int getNumberOfStudents(){
        return students.length;;
    }    


But even this will return the number of elements, so
if in your clear() metthod calling it before you said:


students = new Student[2];


then getNumberOfStudents() will return 2 for you
even though you haven't yet populated
the array with anyb real studentys, just created the array

You actullly need to maintain numberOfStudents separately
by incrementing this number when you add real students






0
 
for_yanCommented:
This would work for this case, I believe:
But better use ArrayList for your students - it is much simpler

class Course{
    private String courseName;
    private String[] students=new String[2];
    private int numberOfStudents;
    
    public Course(String courseName){
        this.courseName=courseName;
    }
    
    public void clear(){
         students=new String[2];
    numberOfStudents = 0;
    }
    
    public void addStudent(String student){
        
        if (numberOfStudents==students.length){
            String[] temp=new String[2*students.length];
            System.arraycopy(students, 0, temp, 0, numberOfStudents);
            students=temp;
        }       
        students[numberOfStudents]=student;
        numberOfStudents++;
    }
    
    public String[] getStudent(){
        return students;
    }
    
    public int getNumberOfStudents(){
        return numberOfStudents;
    }    
    
    public String[] getCourseName(){
        return students;
    }
    
    private int findStudent(String student) {
    for (int p = 0 ; p < students.length; p++) {
       if (students[p].equals(student))    // Consider equalsIgnoreCase here too perhaps
          return p ;
    }
    return -1 ;
    }
    
    public void dropStudent(String student){
        int pos = findStudent(student);
        if (pos == -1) {
            System.err.println("Tried to drop student " + student + " who is not in the list") ;
        }else{
            System.arraycopy(students, pos+1, students, pos, students.length-pos-1);          
        }
        numberOfStudents-- ;
   }     
}

Open in new window

0
 
for_yanCommented:

If you do it the way you did it originally:
 public void clear(){
        String[] students=new String[2];
    }

Open in new window


then within the method clear() symbol students will
apply to local array which is balid only within this method and you instnce array
- the only thing which you can aceess form outside class will be shilded within this method
and woiuld be inaccessible.
So you should be careful and always rememeber that instance variables are those
which are declared within the classs ouside any methods
0
 
Mike EghtebasDatabase and Application DeveloperAuthor Commented:
Thank you
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now