Skip to main content
edited tags
Link
dfhwze
  • 14.2k
  • 3
  • 40
  • 101
Tweeted twitter.com/StackCodeReview/status/1170894885903392768
code formatting
Source Link
Ben A
  • 10.8k
  • 5
  • 38
  • 103

I have a exercise where I have to write a Stack class with the push and pop methods. The code compiles and works as it should. Are there better practices to use in my code? Is there something I should avoid doing?

public class Stack {

    private final int INCREMENTSIZE = 1024;
    Integer position = 0;

    int[] list = null;

    public Stack(){
        list = new int[INCREMENTSIZE];
    }

    public void push(Integer i) {
        if(position == list.length) {
            list = Arrays.copyOf(list, list.length + INCREMENTSIZE);
        }
        list[position++] = i;
    }

    public Integer pop(){
        return list[--position];
    }
}

I have a exercise where I have to write a Stack class with the push and pop methods. The code compiles and works as it should. Are there better practices to use in my code? Is there something I should avoid doing?

public class Stack {

private final int INCREMENTSIZE = 1024;
Integer position = 0;

int[] list = null;

public Stack(){
    list = new int[INCREMENTSIZE];
}

public void push(Integer i) {
    if(position == list.length) {
        list = Arrays.copyOf(list, list.length + INCREMENTSIZE);
    }
    list[position++] = i;
}

public Integer pop(){
    return list[--position];
}
}

I have a exercise where I have to write a Stack class with the push and pop methods. The code compiles and works as it should. Are there better practices to use in my code? Is there something I should avoid doing?

public class Stack {

    private final int INCREMENTSIZE = 1024;
    Integer position = 0;

    int[] list = null;

    public Stack(){
        list = new int[INCREMENTSIZE];
    }

    public void push(Integer i) {
        if(position == list.length) {
            list = Arrays.copyOf(list, list.length + INCREMENTSIZE);
        }
        list[position++] = i;
    }

    public Integer pop(){
        return list[--position];
    }
}
Copy edited (e.g. ref. <https://en.wikipedia.org/wiki/Java_version_history#Java_SE_8>).
Source Link

Stack class in Java8Java 8

I have a exercise where I have to write a Stack class with the push and pop methodmethods. The code compiles and works as it should. My question is: Are there better practices to use in my code? Is there something I should avoid doing?

public class Stack {

private final int INCREMENTSIZE = 1024;
Integer position = 0;

int[] list = null;

public Stack(){
    list = new int[INCREMENTSIZE];
}

public void push(Integer i) {
    if(position == list.length) {
        list = Arrays.copyOf(list, list.length + INCREMENTSIZE);
    }
    list[position++] = i;
}

public Integer pop(){
    return list[--position];
}
}

Stack class in Java8

I have a exercise where I have to write a Stack class with the push and pop method. The code compiles and works as it should. My question is: Are there better practices to use in my code? Is there something I should avoid doing?

public class Stack {

private final int INCREMENTSIZE = 1024;
Integer position = 0;

int[] list = null;

public Stack(){
    list = new int[INCREMENTSIZE];
}

public void push(Integer i) {
    if(position == list.length) {
        list = Arrays.copyOf(list, list.length + INCREMENTSIZE);
    }
    list[position++] = i;
}

public Integer pop(){
    return list[--position];
}
}

Stack class in Java 8

I have a exercise where I have to write a Stack class with the push and pop methods. The code compiles and works as it should. Are there better practices to use in my code? Is there something I should avoid doing?

public class Stack {

private final int INCREMENTSIZE = 1024;
Integer position = 0;

int[] list = null;

public Stack(){
    list = new int[INCREMENTSIZE];
}

public void push(Integer i) {
    if(position == list.length) {
        list = Arrays.copyOf(list, list.length + INCREMENTSIZE);
    }
    list[position++] = i;
}

public Integer pop(){
    return list[--position];
}
}
Became Hot Network Question
edited tags
Link
dfhwze
  • 14.2k
  • 3
  • 40
  • 101
Loading
Source Link
John
  • 241
  • 2
  • 4
Loading