Stack Array implementation of Java











up vote
1
down vote

favorite












This is my array implementation on stack in Java. It involves push, pop, get and growth when limit of array size is reached.



I am a self-taught programmer starting to learn data structures and algorithms.



I am looking for




  1. any recommendation on conventions

  2. an honest opinion on how bad this piece of code if this is an interview.

  3. anything wrong and room for improvement.


I wrote this code myself, not looking at any other similar questions on Code Review.



public class stackAlist{
int data;
int size;
static int growthmultipler = 1;

public stackAlist(){
data = new int[2*growthmultipler];
size = 0;
}

public void push(int value){
if(this.size == 0){
this.data[0] = value;
this.size += 1;
this.growthmultipler = 1;
}
else if(this.size == 2*growthmultipler){
growthmultipler += 1;
stackAlist newlist = new stackAlist();
newlist.data = new int[2*growthmultipler];
System.arraycopy(this.data, 0, newlist.data, 0, this.size);
this.data = newlist.data;
this.data[size] = value;
this.size += 1;
}
else{
this.data[size] = value;
this.size += 1;
}
}

public void pop(){
this.data[size-1] = 0;
this.size = this.size-1;
}

public void get(){
int i;
for(i =0; i < this.size; i++){
System.out.println(data[i]);
}
}

public static void main(String args){
stackAlist a = new stackAlist();
a.push(1);
a.push(2);
a.get();
a.pop();
a.get();
a.push(3);
a.push(4);
a.get();
}
}









share|improve this question









New contributor




Carch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    1
    down vote

    favorite












    This is my array implementation on stack in Java. It involves push, pop, get and growth when limit of array size is reached.



    I am a self-taught programmer starting to learn data structures and algorithms.



    I am looking for




    1. any recommendation on conventions

    2. an honest opinion on how bad this piece of code if this is an interview.

    3. anything wrong and room for improvement.


    I wrote this code myself, not looking at any other similar questions on Code Review.



    public class stackAlist{
    int data;
    int size;
    static int growthmultipler = 1;

    public stackAlist(){
    data = new int[2*growthmultipler];
    size = 0;
    }

    public void push(int value){
    if(this.size == 0){
    this.data[0] = value;
    this.size += 1;
    this.growthmultipler = 1;
    }
    else if(this.size == 2*growthmultipler){
    growthmultipler += 1;
    stackAlist newlist = new stackAlist();
    newlist.data = new int[2*growthmultipler];
    System.arraycopy(this.data, 0, newlist.data, 0, this.size);
    this.data = newlist.data;
    this.data[size] = value;
    this.size += 1;
    }
    else{
    this.data[size] = value;
    this.size += 1;
    }
    }

    public void pop(){
    this.data[size-1] = 0;
    this.size = this.size-1;
    }

    public void get(){
    int i;
    for(i =0; i < this.size; i++){
    System.out.println(data[i]);
    }
    }

    public static void main(String args){
    stackAlist a = new stackAlist();
    a.push(1);
    a.push(2);
    a.get();
    a.pop();
    a.get();
    a.push(3);
    a.push(4);
    a.get();
    }
    }









    share|improve this question









    New contributor




    Carch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      This is my array implementation on stack in Java. It involves push, pop, get and growth when limit of array size is reached.



      I am a self-taught programmer starting to learn data structures and algorithms.



      I am looking for




      1. any recommendation on conventions

      2. an honest opinion on how bad this piece of code if this is an interview.

      3. anything wrong and room for improvement.


      I wrote this code myself, not looking at any other similar questions on Code Review.



      public class stackAlist{
      int data;
      int size;
      static int growthmultipler = 1;

      public stackAlist(){
      data = new int[2*growthmultipler];
      size = 0;
      }

      public void push(int value){
      if(this.size == 0){
      this.data[0] = value;
      this.size += 1;
      this.growthmultipler = 1;
      }
      else if(this.size == 2*growthmultipler){
      growthmultipler += 1;
      stackAlist newlist = new stackAlist();
      newlist.data = new int[2*growthmultipler];
      System.arraycopy(this.data, 0, newlist.data, 0, this.size);
      this.data = newlist.data;
      this.data[size] = value;
      this.size += 1;
      }
      else{
      this.data[size] = value;
      this.size += 1;
      }
      }

      public void pop(){
      this.data[size-1] = 0;
      this.size = this.size-1;
      }

      public void get(){
      int i;
      for(i =0; i < this.size; i++){
      System.out.println(data[i]);
      }
      }

      public static void main(String args){
      stackAlist a = new stackAlist();
      a.push(1);
      a.push(2);
      a.get();
      a.pop();
      a.get();
      a.push(3);
      a.push(4);
      a.get();
      }
      }









      share|improve this question









      New contributor




      Carch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      This is my array implementation on stack in Java. It involves push, pop, get and growth when limit of array size is reached.



      I am a self-taught programmer starting to learn data structures and algorithms.



      I am looking for




      1. any recommendation on conventions

      2. an honest opinion on how bad this piece of code if this is an interview.

      3. anything wrong and room for improvement.


      I wrote this code myself, not looking at any other similar questions on Code Review.



      public class stackAlist{
      int data;
      int size;
      static int growthmultipler = 1;

      public stackAlist(){
      data = new int[2*growthmultipler];
      size = 0;
      }

      public void push(int value){
      if(this.size == 0){
      this.data[0] = value;
      this.size += 1;
      this.growthmultipler = 1;
      }
      else if(this.size == 2*growthmultipler){
      growthmultipler += 1;
      stackAlist newlist = new stackAlist();
      newlist.data = new int[2*growthmultipler];
      System.arraycopy(this.data, 0, newlist.data, 0, this.size);
      this.data = newlist.data;
      this.data[size] = value;
      this.size += 1;
      }
      else{
      this.data[size] = value;
      this.size += 1;
      }
      }

      public void pop(){
      this.data[size-1] = 0;
      this.size = this.size-1;
      }

      public void get(){
      int i;
      for(i =0; i < this.size; i++){
      System.out.println(data[i]);
      }
      }

      public static void main(String args){
      stackAlist a = new stackAlist();
      a.push(1);
      a.push(2);
      a.get();
      a.pop();
      a.get();
      a.push(3);
      a.push(4);
      a.get();
      }
      }






      java algorithm stack






      share|improve this question









      New contributor




      Carch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Carch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 2 days ago









      Toby Speight

      22.2k536108




      22.2k536108






      New contributor




      Carch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 days ago









      Carch

      82




      82




      New contributor




      Carch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Carch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Carch is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          First of all, you are using a static variable (growthmultipler) inside the instance of a class. This is fatal if you have more than one instance of this class. Because you always use 2*growthmultipler, you don't need it. Use +=2 instead.



          You don't need to prealloc data in the constructor. It will be done if it's needed.



          You don't need to handle "size == 0" separately. You only have to handle "size >= length".



          You don't have to create a new class if you want to grow data.



          In pop() you should test to not get negative (and throw an exception). And normally a pop function returns the value.



          In summary it might look like this:



          public class stackAlist{
          int data;
          int size;

          public stackAlist(){
          size = 0;
          data = new int[size];
          }

          public void push(int value){
          if(size>=data.length) {
          int ndata = new int[data.length+2];
          System.arraycopy(data, 0, ndata, 0, size);
          data = ndata;
          }
          data[size] = value;
          size += 1;
          }

          public int pop() {
          int ret=0;
          if(size>0) {
          size -= 1;
          ret = data[size];
          data[size] = 0;
          }
          return ret;
          }
          .....





          share|improve this answer























          • Exceptions exist for a reason; the use of a "special" return value should be well documented.
            – Solomon Ucko
            2 days ago










          • I do think it should fail early, though.
            – Solomon Ucko
            2 days ago


















          up vote
          0
          down vote













          A couple things to add:




          1. Classes should use CamelCase and should preferably have descriptive names. stackAlist would be better named ArrayStack.

          2. You might want to have a Stack interface that ArrayStack implements.

          3. You might want to extract the resizing code into a separate method, and optionally make it public and/or take parameters.

          4. In your pop method, there's no need to zero out the popped items.






          share|improve this answer





















          • Thank you for all answers. I am kind of stuck at thinking about why I need a Stack interface that ArrayStack implements. Because I don't know why, I can't think of how to do it. An Interface should be used when there are multiple classes/data types involved. Are you saying that I should make ArrayStack generic so that it can accept all data types? Kinda confused here, please help, thanks.
            – Carch
            yesterday










          • Generics might be good too. Also, having an interface isn't necessary, but it can allow you to decouple it from code that uses it.
            – Solomon Ucko
            yesterday











          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: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Carch is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207968%2fstack-array-implementation-of-java%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          First of all, you are using a static variable (growthmultipler) inside the instance of a class. This is fatal if you have more than one instance of this class. Because you always use 2*growthmultipler, you don't need it. Use +=2 instead.



          You don't need to prealloc data in the constructor. It will be done if it's needed.



          You don't need to handle "size == 0" separately. You only have to handle "size >= length".



          You don't have to create a new class if you want to grow data.



          In pop() you should test to not get negative (and throw an exception). And normally a pop function returns the value.



          In summary it might look like this:



          public class stackAlist{
          int data;
          int size;

          public stackAlist(){
          size = 0;
          data = new int[size];
          }

          public void push(int value){
          if(size>=data.length) {
          int ndata = new int[data.length+2];
          System.arraycopy(data, 0, ndata, 0, size);
          data = ndata;
          }
          data[size] = value;
          size += 1;
          }

          public int pop() {
          int ret=0;
          if(size>0) {
          size -= 1;
          ret = data[size];
          data[size] = 0;
          }
          return ret;
          }
          .....





          share|improve this answer























          • Exceptions exist for a reason; the use of a "special" return value should be well documented.
            – Solomon Ucko
            2 days ago










          • I do think it should fail early, though.
            – Solomon Ucko
            2 days ago















          up vote
          0
          down vote



          accepted










          First of all, you are using a static variable (growthmultipler) inside the instance of a class. This is fatal if you have more than one instance of this class. Because you always use 2*growthmultipler, you don't need it. Use +=2 instead.



          You don't need to prealloc data in the constructor. It will be done if it's needed.



          You don't need to handle "size == 0" separately. You only have to handle "size >= length".



          You don't have to create a new class if you want to grow data.



          In pop() you should test to not get negative (and throw an exception). And normally a pop function returns the value.



          In summary it might look like this:



          public class stackAlist{
          int data;
          int size;

          public stackAlist(){
          size = 0;
          data = new int[size];
          }

          public void push(int value){
          if(size>=data.length) {
          int ndata = new int[data.length+2];
          System.arraycopy(data, 0, ndata, 0, size);
          data = ndata;
          }
          data[size] = value;
          size += 1;
          }

          public int pop() {
          int ret=0;
          if(size>0) {
          size -= 1;
          ret = data[size];
          data[size] = 0;
          }
          return ret;
          }
          .....





          share|improve this answer























          • Exceptions exist for a reason; the use of a "special" return value should be well documented.
            – Solomon Ucko
            2 days ago










          • I do think it should fail early, though.
            – Solomon Ucko
            2 days ago













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          First of all, you are using a static variable (growthmultipler) inside the instance of a class. This is fatal if you have more than one instance of this class. Because you always use 2*growthmultipler, you don't need it. Use +=2 instead.



          You don't need to prealloc data in the constructor. It will be done if it's needed.



          You don't need to handle "size == 0" separately. You only have to handle "size >= length".



          You don't have to create a new class if you want to grow data.



          In pop() you should test to not get negative (and throw an exception). And normally a pop function returns the value.



          In summary it might look like this:



          public class stackAlist{
          int data;
          int size;

          public stackAlist(){
          size = 0;
          data = new int[size];
          }

          public void push(int value){
          if(size>=data.length) {
          int ndata = new int[data.length+2];
          System.arraycopy(data, 0, ndata, 0, size);
          data = ndata;
          }
          data[size] = value;
          size += 1;
          }

          public int pop() {
          int ret=0;
          if(size>0) {
          size -= 1;
          ret = data[size];
          data[size] = 0;
          }
          return ret;
          }
          .....





          share|improve this answer














          First of all, you are using a static variable (growthmultipler) inside the instance of a class. This is fatal if you have more than one instance of this class. Because you always use 2*growthmultipler, you don't need it. Use +=2 instead.



          You don't need to prealloc data in the constructor. It will be done if it's needed.



          You don't need to handle "size == 0" separately. You only have to handle "size >= length".



          You don't have to create a new class if you want to grow data.



          In pop() you should test to not get negative (and throw an exception). And normally a pop function returns the value.



          In summary it might look like this:



          public class stackAlist{
          int data;
          int size;

          public stackAlist(){
          size = 0;
          data = new int[size];
          }

          public void push(int value){
          if(size>=data.length) {
          int ndata = new int[data.length+2];
          System.arraycopy(data, 0, ndata, 0, size);
          data = ndata;
          }
          data[size] = value;
          size += 1;
          }

          public int pop() {
          int ret=0;
          if(size>0) {
          size -= 1;
          ret = data[size];
          data[size] = 0;
          }
          return ret;
          }
          .....






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 2 days ago

























          answered 2 days ago









          Holger

          1513




          1513












          • Exceptions exist for a reason; the use of a "special" return value should be well documented.
            – Solomon Ucko
            2 days ago










          • I do think it should fail early, though.
            – Solomon Ucko
            2 days ago


















          • Exceptions exist for a reason; the use of a "special" return value should be well documented.
            – Solomon Ucko
            2 days ago










          • I do think it should fail early, though.
            – Solomon Ucko
            2 days ago
















          Exceptions exist for a reason; the use of a "special" return value should be well documented.
          – Solomon Ucko
          2 days ago




          Exceptions exist for a reason; the use of a "special" return value should be well documented.
          – Solomon Ucko
          2 days ago












          I do think it should fail early, though.
          – Solomon Ucko
          2 days ago




          I do think it should fail early, though.
          – Solomon Ucko
          2 days ago












          up vote
          0
          down vote













          A couple things to add:




          1. Classes should use CamelCase and should preferably have descriptive names. stackAlist would be better named ArrayStack.

          2. You might want to have a Stack interface that ArrayStack implements.

          3. You might want to extract the resizing code into a separate method, and optionally make it public and/or take parameters.

          4. In your pop method, there's no need to zero out the popped items.






          share|improve this answer





















          • Thank you for all answers. I am kind of stuck at thinking about why I need a Stack interface that ArrayStack implements. Because I don't know why, I can't think of how to do it. An Interface should be used when there are multiple classes/data types involved. Are you saying that I should make ArrayStack generic so that it can accept all data types? Kinda confused here, please help, thanks.
            – Carch
            yesterday










          • Generics might be good too. Also, having an interface isn't necessary, but it can allow you to decouple it from code that uses it.
            – Solomon Ucko
            yesterday















          up vote
          0
          down vote













          A couple things to add:




          1. Classes should use CamelCase and should preferably have descriptive names. stackAlist would be better named ArrayStack.

          2. You might want to have a Stack interface that ArrayStack implements.

          3. You might want to extract the resizing code into a separate method, and optionally make it public and/or take parameters.

          4. In your pop method, there's no need to zero out the popped items.






          share|improve this answer





















          • Thank you for all answers. I am kind of stuck at thinking about why I need a Stack interface that ArrayStack implements. Because I don't know why, I can't think of how to do it. An Interface should be used when there are multiple classes/data types involved. Are you saying that I should make ArrayStack generic so that it can accept all data types? Kinda confused here, please help, thanks.
            – Carch
            yesterday










          • Generics might be good too. Also, having an interface isn't necessary, but it can allow you to decouple it from code that uses it.
            – Solomon Ucko
            yesterday













          up vote
          0
          down vote










          up vote
          0
          down vote









          A couple things to add:




          1. Classes should use CamelCase and should preferably have descriptive names. stackAlist would be better named ArrayStack.

          2. You might want to have a Stack interface that ArrayStack implements.

          3. You might want to extract the resizing code into a separate method, and optionally make it public and/or take parameters.

          4. In your pop method, there's no need to zero out the popped items.






          share|improve this answer












          A couple things to add:




          1. Classes should use CamelCase and should preferably have descriptive names. stackAlist would be better named ArrayStack.

          2. You might want to have a Stack interface that ArrayStack implements.

          3. You might want to extract the resizing code into a separate method, and optionally make it public and/or take parameters.

          4. In your pop method, there's no need to zero out the popped items.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          Solomon Ucko

          924313




          924313












          • Thank you for all answers. I am kind of stuck at thinking about why I need a Stack interface that ArrayStack implements. Because I don't know why, I can't think of how to do it. An Interface should be used when there are multiple classes/data types involved. Are you saying that I should make ArrayStack generic so that it can accept all data types? Kinda confused here, please help, thanks.
            – Carch
            yesterday










          • Generics might be good too. Also, having an interface isn't necessary, but it can allow you to decouple it from code that uses it.
            – Solomon Ucko
            yesterday


















          • Thank you for all answers. I am kind of stuck at thinking about why I need a Stack interface that ArrayStack implements. Because I don't know why, I can't think of how to do it. An Interface should be used when there are multiple classes/data types involved. Are you saying that I should make ArrayStack generic so that it can accept all data types? Kinda confused here, please help, thanks.
            – Carch
            yesterday










          • Generics might be good too. Also, having an interface isn't necessary, but it can allow you to decouple it from code that uses it.
            – Solomon Ucko
            yesterday
















          Thank you for all answers. I am kind of stuck at thinking about why I need a Stack interface that ArrayStack implements. Because I don't know why, I can't think of how to do it. An Interface should be used when there are multiple classes/data types involved. Are you saying that I should make ArrayStack generic so that it can accept all data types? Kinda confused here, please help, thanks.
          – Carch
          yesterday




          Thank you for all answers. I am kind of stuck at thinking about why I need a Stack interface that ArrayStack implements. Because I don't know why, I can't think of how to do it. An Interface should be used when there are multiple classes/data types involved. Are you saying that I should make ArrayStack generic so that it can accept all data types? Kinda confused here, please help, thanks.
          – Carch
          yesterday












          Generics might be good too. Also, having an interface isn't necessary, but it can allow you to decouple it from code that uses it.
          – Solomon Ucko
          yesterday




          Generics might be good too. Also, having an interface isn't necessary, but it can allow you to decouple it from code that uses it.
          – Solomon Ucko
          yesterday










          Carch is a new contributor. Be nice, and check out our Code of Conduct.










           

          draft saved


          draft discarded


















          Carch is a new contributor. Be nice, and check out our Code of Conduct.













          Carch is a new contributor. Be nice, and check out our Code of Conduct.












          Carch is a new contributor. Be nice, and check out our Code of Conduct.















           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f207968%2fstack-array-implementation-of-java%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Quarter-circle Tiles

          build a pushdown automaton that recognizes the reverse language of a given pushdown automaton?

          Mont Emei