Linked List - Deleting Element That User Specifies As A Parameter

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

32 Replies - 8310 Views - Last Post: 21 March 2013 - 05:23 AM Rate Topic: -----

#16 parry2411   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 13-February 13

Re: Linked List - Deleting Element That User Specifies As A Parameter

Posted 19 March 2013 - 12:57 AM

View Postpbl, on 18 March 2013 - 05:19 PM, said:

I don't know in which language I can tell you

The LinkList remove(Object) method when asked to remove and object scan the list, use the equals() method to identify the object to remove

   Student s1 = new Student("John", "Lennon", 101);
   Student s2 = new Student("John", "Lennon", 101);

   // this is internally what the remove() method will do
   System.out.println("Is s1 equals s2:", s1.equals(s2));


should print "false"
s1 and s2 are not equal until you write an equals() method that return true if they have the same ID



I undrstand this what you are saying, If i had 2 student objects the same i would use this to delete from the lisst. But in my case it doesnt matter if the sudentsList have duplicates, All i need in the end is an interface where:

It loads a menu with:

1.Add Student
2.Delete Student
3.Print Registry
4.quit

When it comes to deleteing a student it will ask for:

Please Enter Students ID That You Wish To Delete:> (YOU ENTER AN ID HERE, INT Values)

then this then will look through the LinedList for anyObject that has the studentId of what has been entered, When its found it will delete that student from the LinkedList.

Is this a possible feature that i can do like?

Sorry for the trouble im causing
Was This Post Helpful? 0
  • +
  • -

#17 parry2411   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 13-February 13

Re: Linked List - Deleting Element That User Specifies As A Parameter

Posted 19 March 2013 - 01:09 AM

I think i have done it, One little word missing break,

public void deleteStudent(int studentID)
    {
        int listSize = studentList.size();
        
        System.out.println(listSize);
        
        for (int i = 0; i < listSize; i++)
        {
            Student x = studentList.get(i);
            if (x.getStudentID() == studentID)
            {
                studentList.remove(i);
                break;
            }
        }
    }

Was This Post Helpful? 0
  • +
  • -

#18 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Linked List - Deleting Element That User Specifies As A Parameter

Posted 19 March 2013 - 03:53 AM

View Postparry2411, on 19 March 2013 - 03:57 AM, said:

I undrstand this what you are saying,

I am afraid not

View Postparry2411, on 19 March 2013 - 03:57 AM, said:

If i had 2 student objects the same i would use this to delete from the lisst. But in my case it doesnt matter if the sudentsList have duplicates,

So to that, you pass the Student object to the LinkedList remove() method
This remove() method will scan again all the List comparing each of the element in the list with the one you passed as parameter. If the two objects are equal (which is done by calling these objects equals() method), the method will remove the one from the list and exit. So if there are duplicate, you will have to call the method multiple times.
Was This Post Helpful? 0
  • +
  • -

#19 parry2411   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 13-February 13

Re: Linked List - Deleting Element That User Specifies As A Parameter

Posted 20 March 2013 - 12:55 AM

Im not checking for duplicates, All im doing is Adding an interface so a person can, ADD, DELETE and VIEW students, When the user goes to delete it asks for an ID of a student, Then it looks through the List to find that Student Object with ID...., Then Dleetes it, Now Im not ment to use some methods of Linked List for example Remove, Which i have so i have a newer version:

  public void deleteStudent(int studentID)
    {
        while (iter.hasNext())
        {
            Student ob = iter.next();
            if (ob.getStudentID() == studentID)
            {
                iter.remove();
                break;
            }
        }
    }


Now how would i modify this so that if an ID is not found it brings a message to the user saying No Id Found, And also for my add student how would i get it to verify that A studnet object is not the same already in the Linked List,

Heres the code for the whole Register Class:

import java.util.Iterator;
import java.util.LinkedList;

public class Registry
{

    public LinkedList<Student> studentList = new LinkedList<>();
    public Iterator<Student> iter = studentList.iterator();
   

    public Registry()
    {
    }

    public void addStudent(Student aStudent)
    {
        studentList.addLast(aStudent);
    }

    public void deleteStudent(int studentID)
    {
        while (iter.hasNext())
        {
            Student ob = iter.next();
            if (ob.getStudentID() == studentID)
            {
                iter.remove();
                break;
            }
        }
    }

    public String format()
    {   
        
        
        return null;
        
    }

    public static void main(String[] args)
    {
        Student s1 = new Student("Sall", "Willian", 980);
        Student s2 = new Student("Sallie", "Jones", 789);

        Registry r = new Registry();
        
        r.addStudent(s1);
        r.addStudent(s2);
        
        System.out.println(r.format());
        
 
    }
}



Also with the format method, ive been trying to get it to print out the elements from the Linked List in this style:

Sallie Williams 980

It never works for me keep getting the error:

run:
Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:953)
	at java.util.LinkedList$ListItr.next(LinkedList.java:886)
	at student.Registry.format(Registry.java:43)
	at student.Registry.main(Registry.java:65)
Java Result: 1


Thanks

This post has been edited by parry2411: 20 March 2013 - 12:56 AM

Was This Post Helpful? 0
  • +
  • -

#20 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Linked List - Deleting Element That User Specifies As A Parameter

Posted 20 March 2013 - 09:48 AM

Quote

Now how would i modify this so that if an ID is not found it brings a message to the user saying No Id Found

There are multiple ways. 2 that come first to mind are:

1. change the method so that it returns a boolean. If true is returned, it means the specified student ID was found and removed. If false is returned, the student was not found, not removed. The caller can determine what to do for both true and false.

2. if the student ID is found and removed, return immediately afterwards. otherwise the while loop completes and prints the message that the ID was not found and then returns.

Come back with your other questions and updated code.
Was This Post Helpful? 0
  • +
  • -

#21 parry2411   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 13-February 13

Re: Linked List - Deleting Element That User Specifies As A Parameter

Posted 20 March 2013 - 10:13 AM

View PostGregBrannon, on 20 March 2013 - 09:48 AM, said:

Quote

Now how would i modify this so that if an ID is not found it brings a message to the user saying No Id Found

There are multiple ways. 2 that come first to mind are:

1. change the method so that it returns a boolean. If true is returned, it means the specified student ID was found and removed. If false is returned, the student was not found, not removed. The caller can determine what to do for both true and false.

2. if the student ID is found and removed, return immediately afterwards. otherwise the while loop completes and prints the message that the ID was not found and then returns.

Come back with your other questions and updated code.


My method public void addStudent()

cant return anything , it has to be a null returned method.
Was This Post Helpful? 0
  • +
  • -

#22 GregBrannon   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Linked List - Deleting Element That User Specifies As A Parameter

Posted 20 March 2013 - 10:24 AM

Okay, so approach number 1 is out. Try approach number 2 which doesn't require changing the return type from void. A return statement following by nothing, as in:

return;

is allowed as an exit from a void method.
Was This Post Helpful? 0
  • +
  • -

#23 parry2411   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 13-February 13

Re: Linked List - Deleting Element That User Specifies As A Parameter

Posted 20 March 2013 - 10:27 AM

This is what i tried

 public void addStudent(Student aStudent)
    {
        while(iter.hasNext())
        {
            Student ob = iter.next();
            
            if(ob.getStudentID() == aStudent.getStudentID())
            {
                System.out.println("This Student ID Is Already Used");
            }
            else
            {
                studentList.addLast(aStudent);
            }
        }
    }


And it doesnt add an item to the list at all.

This post has been edited by parry2411: 20 March 2013 - 10:29 AM

Was This Post Helpful? 0
  • +
  • -

#24 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Linked List - Deleting Element That User Specifies As A Parameter

Posted 20 March 2013 - 12:21 PM

It will try (and fail) to add it at every entry that is not a duoplicate
you would need (assuming iter was correctly defined)
public void addStudent(Student aStudent)
   {
       while(iter.hasNext())
       {
           Student ob = iter.next();
           
           if(ob.getStudentID() == aStudent.getStudentID())
           {
               System.out.println("This Student ID Is Already Used");
               return;
           }
       }
       studentList.addLast(aStudent);
   }


This post has been edited by pbl: 20 March 2013 - 12:21 PM

Was This Post Helpful? 0
  • +
  • -

#25 parry2411   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 13-February 13

Re: Linked List - Deleting Element That User Specifies As A Parameter

Posted 20 March 2013 - 12:43 PM

View Postpbl, on 20 March 2013 - 12:21 PM, said:

It will try (and fail) to add it at every entry that is not a duoplicate
you would need (assuming iter was correctly defined)
public void addStudent(Student aStudent)
   {
       while(iter.hasNext())
       {
           Student ob = iter.next();
           
           if(ob.getStudentID() == aStudent.getStudentID())
           {
               System.out.println("This Student ID Is Already Used");
               return;
           }
       }
       studentList.addLast(aStudent);
   }




No This doesnt Work Either Error i get is:

run:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:953)
at java.util.LinkedList$ListItr.next(LinkedList.java:886)
at student.Registry.addStudent(Registry.java:26)
at student.Registry.main(Registry.java:50)
Java Result: 1


Here is all the code for Registry:

public class Registry
{

    public LinkedList<Student> studentList = new LinkedList<>();
    public Iterator<Student> iter = studentList.iterator();

    public Registry()
    {
    }

    public void addStudent(Student aStudent)
    {
        while (iter.hasNext())
        {
            Student ob = iter.next();

            if (ob.getStudentID() == aStudent.getStudentID())
            {
                System.out.println("This Student ID Is Already Used");
                return;
            }
        }
        studentList.addLast(aStudent);
    }

    public void deleteStudent(int studentID)
    {
     
    }

    public static void main(String[] args)
    {
        Student s1 = new Student("Sall", "Willian", 798);
        Student s2 = new Student("Sallie", "Jones", 980);

        Registry r = new Registry();

        r.addStudent(s1);
        r.addStudent(s2);

        System.out.println(r.studentList.size());
    }
}

Was This Post Helpful? 0
  • +
  • -

#26 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Linked List - Deleting Element That User Specifies As A Parameter

Posted 20 March 2013 - 12:51 PM

We do not have all the code
You have another Iterator running
Was This Post Helpful? 0
  • +
  • -

#27 parry2411   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 13-February 13

Re: Linked List - Deleting Element That User Specifies As A Parameter

Posted 20 March 2013 - 01:01 PM

View Postpbl, on 20 March 2013 - 12:51 PM, said:

We do not have all the code
You have another Iterator running


Yes thats all the code for the Registry Class

What i can do is though, Add the Iterator locally to the method, This works by doing This, BUT how would i go abouts on doing it without having more than one Iterator on the Class, Heres the updated code:

package student;

import java.util.Iterator;
import java.util.LinkedList;

public class Registry
{

    public LinkedList<Student> studentList = new LinkedList<>();
    public Iterator<Student> iter = studentList.iterator();

    public Registry()
    {
    }

    public void addStudent(Student aStudent)
    {
        Iterator<Student> addIterator = studentList.iterator();
        
        while (addIterator.hasNext())
        {
            Student ob = addIterator.next();
            if (ob.getStudentID() == aStudent.getStudentID())
            {
                System.out.println("This Student ID Is Already Used");
                return;
            }
        }
        studentList.addLast(aStudent);
    }

    public void deleteStudent(int studentID)
    {
     
    }

    public static void main(String[] args)
    {
        Student s1 = new Student("Sall", "Willian", 980);
        Student s2 = new Student("Sallie", "Jones", 980);

        Registry r = new Registry();

        r.addStudent(s1);
        

        System.out.println(r.studentList.size());
        
        r.addStudent(s2);
        
    }
}


Was This Post Helpful? 0
  • +
  • -

#28 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Linked List - Deleting Element That User Specifies As A Parameter

Posted 20 March 2013 - 01:06 PM

So it is somewhere else than in there Registry class that you have another Iterator open

You can't modify a Collection which has an active Iterator... that is why in my first reply I wrote

Quote

It will try (and fail) to add it at every entry that is not a duoplicate

because you were trying to add() on a Collection having an opened Iterator

Howver this works like a charm, may be you should create your Iterator in the method so it will vanish when the method exit
import java.util.*;

public class TestL {
	
	LinkedList<Stu> ll = new LinkedList<Stu>();
	TestL() {
		Stu a = new Stu(1);
		Stu b = new Stu(10);	
		ll.add(a);
		ll.add(B)/>;
		add(a);
		add(new Stu(5));
	}
	void add(Stu x) {
		Iterator<Stu> iter = ll.iterator();
		while(iter.hasNext()) {
			Stu y = iter.next();
			System.out.println("y = " + y.id);
			if(y.id == x.id) {
				System.out.println("Already there");
				return;
			}
		}
		ll.add(x);
	}
	class Stu {
		int id;
		Stu(int id) {
			this.id = id;
		}
	}
	
	public static void main(String[] args) {
		new TestL();
	}
}


Was This Post Helpful? 0
  • +
  • -

#29 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Linked List - Deleting Element That User Specifies As A Parameter

Posted 20 March 2013 - 01:11 PM

View Postparry2411, on 20 March 2013 - 04:01 PM, said:

Add the Iterator locally to the method, This works by doing This, BUT how would i go abouts on doing it without having more than one Iterator on the Class,


By definition Iterator are something with a short life that vanish when their job is done so no reason not to make it local to the method.
And as you just can't access a Collection which has an Iterator running but to remove the last element retreive by the operator no reason to have a "global" Iterator unless you flush it when done

	void add(Stu x) {
		while(iter.hasNext()) {
			Stu y = iter.next();
			System.out.println("y = " + y.id);
			if(y.id == x.id) {
				System.out.println("Already there");
				while(iter.hasNext())   // flush
				    iter.next();        // flush
				return;
			}



But this is kind of useless, because next time you will need that Iterator object you will need to iterator = linkedList.iterator(); anyhow

This post has been edited by pbl: 20 March 2013 - 01:14 PM

Was This Post Helpful? 0
  • +
  • -

#30 parry2411   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 49
  • Joined: 13-February 13

Re: Linked List - Deleting Element That User Specifies As A Parameter

Posted 20 March 2013 - 02:11 PM

So creating a new Iterator in every method i want to use it wont make any harm at all then?

Now how would i go abouts on doing the format() method, This should print out the contents of the LinkedList in this format:

Sammy Williams 890
Jamie Roberts 891

The method has to return a String so heres the method:

public String format()
{

}



How would i get this to do what i want?

Thanks
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3