[英]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
I have an assignment to create an array class where there are 2 constructors where each constructor sets a different size for the array.我有一个任务来创建一个数组类,其中有 2 个构造函数,每个构造函数为数组设置不同的大小。
The array is already an instance variable along with another instance variable to keep track of the current position in the array.该数组已经是一个实例变量以及另一个实例变量,用于跟踪数组中的当前位置。
I have to create a method called add with an integer parameter that will store the parameter value in the array at the index of the position variable, then add 1 to the position variable.我必须使用一个整数参数创建一个名为 add 的方法,它将参数值存储在数组中位置变量的索引处,然后将 1 添加到位置变量。 If the incremented position variable is outside the bounds of the array, the method calls the addspace method.
如果增加的位置变量超出了数组的边界,则该方法调用 addspace 方法。
The addspace method creates a new array 25% larger than the instance variable array, copies all the values of the instance array to the new array, and assigns the new array to the instance variable. addspace 方法创建一个比实例变量数组大 25% 的新数组,将实例数组的所有值复制到新数组,并将新数组分配给实例变量。
I also need a method called size that will return the value in the position variable and a method called get that with 1 parameter(an index), the method returns the value at the parameter index.我还需要一个名为 size 的方法,它将返回位置变量中的值,以及一个名为 get 的方法,它带有 1 个参数(一个索引),该方法返回参数索引处的值。
The last thing I need is a print method that uses a for loop to print the values in the array.我需要的最后一件事是使用 for 循环打印数组中的值的打印方法。
So far this is what I have到目前为止,这就是我所拥有的
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);
}
}
I know the title only asks for help with the first method but if someone would be kind enough to help with the other methods and the reason why my code won't run and print what I want it to that would be much appreciated.我知道标题只要求第一种方法的帮助,但如果有人愿意帮助其他方法以及我的代码无法运行并打印我想要的内容的原因,我将不胜感激。
Use the ArrayClass
for only for declaring your functionality.Call add
method as obj.add(number)
until and unless you need to add something inside ArrayClass
constructor itself.使用
ArrayClass
仅用于声明您的功能。 add
方法调用为obj.add(number)
直到并且除非您需要在ArrayClass
构造函数本身中添加某些内容。
Modified these things as per my understanding根据我的理解修改了这些东西
add
method you are assigning the value first and then adding space if the array is full, in this case, you are increasing the size even if it might not be needed (ie not calling add method again)
.add
方法中,您首先分配值,然后在数组已满时添加空间,在这种情况下,即使可能不需要它,您也会增加大小(ie not calling add method again)
。 Instead of this increase the size only when you require it.print
function you are iterating through the whole array
.Modified to-> it will iterate till the last index
of value (ie x)
print
函数中,您正在遍历整个array
。修改为-> 它将迭代到最后一个值index
(ie x)
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]+" ");
}
}
From the main
method从
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.