Stack and dynamic array implementation in Java -


I need to implement a stack array using a dynamic array, which I had to make myself.

Here's my dynamic array:

  Import java.util.Arrays; Public class array {public entity] dianare; Int counter = 0; Public Zero (int n) {dynArray = new int [n]; } Public zero doubleSize () {int currentSize = dynArray.length; Int newSize = currentSize * 2; DynArray = Arrays.copyOf (dynArray, newSize); } Private zero half size () {int currentSize = dynArray.length; Int newSize = currentSize / 2; DynArray = Arrays.copyOf (dynArray, newSize); } Public Zero Addition (int x) {dynArray [Counter] = x; Counter ++; Float ratio = (float) counter / (float) diamond line; If (ratio == 1) {doubleSize (); }} Public int rem () {int last = dynAray [counter 1]; DynArray [Counter-1] = 0; Counter--; Float ratio = (float) counter / (float) diamond line; If (ratio & lt; = 0.25) {halfSize (); } Previous return; } Public int (int i) {if (! (((I))) {//System.out.print ("can not be added"); Return i; } And instead dynArray [i]; } Enter the public zero (int x, int y) {float ratio = (float) counter / (float) dynArray.length; If (ratio == 1) {doubleSize (); } If (check (y)) {System.out.println ("Any such index, nothing is added" + y); } And {dynArray [y] = x; }} Public full lane () {return dynArray.length; } Public Boolean check (int index) {if (index  

Here is my stack code:

  public class stack {Private static array A; Generate Public Zero () {a = New Array (); } Zero push (int x) {a.add (x); }} Pop () {return a.rem (); } Boolean is empty () {return a.len () == 0; } Public static zero main (string [] args) {stack stack = new stack (;); Stack.create (); Stack.push (1); / * Int k = 1; Stack.push (2 * t); Stack.push (2 * k + 1); Stack.push (2 * k + 2); {System.out.println (stack.pop ()) for (Int i = 0; I & lt; 40; i ++); } When I type  stack.java  in  stack.push (1)  main, then it will send me   

Code> NullPointerException on the line where it says a.add (x) . I do not understand why this is an exception.

Creates an example of its stack a new array , But does not call to create the array method, so dynArray remains redundant, and you receive NullPointerException ( DynArray [counter] = x; ).

You should probably call create in the constructor of array (or create to eliminate the code and its code Should be moved to the constructor)


Comments

Popular posts from this blog

HTML/CSS - Automatically set height width from background image? -

php - Mysql Show Process - Sleep Commands and what to do -

c - What is the address of buf (the local variable in the main function)? -