Data structures 如何在堆栈java中添加三个数组

Data structures 如何在堆栈java中添加三个数组,data-structures,java,Data Structures,Java,我正在尝试添加三个数组,因此当进入堆栈时,当数组1已满时,移动到下一个数组并获取大小等等,我生成了以下代码,但没有工作 这可能需要很长时间,但我能做的就是弥补我在java方面的糟糕经验 private int itop1,itop2,itop3; //top of the array list int ArraSize=3; // MAx size of the array int [] Array1,Array2,Array3; //first array

我正在尝试添加三个数组,因此当进入堆栈时,当数组1已满时,移动到下一个数组并获取大小等等,我生成了以下代码,但没有工作 这可能需要很长时间,但我能做的就是弥补我在java方面的糟糕经验

private int itop1,itop2,itop3;         //top of the array list
int ArraSize=3;     // MAx size of the array
int [] Array1,Array2,Array3;        //first array


public LinkedSt (){

    itop1=-1;
    itop2=-1;
    itop3=-1;
    Array1=new int [ArraSize];
    //head.next=tail.next=null;
    Array2=new int [ArraSize];
    Array3=new int [ArraSize];
    } 
public void push1 (int el){

    if (isStackFull2()&&isStackFull1()){
        System.out.println("Fuckin FUll");}

    if (itop2<Array2.length){
    addToHead(el);
    itop2=itop2+1;
    Array1[itop2]=el;
}
    if (itop1<Array1.length){
    addToHead(el);
    itop1=itop1+1;
    Array1[itop1]=el;
    }


public class LinkedSt extends SingleLinkList {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    LinkedSt myLinkedSt= new LinkedSt();
    System.out.println("Is the linked stack empty??"+myLinkedSt.isEmpty());
    System.out.println("Is the linked stack Full??"+myLinkedSt.isStackFull1());
    System.out.println("Size"+myLinkedSt.size());
    myLinkedSt.push1(10);
    myLinkedSt.push1(10);
    myLinkedSt.push1(10);
    System.out.println("Size"+myLinkedSt.size());
    System.out.println("after pushing 10");
    System.out.println("Is the linked stack empty??"+myLinkedSt.isEmpty());
    System.out.println("Is the linked stack Full??"+myLinkedSt.isStackFull1());
    System.out.println("stack full and pushing 10");
    myLinkedSt.push1(10);
    System.out.println("Size"+myLinkedSt.size());
    myLinkedSt.push1(10);
    myLinkedSt.push1(10);
    myLinkedSt.push1(10);
    System.out.println("Size"+myLinkedSt.size());
    myLinkedSt.push1(10);
    myLinkedSt.push1(10);
    myLinkedSt.push1(10);
    System.out.println("Size"+myLinkedSt.size());
    myLinkedSt.push1(10);
    myLinkedSt.push1(10);
    myLinkedSt.push1(10);
    System.out.println("Size"+myLinkedSt.size());
    myLinkedSt.push1(10);
    myLinkedSt.push1(10);
    myLinkedSt.push1(10);
    System.out.println("Size"+myLinkedSt.size());
    myLinkedSt.push1(10);
    myLinkedSt.push1(10);
    myLinkedSt.printList();
}

有更简单的方法来实现循环数组

下面是一个使用二维数组的快速尝试

public class CyclicArray{

    private static final int NB_ARRAYS = 3; //The number of array
    private static final int ARRAY_SIZE = 5; //The length of each array
    private int[][] arrays = new int[NB_ARRAYS][];
    private int currentArray = -1, 
                currentIndex = ARRAY_SIZE; //Those value are set to force the first cycle that will create the first array

    public static void main(String[] args){
        CyclicArray main = new CyclicArray();

        for(int i = 0; i < 20; ++i){
            main.add(i);
        }

        for(int[] array : main.arrays)
            System.out.println(Arrays.toString(array));
    }

    public void add(int i){
        if(currentIndex >= ARRAY_SIZE){ //Change of array
            currentIndex = 0; 
            currentArray = ++currentArray % NB_ARRAYS; //Using % to return at the beginning

            if(arrays[currentArray] == null){ //Create if it doesn't exist (could be done at the beginning instead)
                arrays[currentArray] = new int[ARRAY_SIZE];
            }
        }

        arrays[currentArray][currentIndex++] = i; //incrementing the currentIndex after use
    }
}
这将在当前数组的下一个单元格中添加一个值。当它到达末尾时,将使用下一个数组。到达末尾后,重用第一个数组覆盖这些值

输出 本演示的小示例将输出以下内容:

[15,16,17,18,19]

[5,6,7,8,9]

[10,11,12,13,14]


要解决此问题,不需要创建三个数组。首先,您可以定义一个足够大的数组,比如说5000大小的数组。其次,根据数组的定义,如果数组已满,则需要创建另一个2倍或4倍于当前数组的数组,然后将所有内容从当前数组移动到2倍于当前数组的新数组(如果当前已满)。另一种避免元素移动的方法是使用Arraylist。如果Arraylist已满,它将自动调整大小。要在所有内容都已满时停止,只需删除%并添加一些检查以防止异常