我正在爲java編程,我需要動態數組的幫助。我環顧四周,找不到符合我簡單程度的方法。我在班上並不遙遠,只是學習了基礎知識,所以我不太瞭解,但我需要知道如何製作動態數組。Java動態數組
下面是我們給出的兩個示例程序:第二個應該是繼承了第一,並允許您創建多個陣列一旦被輸入
public class DynamicArrayOfInt
{
private int[] data;
public DynamicArrayOfInt()
{
data = new int[1];
}
public int get(int position)
{
if (position >= data.length)
return 0;
else
return data[position];
}
public void put(int position, int value)
{
if (position >= data.length)
{
int newSize = 2 * data.length;
if (position >= newSize)
newSize = 2 * position;
int[] newData = new int[newSize];
System.arraycopy(data, 0, newData, data.length);
data = newData;
System.out.println("Size of dynamic array increased to " + newSize);
}
data[position] = value;
}
}
`
2號
import java.util.Scanner;
public class ReverseWithDynamicArray
{
public static void main(Sting[] args)
{
DyanamicArrayOfInt numbers;
int numCt;
int num;
Scanner scan = new Scanner(System.in);
numbers = new DynamicArrayOfInt();
numCt = 0;
System.out.println("Enter some postive integers; Enter 0 to end");
while (true)
{
num = scan.nextInt();
if (num <= 0)
break;
numbers.put(numCt, num);
numCt++;
}
System.out.println("\nYour numbers in reverse order are:\n");
for (int i = numCt - 1; i >= 0; i--)
{
System.out.println(numbers.get(i));
}
}
}
英寸但是當我使用這些它說我有一個錯誤,它說類名ReverseWithDynamicArray
只接受如果明確要求註釋處理。
我想你在System.arraycopy()中缺少一個參數? – cakil 2013-04-09 00:53:14