Array-based unordered list implementation in Java

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












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();








share|improve this question



























    up vote
    3
    down vote

    favorite












    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();








    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      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();








      share|improve this question













      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();










      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 8 at 2:24









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked Jun 8 at 2:21









      Hamidur Rahman

      436




      436




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote














          • locate never throws, so what find expects to catch? Consider



            public boolean find(int data) 
            return locate(data) >= 0;




          • locate is overcomplicated. Consider



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


            return -1;



          • reset, hasNext, and next do not belong to the list class; they are the methods of a list iterator. For starters, notice that your implementation only allows one list traversal at a time.



            That said, the semantics of hasNext() is wrong. The condition currentIndex == size() means that there is no next. A client of your class would be badly confused.



            Along the same line, next() should not silently ignore the end-of-list condition.







          share|improve this answer





















          • how do I separate Iterator class from this class? Any demonstration or link to this solution would be very helpful. @vnp
            – Hamidur Rahman
            Jun 8 at 19:06











          • @HamidurRahman I don't know Java conventions, but normally you'd define an iterator class holding currentIndex, with next and hasNext methods, and pass your list to its constructor,
            – vnp
            Jun 8 at 19:09










          • Thanks. I will look for it and find the solution and then post it in this comment section. Is that okay? @vnp
            – Hamidur Rahman
            Jun 8 at 19:12










          • @HamidurRahman Better post a follow-up question.
            – vnp
            Jun 8 at 19:13

















          up vote
          2
          down vote













          I would recommend using Log4j instead of System.out.println() with some meaningful message to the user. Also, you can implement locate() method as mentioned below to avoid initializing the flag



          private int locate(int data)

          int i = 0;
          int index = -1;
          while (i < size())

          if (data == nodeElements[i].getData())

          index = i;
          break;

          i++;

          return index;



          Change: Use break; to come out of the loop rather than using flag to check it.






          share|improve this answer























          • Please summarize the changes you made. Code only answers are considered off-topic.
            – t3chb0t
            Jun 8 at 6:50






          • 1




            @t3chb0t, done:).
            – Stack Victor
            Jun 8 at 7:27










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196083%2farray-based-unordered-list-implementation-in-java%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote














          • locate never throws, so what find expects to catch? Consider



            public boolean find(int data) 
            return locate(data) >= 0;




          • locate is overcomplicated. Consider



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


            return -1;



          • reset, hasNext, and next do not belong to the list class; they are the methods of a list iterator. For starters, notice that your implementation only allows one list traversal at a time.



            That said, the semantics of hasNext() is wrong. The condition currentIndex == size() means that there is no next. A client of your class would be badly confused.



            Along the same line, next() should not silently ignore the end-of-list condition.







          share|improve this answer





















          • how do I separate Iterator class from this class? Any demonstration or link to this solution would be very helpful. @vnp
            – Hamidur Rahman
            Jun 8 at 19:06











          • @HamidurRahman I don't know Java conventions, but normally you'd define an iterator class holding currentIndex, with next and hasNext methods, and pass your list to its constructor,
            – vnp
            Jun 8 at 19:09










          • Thanks. I will look for it and find the solution and then post it in this comment section. Is that okay? @vnp
            – Hamidur Rahman
            Jun 8 at 19:12










          • @HamidurRahman Better post a follow-up question.
            – vnp
            Jun 8 at 19:13














          up vote
          2
          down vote














          • locate never throws, so what find expects to catch? Consider



            public boolean find(int data) 
            return locate(data) >= 0;




          • locate is overcomplicated. Consider



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


            return -1;



          • reset, hasNext, and next do not belong to the list class; they are the methods of a list iterator. For starters, notice that your implementation only allows one list traversal at a time.



            That said, the semantics of hasNext() is wrong. The condition currentIndex == size() means that there is no next. A client of your class would be badly confused.



            Along the same line, next() should not silently ignore the end-of-list condition.







          share|improve this answer





















          • how do I separate Iterator class from this class? Any demonstration or link to this solution would be very helpful. @vnp
            – Hamidur Rahman
            Jun 8 at 19:06











          • @HamidurRahman I don't know Java conventions, but normally you'd define an iterator class holding currentIndex, with next and hasNext methods, and pass your list to its constructor,
            – vnp
            Jun 8 at 19:09










          • Thanks. I will look for it and find the solution and then post it in this comment section. Is that okay? @vnp
            – Hamidur Rahman
            Jun 8 at 19:12










          • @HamidurRahman Better post a follow-up question.
            – vnp
            Jun 8 at 19:13












          up vote
          2
          down vote










          up vote
          2
          down vote










          • locate never throws, so what find expects to catch? Consider



            public boolean find(int data) 
            return locate(data) >= 0;




          • locate is overcomplicated. Consider



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


            return -1;



          • reset, hasNext, and next do not belong to the list class; they are the methods of a list iterator. For starters, notice that your implementation only allows one list traversal at a time.



            That said, the semantics of hasNext() is wrong. The condition currentIndex == size() means that there is no next. A client of your class would be badly confused.



            Along the same line, next() should not silently ignore the end-of-list condition.







          share|improve this answer














          • locate never throws, so what find expects to catch? Consider



            public boolean find(int data) 
            return locate(data) >= 0;




          • locate is overcomplicated. Consider



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


            return -1;



          • reset, hasNext, and next do not belong to the list class; they are the methods of a list iterator. For starters, notice that your implementation only allows one list traversal at a time.



            That said, the semantics of hasNext() is wrong. The condition currentIndex == size() means that there is no next. A client of your class would be badly confused.



            Along the same line, next() should not silently ignore the end-of-list condition.








          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Jun 8 at 6:48









          vnp

          36.4k12890




          36.4k12890











          • how do I separate Iterator class from this class? Any demonstration or link to this solution would be very helpful. @vnp
            – Hamidur Rahman
            Jun 8 at 19:06











          • @HamidurRahman I don't know Java conventions, but normally you'd define an iterator class holding currentIndex, with next and hasNext methods, and pass your list to its constructor,
            – vnp
            Jun 8 at 19:09










          • Thanks. I will look for it and find the solution and then post it in this comment section. Is that okay? @vnp
            – Hamidur Rahman
            Jun 8 at 19:12










          • @HamidurRahman Better post a follow-up question.
            – vnp
            Jun 8 at 19:13
















          • how do I separate Iterator class from this class? Any demonstration or link to this solution would be very helpful. @vnp
            – Hamidur Rahman
            Jun 8 at 19:06











          • @HamidurRahman I don't know Java conventions, but normally you'd define an iterator class holding currentIndex, with next and hasNext methods, and pass your list to its constructor,
            – vnp
            Jun 8 at 19:09










          • Thanks. I will look for it and find the solution and then post it in this comment section. Is that okay? @vnp
            – Hamidur Rahman
            Jun 8 at 19:12










          • @HamidurRahman Better post a follow-up question.
            – vnp
            Jun 8 at 19:13















          how do I separate Iterator class from this class? Any demonstration or link to this solution would be very helpful. @vnp
          – Hamidur Rahman
          Jun 8 at 19:06





          how do I separate Iterator class from this class? Any demonstration or link to this solution would be very helpful. @vnp
          – Hamidur Rahman
          Jun 8 at 19:06













          @HamidurRahman I don't know Java conventions, but normally you'd define an iterator class holding currentIndex, with next and hasNext methods, and pass your list to its constructor,
          – vnp
          Jun 8 at 19:09




          @HamidurRahman I don't know Java conventions, but normally you'd define an iterator class holding currentIndex, with next and hasNext methods, and pass your list to its constructor,
          – vnp
          Jun 8 at 19:09












          Thanks. I will look for it and find the solution and then post it in this comment section. Is that okay? @vnp
          – Hamidur Rahman
          Jun 8 at 19:12




          Thanks. I will look for it and find the solution and then post it in this comment section. Is that okay? @vnp
          – Hamidur Rahman
          Jun 8 at 19:12












          @HamidurRahman Better post a follow-up question.
          – vnp
          Jun 8 at 19:13




          @HamidurRahman Better post a follow-up question.
          – vnp
          Jun 8 at 19:13












          up vote
          2
          down vote













          I would recommend using Log4j instead of System.out.println() with some meaningful message to the user. Also, you can implement locate() method as mentioned below to avoid initializing the flag



          private int locate(int data)

          int i = 0;
          int index = -1;
          while (i < size())

          if (data == nodeElements[i].getData())

          index = i;
          break;

          i++;

          return index;



          Change: Use break; to come out of the loop rather than using flag to check it.






          share|improve this answer























          • Please summarize the changes you made. Code only answers are considered off-topic.
            – t3chb0t
            Jun 8 at 6:50






          • 1




            @t3chb0t, done:).
            – Stack Victor
            Jun 8 at 7:27














          up vote
          2
          down vote













          I would recommend using Log4j instead of System.out.println() with some meaningful message to the user. Also, you can implement locate() method as mentioned below to avoid initializing the flag



          private int locate(int data)

          int i = 0;
          int index = -1;
          while (i < size())

          if (data == nodeElements[i].getData())

          index = i;
          break;

          i++;

          return index;



          Change: Use break; to come out of the loop rather than using flag to check it.






          share|improve this answer























          • Please summarize the changes you made. Code only answers are considered off-topic.
            – t3chb0t
            Jun 8 at 6:50






          • 1




            @t3chb0t, done:).
            – Stack Victor
            Jun 8 at 7:27












          up vote
          2
          down vote










          up vote
          2
          down vote









          I would recommend using Log4j instead of System.out.println() with some meaningful message to the user. Also, you can implement locate() method as mentioned below to avoid initializing the flag



          private int locate(int data)

          int i = 0;
          int index = -1;
          while (i < size())

          if (data == nodeElements[i].getData())

          index = i;
          break;

          i++;

          return index;



          Change: Use break; to come out of the loop rather than using flag to check it.






          share|improve this answer















          I would recommend using Log4j instead of System.out.println() with some meaningful message to the user. Also, you can implement locate() method as mentioned below to avoid initializing the flag



          private int locate(int data)

          int i = 0;
          int index = -1;
          while (i < size())

          if (data == nodeElements[i].getData())

          index = i;
          break;

          i++;

          return index;



          Change: Use break; to come out of the loop rather than using flag to check it.







          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jun 8 at 7:27


























          answered Jun 8 at 6:18









          Stack Victor

          1294




          1294











          • Please summarize the changes you made. Code only answers are considered off-topic.
            – t3chb0t
            Jun 8 at 6:50






          • 1




            @t3chb0t, done:).
            – Stack Victor
            Jun 8 at 7:27
















          • Please summarize the changes you made. Code only answers are considered off-topic.
            – t3chb0t
            Jun 8 at 6:50






          • 1




            @t3chb0t, done:).
            – Stack Victor
            Jun 8 at 7:27















          Please summarize the changes you made. Code only answers are considered off-topic.
          – t3chb0t
          Jun 8 at 6:50




          Please summarize the changes you made. Code only answers are considered off-topic.
          – t3chb0t
          Jun 8 at 6:50




          1




          1




          @t3chb0t, done:).
          – Stack Victor
          Jun 8 at 7:27




          @t3chb0t, done:).
          – Stack Victor
          Jun 8 at 7:27












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f196083%2farray-based-unordered-list-implementation-in-java%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?