Zen_2011
Zen_2011

Reputation: 75

Creating a remove() method to remove an item from an arraylist (Java)

I have 2 classes Student class and StudentTest class. In my Student class I need to write a method called remove(int ID) to remove a student with a specific ID, from an arraylist. The method is called by the main() method in StudentTest class. the code looks like this:

public class Student {
     private final int size = 12;  //max. size
     private int [] ID = new int [size];   //the student's id number
     private String [] name = new String [size];  //the student's name
     private double [] tuition = new double [size];

     int position= 0;  //position to add data

//an add() method goes here, but is not the case of my question so I'm emitting it

 //Here is the remove() to remove a student given their ID number
 public void remove(int ID){
 for(int i=0; i<size ; i++)
    if (ID[i].equals(ID)
 {
   remove(i);
   return true;
  }
  return false;
  }//remove() :this method is so wrong I know, but I've been trying so many different  things and its just driving me nutts!

 //a method goes here to display student info.
  } //end Student class

  //below is my StudentTest class which will be calling the remove() method

  public class StudentTest extends Student {
      //main
      public static void main(String args[]){

        Student stuList = new Student();

         stuList.add(1234, "Jane Jane", 23000);
         stuList.add(4321, "Billy Bill", 15500);
         //2 students are added to the list: in this order; (ID, "Name", Tuition)

         //now this main program calls remove(), to remove a student by ID
         stuList.remove(1234);

        //rest of code entails displaying the new list and so on

        }//main()
       }// StudentTest class

Now. My remove method desperately needs help. I've studied the ArrayList class and its methods. but simply writing stuList.remove() doesn't work at all. I also tried the iterator method (I got lost on that one). Please guide me in the right direction ..thanks!

Upvotes: 1

Views: 8926

Answers (4)

pgras
pgras

Reputation: 12770

You have 3 arrays and an int position to store the position to add data. If you remove one student you have to:

1) find it's position in the array, say r is the position to remove (it can be between 0 and position-1)

2) decrease the position (position = position - 1) because now your list will be shorter.

3) replace, for all 3 arrays, element with position r with the one located at position r+1, now you have lost the element at position r and you have twice the one that is located at r+1.

4)replace r+1 with r+2 and so on until you have replaced position-1 with position (the new value of position)

If you have problems implementing this show us some code and ask for help again...

EDIT: to respond to your comments:

You have 7 elements numbered 0 to 6, position is 7 as it is where to insert the next value, you want to remove the one numbered 4 (r=4). Here is a simpler solution but it will change the order of the list:

position = position - 1; // now position is 6
array[r] = array[position]; // now element at position 4 was replaced with the one at the end of the array, which is still there by the way. Do this for all the 3 arrays...

That's it...

Upvotes: 0

Tonny
Tonny

Reputation: 76

I would give up on solving the immediate issue and return to the design and get the OOP right, starting with

1) Student, should that be a collection or does it represent a single student.

Is the an assignment in an introduction programming course?

Upvotes: 5

Stephen
Stephen

Reputation: 3084

I dont see why you have to have the StudentID, Name and Tuition as arrays, the student class should define a "Student" not multiple students.

Class 1 - Student

Student
{
   int ID;
   string Name;
   double Tution;
}

Class 2 - StudentManager

StudentManager
{
   Student ListOfStudents;

   AddStudent();
   RemoveStudent();
}

EDIT:

The Student Class represents one student, and the features of that student such as Name, and Tuition, the StudentManager is used for interacting with Students objects adding and removing them from Lists etc, as opposed to having 3 arrays containing one piece of the students information and trying to update them all, this is poor design and its good to learn to avoid this kind of thing early on.

When I was learning OOP before I even started coding I used to identify possible objects that could be translated into Classes, discovered what properties they could have and how they would interact with other Objects.

You will see that no-one will post a solution to your problem here as this is homework, but we can try and help you understand how you can solve your problem.

Upvotes: 1

Suraj Chandran
Suraj Chandran

Reputation: 24791

The problem in your code is that your remove() method calls itself recursively infinetly to die with a StackOverflow

public void remove(int ID){
 boolean found = false;
 int i = 0;
 for(i=0; i<size ; i++)
    if (ID[i].equals(ID)
 {
   found = true;
   break;

 }
 if (found) {
    // remove the item and push all subsequent items to save space.
    while (i < size - 1; i++) {
       ID[i] = ID[i + 1];
    }
 }
 return found;
}

Upvotes: -1

Related Questions