1

I need to search a LinkedList and compare the objects in the list so I don't add duplicates. I can't figure out how to pull a node so I can compare it to the StudentInfo object I'm trying to add. This is my attempt to use an iterator:

private LinkedList<CourseInfo> classes = new LinkedList<>();
 public void addCourse(String cid, String name, String prof, String days, 
 String time, String room)
 {
 CourseInfo course = new CourseInfo(cid, name, prof, days, time, room);
 Iterator i = classes.iterator();
 while(i.hasNext())
 {
 if(i.equals(course))
 {
 System.out.println("Caught");
 break;
 }
 }
 }

I specifically need to compare the cid variables

asked Jun 18, 2018 at 23:12
2
  • Don't want duplicates: use HashSet. Find an object: list.contains(...). Commented Jun 18, 2018 at 23:26
  • 2
    Did you override equals() and hashcode() in your CourseInfo class? Commented Jun 18, 2018 at 23:38

1 Answer 1

2

Minor note, traversing a LinkedList to check if an element exists is expensive. You might want to consider HashSet. That being said, in your code you are comparing the iterator with the CourseInfo, you need to compare the element pointed by the iterator instead by using next(). Also, next() is required in order to advance the iterator (thanks to Code-Apprentice for the suggestion) otherwise you could end up in an infinite loop in i.hasNext().

private LinkedList<CourseInfo> classes = new LinkedList<>();
 public void addCourse(String cid, String name, String prof, String days, 
 String time, String room)
 {
 CourseInfo course = new CourseInfo(cid, name, prof, days, time, room);
 // Don't use raw types e.g. Iterator it
 // Specify the type of element you are iterating on
 // Iterator<T> it
 Iterator<CourseInfo> i = classes.iterator();
 while(i.hasNext())
 {
 CourseInfo cInfo = i.next(); // get the element pointed by the iterator
 if(cInfo.equals(course))
 {
 System.out.println("Caught");
 break;
 }
 }
 }

Or using enhanced for-loops:

for(CourseInfo ci : classes) {
 if(ci.equals(course)) {
 System.out.println("Caught");
 break;
 }
}
answered Jun 18, 2018 at 23:19
10
  • it says incompatible types: Object cannot be converted to CourseInfo Commented Jun 18, 2018 at 23:23
  • 1
    tmp is probably the worst imaginable variable name. Commented Jun 18, 2018 at 23:36
  • 1
    Note that the call to next() is required in order to advance the iterator. The original while loop would continue indefinitely without this. Commented Jun 18, 2018 at 23:40
  • 2
    @I.McFee You should edit your question with an update so that you can format the code correctly. This answer assumes that you already overrode the equals() and hashcode() method correctly. Commented Jun 18, 2018 at 23:52
  • 1
    @I.McFee are you overriding equals & hashcode for CourseInfo? Commented Jun 18, 2018 at 23:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.