Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Linked Lists

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Linked Lists

    Hi, i keep getting an error saying that NaN might not have been initialized. I think its because there is something else is wrong with my code, but i am not sure what it is. I just started using linked lists and any help would be really appreciated

    import java.lang.Double;
    public class LinkedListOfDoubles
    {
    private DoubleNode head;
    private DoubleNode tail;
    private int size;
    public static final double NaN;
    public LinkedListOfDoubles()
    {
    this.head = null;
    this.tail = null;
    this.size = 0;
    }

    public void addFront(double d)
    {
    if(head == null){
    DoubleNode newNode;
    newNode = new DoubleNode(d, head, null);

    newNode.setNext(head);
    head = newNode;
    tail = head;
    size++;
    }else{
    DoubleNode newNode;
    newNode = new DoubleNode(d, head, null);

    newNode.setNext(head);
    head = newNode;
    size++;
    }
    }

    public void addBack(double d)
    {
    if(tail == null){
    addFront(d);
    }else{

    DoubleNode newNode;
    newNode = new DoubleNode(d, null, tail);
    tail.setNext(newNode);
    tail = newNode;
    size++;
    }
    }

    public void remove(double d)
    {
    if(size()== 1){
    head = null;
    tail = null;
    size--;
    }else if(size > 1){
    for(DoubleNode x = head; x.next() != null; x.next())
    {
    if(x.getItem() == d)
    {
    x.setItem(x.next().getItem());
    size--;
    }
    }
    }
    }

    public void removeThreshold(double d, double threshold)
    {
    DoubleNode compare = head;
    double d1 = d + threshold;
    double d2 = d - threshold;
    if(compare.getItem() >= d2 && compare.getItem() <= d1 ){
    compare.setItem(compare.next().getItem());
    size--;
    }
    }

    public int size()
    {
    return size;
    }

    public int withinThreshold(double d, double threshold)
    {
    int count = 0;
    double d1 = d + threshold;
    double d2 = d - threshold;
    for(DoubleNode compare = head; compare.next() != null; compare = compare.next()){
    if(compare.getItem() >= d2 && compare.getItem() <= d1){
    count++;
    }
    compare = compare.next();
    }
    return count;
    }

    public double maximum()
    {
    DoubleNode compare = head;
    if(size >= 1){
    while(compare.next() != null){
    if(compare.next().getItem() > compare.getItem() || compare.next().getItem() == compare.getItem()){
    compare = compare.next();
    }
    }
    return compare.getItem();
    }else{
    return Double.NaN;
    }
    }

    public double minimum()
    {
    DoubleNode compare = head;
    if(size >= 1){
    while(compare.next() != null){
    if(compare.next().getItem() > compare.getItem() || compare.next ().getItem() == compare.getItem()){
    compare = compare.next();
    }
    }
    return compare.getItem();
    }else{
    return Double.NaN;
    }
    }

    public double mean()
    {
    if(size() == 0){
    return Double.NaN;
    }else{
    double mean = 0.0;
    DoubleNode curr = head;
    mean += curr.getItem();
    if (size >= 1){
    curr = curr.next();
    mean += curr.getItem();
    }
    return (mean / size);
    }
    }

    public double elementAt(int index)
    {
    DoubleNode temp;
    temp = head;
    if(index >= 0 && index < size())
    {
    for(int i =0; i < index; i++)
    {
    temp = temp.next();
    }
    return temp.getItem();
    }

    return Double.NaN;
    }

    public String toString()
    {
    DoubleNode curr = head;
    String list = "{";
    list += curr;
    while(curr == null){
    list += ", ";
    curr = curr.next();
    list += curr;
    }
    list += "}";
    return list;
    }

    }


  2. #2
    Member
    Join Date
    Oct 2010
    Posts
    38
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Linked Lists

    It's because you have the line:

    private static double NaN;

    Then the value for that is never set and you start to return that value even though you never set it to anything. Also please use the CODE tags next time

  3. The Following User Says Thank You to sp11k3t3ht3rd For This Useful Post:

    lieles (February 6th, 2011)

Similar Threads

  1. Array Lists help!!
    By lilika in forum What's Wrong With My Code?
    Replies: 17
    Last Post: January 4th, 2011, 09:21 AM
  2. Linked Lists
    By Jnoobs in forum Java Theory & Questions
    Replies: 1
    Last Post: October 23rd, 2010, 04:09 PM
  3. Problems in linked lists
    By Hotzero in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 5th, 2010, 09:25 AM
  4. iterators & linked lists in java question
    By somewhat_confused in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 4th, 2010, 10:59 AM
  5. Constructors, Hash Tables, & Linked Lists
    By illusion887 in forum Collections and Generics
    Replies: 2
    Last Post: December 3rd, 2009, 03:46 AM