我将如何创建一个方法,将参数值存储在位置变量索引处的数组中,然后将 1 添加到位置变量

How would I create a method that stores the parameter value in an array at the index of the position variable, then adds 1 to the position variable

我有一个创建数组的任务 class,其中有 2 个构造函数,每个构造函数为数组设置了不同的大小。

该数组已经是一个实例变量以及另一个实例变量以跟踪数组中的当前位置。

我必须创建一个名为 add 的方法,它带有一个整数参数,它将参数值存储在数组中位置变量的索引处,然后将位置变量加 1。如果递增的位置变量在数组边界之外,则该方法调用 addspace 方法。

addspace方法创建一个比实例变量数组大25%的新数组,将实例数组的所有值复制到新数组中,并将新数组赋值给实例变量。

我还需要一个名为 size 的方法,它将 return 位置变量中的值和一个名为 get 的方法,该方法带有 1 个参数(索引),该方法 returns 处的值参数索引。

我最不需要的是使用 for 循环打印数组中的值的打印方法。

到目前为止,这就是我所拥有的

public class ArrayClass
{
    private int array[];
    private int x=0;

    public ArrayClass()
    {
        this.array= new int[10];
        add(1);
        getThat(0);
        print();
    }

    public ArrayClass(int y)
    {
        this.array= new int[y];
        add(2);
        getThat(0);
        print();
    }

    public void add(int a)
    {
        array[x]=a;
        x++;
        if(x>array.length)
            addspace();
    }

    public void addspace()
    {
        double d=array.length+(array.length*0.25);
        int v=(int)d;
        int newArray[]= new int[v];
        for(int i=0; i<array.length; i++)
        {
            newArray[i]=array[i];
            System.out.println(newArray[i]);
        }

    }
    public int size()
    {
        return x;
    }
    public int getThat(int index)
    {
        return array[index];
    }

    public void print()
    {
        for(int i=0; i<array.length; i++)
            System.out.println(array[i]+" ");
    }
    public static void main(String[] args)
    {
        new ArrayClass();
        new ArrayClass(5);
    }
}

我知道标题只寻求第一种方法的帮助,但如果有人愿意帮助其他方法以及我的代码无法 运行 并打印我想要的内容的原因将不胜感激。

仅将 ArrayClass 用于将 functionality.Call add 方法声明为 obj.add(number) 直到并且除非您需要在 ArrayClass 构造函数本身中添加一些内容.

根据我的理解修改了这些东西

  1. 在您的 add 方法中,您首先分配值,然后在数组已满时添加 space,在这种情况下,即使可能不需要,您也会增加大小 (i.e not calling add method again)。 而不是仅在需要时才增加大小。
  2. print 函数中,您正在遍历整个 array。修改为-> 它将迭代到值 (i.e x)[=32= 的最后一个 index ]
package com.example;
public class ArrayClass
{
    private int array[];
    private int x=0;
    private final int DEFAULT_SIZE=4;

   public ArrayClass(){
    this.array = new int[DEFAULT_SIZE];
   }

   public ArrayClass(int size){
        this.array = new int[size];
   }

   public void add(int number){
       //check whether array have space or not .if not then increase the space.
       if(x > this.array.length-1){
           addSpace();
       }
       array[x] =number;
       x++; 
   }

   private void addSpace(){
        double newSize = array.length + array.length * 0.25;
        int tempArray[] = new int[(int) newSize];
        for(int i=0; i<array.length; i++){
            tempArray[i]=array[i];            
        }
        this.array = tempArray;
   }


   public int size()
    {
        return x;
    }
    public int getThat(int index)
    {
        return array[index];
    }

    public void print()
    {   
        //instead of of printing the whole array Printed till last value index.
        for(int i=0; i<x; i++)
            System.out.println(array[i]+" ");
    }

}

来自main方法

    ArrayClass ac1 = new ArrayClass();
    ac1.add(5);
    ac1.add(4);
    ac1.add(5);
    ac1.add(4);
    ac1.add(7);
    ac1.add(19);
    ac1.print();
    ArrayClass ac2 = new ArrayClass(5);
    ac2.add(1);
    //rest of your function call here