6 Replies - 1411 Views - Last Post: 08 June 2018 - 03:39 PM Rate Topic: -----

#1 Hamidur   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-May 17

Array-based unordered list implementation in Java.

Posted 07 June 2018 - 09:47 PM

This class, UnorderedList, implements an Unordered list in Java. In this class, duplicates are allowed. remove(argument) will delete the first occurrence of the argument. I was wondering to add other functions named add(int index, int data) and set(int index, int data) or not. Since the class is unordered, I thought I would not need it. The size of the class is limited although that does not concern me (UnorderedLinkedList coming up next).

I tried to make sure all the functionality and behaviours are correct. If this class is lacking any method(s) and/or correct usage of exception and anything else please comment below. Any kind of suggestions and advice is appreciated.

//Node class

package lists.arrayLists.unorderedList;

public class Node
{
    private int data;

    public Node(int data)
    {
        this.data = data;
    }

    public int getData()
    {
        return data;
    }
}

//UnorderedList class

package lists.arrayLists.unorderedList;

import java.lang.IndexOutOfBoundsException;
import java.util.NoSuchElementException;

public class UnorderedList
{
    public static final int MAX_SIZE = 100;
    private int size;
    private Node[] nodeElements;
    private int currentIndex;                           // to navigate/iterate over list

    public UnorderedList()
    {
        nodeElements = new Node[MAX_SIZE];
    }

    public boolean isFull()
    {
        return (size() == MAX_SIZE);
    }

    public boolean isEmpty()
    {
        return (size() == 0);
    }

    public int size()
    {
        return size;
    }

    public boolean add(int data)
    {
        try
        {
            Node node = new Node(data);
            nodeElements[size++] = node;
            return true;
        }
        catch (IndexOutOfBoundsException ex)
        {
            System.out.println(ex.getMessage());
            throw new IndexOutOfBoundsException();
        }
    }

    public void remove(int data)
    {
        int index = locate(data);
        try
        {
            nodeElements[index] = nodeElements[size() - 1];
            size--;
        }
        catch (IndexOutOfBoundsException ex)
        {
            throw new NoSuchElementException();
        }
    }

    public boolean find(int data)
    {
        boolean found = false;
        try
        {
            if(locate(data) >= 0)
            {
                found = true;
            }
            return found;
        }
        catch (NoSuchElementException ex)
        {
            throw new NoSuchElementException();
        }
    }

    // locate function looks for an element in the array, if there is one then returns its index
    private int locate(int data)
    {
        int i = 0;
        int index = -1;
        boolean found = false;
        while (i < size() && !found)
        {
            if (data == nodeElements[i].getData())
            {
                found = true;
                index = i;
            }
            i++;
        }
        return index;
     }

    //Navigator methods

    public void reset()
    {
        currentIndex = 0;
    }

    public boolean hasNext()
    {
        return (currentIndex == size());
    }

    public void next()
    {
        if (!hasNext()) currentIndex++;
    }

    public int currentElement()
    {
        return nodeElements[currentIndex].getData();
    }
}




Is This A Good Question/Topic? 0
  • +

Replies To: Array-based unordered list implementation in Java.

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 712
  • View blog
  • Posts: 5,466
  • Joined: 25-December 13

Re: Array-based unordered list implementation in Java.

Posted 08 June 2018 - 05:59 AM

Do you have a program to test the different features of your class? That would be a requirement.
Was This Post Helpful? 0
  • +
  • -

#3 Hamidur   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-May 17

Re: Array-based unordered list implementation in Java.

Posted 08 June 2018 - 12:02 PM

View PostNormR, on 08 June 2018 - 05:59 AM, said:

Do you have a program to test the different features of your class? That would be a requirement.


Yes. I tested it is working. Though I have a question how do I separate iterator methods from the UnorderedList class?
Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 712
  • View blog
  • Posts: 5,466
  • Joined: 25-December 13

Re: Array-based unordered list implementation in Java.

Posted 08 June 2018 - 12:45 PM

Quote

how do I separate iterator methods from the UnorderedList class?

Sorry, I do not see an iterator method in the class.
What method(s) are you asking about?
Was This Post Helpful? 0
  • +
  • -

#5 Hamidur   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-May 17

Re: Array-based unordered list implementation in Java.

Posted 08 June 2018 - 12:48 PM

I used reset(), hasNext(), next() to iterate but I donít want these methods inside this class.
Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 712
  • View blog
  • Posts: 5,466
  • Joined: 25-December 13

Re: Array-based unordered list implementation in Java.

Posted 08 June 2018 - 01:07 PM

Do you want to have an Iterator class for your class?
See the API doc for the List interface and the Iterator class to see what is required.
Was This Post Helpful? 0
  • +
  • -

#7 Hamidur   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-May 17

Re: Array-based unordered list implementation in Java.

Posted 08 June 2018 - 03:39 PM

View PostNormR, on 08 June 2018 - 01:07 PM, said:

Do you want to have an Iterator class for your class?
See the API doc for the List interface and the Iterator class to see what is required.


I think I figured out what I needed. This is the final version so far I have made. If have time please have a look at it. Thanks.


//List Interface

package lists.arrayLists;

public interface List<T>
{
    boolean isFull();
    boolean isEmpty();
    boolean add(T data);
    void remove(T data);
    boolean find(T data);
    int size();
}




// Node class

package lists.arrayLists.unorderedList;

public class Node<T>
{
    private T data;

    public Node(T data)
    {
        this.data = data;
    }

    public T getData()
    {
        return data;
    }
}




// UnorderedList Class

package lists.arrayLists.unorderedList;

import lists.arrayLists.List;

import java.lang.IndexOutOfBoundsException;
import java.util.Iterator;
import java.util.NoSuchElementException;

public class UnorderedList<T> implements List<T>, Iterable<T>
{
    private static int MAX_SIZE = 100;
    private int size;
    private Node<T>[] nodeElements;

    public UnorderedList()
    {
        nodeElements = new Node[MAX_SIZE];
    }

    public UnorderedList(int capacity)
    {
        MAX_SIZE = capacity;
        nodeElements = new Node[MAX_SIZE];
    }

    @Override
    public boolean isFull()
    {
        return (size() == MAX_SIZE);
    }

    @Override
    public boolean isEmpty()
    {
        return (size() == 0);
    }

    @Override
    public int size()
    {
        return size;
    }

    @Override
    public boolean add(T data)
    {
        try
        {
            Node<T> node = new Node<>(data);
            nodeElements[size++] = node;
            return true;
        }
        catch (IndexOutOfBoundsException ex)
        {
            System.out.println(ex.getMessage());
            throw new IndexOutOfBoundsException();
        }
    }

    @Override
    public void remove(T data)
    {
        int index = locate(data);
        try
        {
            nodeElements[index] = nodeElements[size() - 1];
            size--;
        }
        catch (IndexOutOfBoundsException ex)
        {
            throw new NoSuchElementException();
        }
    }

    @Override
    public boolean find(T data)
    {
        return (locate(data) >= 0);
    }

    private int locate(T data)
    {
        for (int index = 0; index < size(); index++)
        {
            if (data == nodeElements[index].getData())
            {
                return index;
            }
        }
        return - 1;
    }

    @Override
    public Iterator<T> iterator()
    {
        return new ULIterator<T>();
    }

    private class ULIterator<T> implements Iterator<T>
    {
        private int currentIndex = 0;

        @Override
        public boolean hasNext()
        {
            return (currentIndex < size());
        }

        @Override
        public T next()
        {
            if(hasNext())
            {
                return (T)nodeElements[currentIndex++].getData();
            }
            else
            {
                return null;
            }
        }
    }
}




// Tester class working fine

package lists;

import lists.arrayLists.List;
import lists.arrayLists.unorderedList.UnorderedList;
import java.util.Iterator;

public class Tester
{
    public static void main(String[] args)
    {
        List<Integer> ul = new UnorderedList<>();

        System.out.println(ul.isEmpty() + " " + ul.isFull());

        ul.add(6);
        ul.add(7);
        ul.add(8);
        ul.add(9);
        ul.add(10);
        ul.add(11);

        Iterator<Integer> itr = ((UnorderedList) ul).iterator();

        while (itr.hasNext())
        {
            Object e = itr.next();
            System.out.println(e);
        }

    }

}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1