First make your list as a LinkedList not a List
an make it an instance variable not just part of the contructor
CODE
import java.util.*;
public class NamesList
{
private static final String names[] = {"Arron", "Arron", "Jack", "Jack", "Jill", "Jill", "Brian", "Beth", "Donny", "Samantha", "Alex", "Thomas"};
LinkedList< String > list = new LinkedList< String >();
public NamesList()
{
for(String first : names)
list.add(first);
// now you do nota have to pass the LinkedList as argument to printList
printList();
}
public void printList()
{
System.out.println("\nList: ");
for(String first : newList)
System.out.printf("%s", first);
System.out.println();
}
}
Now you can write your own add() method that checks if the elemet is already there
CODE
// method to add a new String to the list return false if duplicate
boolean add(String toInsert) {
for(String member : list) {
if(member.equals(toInsert)) // String already in list
return false;
}
// so not already in the linkedList
list.add(toInsert);
return true;
}
Edited to correct a typo printList() not printList(1
This post has been edited by pbl: 8 May, 2008 - 08:22 PM