How to place two stacks in the same array? Implemented like this, you need two stacks to be placed in one array, how to do it?

public class Stac { private int maxSize; //razamer steka private long[] stacArray; private int top; //top steka public Stac(int s) { maxSize = s; stacArray = new long[maxSize] ; top = -1; } public void push(long j) //vstavka elementa { stacArray[++top] = j; } public long remove() //izvlechenie elementa { return stacArray[top--]; } public long read() //chtenie elementa { return stacArray; } public boolean isEmpty() { return (top == -1); } public boolean isFull() { return (top == maxSize - 1); } } 
  • First, where are you two stacks? Secondly, the read () method, contrary to its name and your comment, does not return an element, but an array size. Third, what will happen to your stack when you try to add (s + 1) -th element to it or to retrieve an element that has not been previously added? - m. vokhm
  • This is the class that describes the stack, I need that when I create 2 instances of this class, their elements are written to one array. - Maxim Shemet

2 answers 2

Author, read about the singleton design pattern , maybe you need him? For the given formulation of the problem sounds very strange (and in general it is not clear why this is all needed).

But I still try to translate into reality the task of the author ( again, if I understood her correctly ) with a simple example.

Data class:

 public class Data { private static int[] mArray; private static int mSize; public Data(int capacity) { if (mArray == null) { mArray = new int[capacity]; mSize = 0; } else { // some actions } } public void add(int value) { if (mSize < mArray.length) { mArray[mSize] = value; mSize++; } else { // some actions } } public void print() { for (int i=0; i<mSize; i++) { System.out.print(mArray[i] + " "); } System.out.println(); } } 

Example of use:

 Data dateOne = new Data(7); dateOne.add(1); dateOne.add(2); dateOne.add(3); Data dateTwo = new Data(15); dateTwo.add(4); dateTwo.add(5); dateTwo.add(10); System.out.print("dateOne: "); dateOne.print(); System.out.print("dateTwo: "); dateTwo.print(); 

Output to console:

 dateOne: 1 2 3 4 5 10 dateTwo: 1 2 3 4 5 10 

The fields mArray and mSize declared as static , this achieves the necessary:

I need that when I create 2 instances of this class, their elements are written to one array

In this implementation, the capacity of the array is set using the constructor when creating the first instance (the constructor calls will be ignored during all subsequent creation of objects of this class).

UPD . Regarding the extension:

 public Data(int capacity) { if (mArray == null) { mArray = new int[capacity]; mSize = 0; } else { mArray = Arrays.copyOf(mArray, mArray.length + capacity); } } 

In this case, the second and subsequent calls to the constructor will expand the capacity of the array by capacity .

  • That is, using the static modifier, an array is created only once, and with the following instantiations, the instance is already expanded? 'For the given formulation of the problem sounds very strange (and it’s not at all clear why all this is necessary)' (This assignment is based on Algorithms and data structures) @post_zeew - Maxim Shemetta
  • @MaksimShemet, In this example, an array is created once (when creating the first instance of this class). With subsequent instantiations of this class, nothing happens to this arrays. - post_zeew
  • If I set the size of 7 at the creation of the first instance, completely filled it, created the second copy and add one more element, it will not be added as an overflow, right? - Maxim Shemet
  • @MaksimShemet, Yes. - post_zeew
  • @post_zeev; Can you make it so that when you call the second instance, the array expands? - Maxim Shemet

There is a standard wonderful class Stack in which everything is already done. Declare the instance static and forward.

  • This is of course understandable, but the task is to implement two stacks by yourself, based on a single array. - Maxim Shemet