I am taking a programming class for java and I need help with the Dynamic arrays. I have looked around and can't find ways to do so that are on my level of simplicity. I am not far in the class and just learned the basics so I don't know to much but I need to know how to make a Dynamic Array.
Here are the two sample Programs we were given:
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;
}
}
`
Number 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) );
}
}
}
The Second one is supposed to inherit the first and allow you to Create more arrays once they are typed in. But when I use these it says I have an error and it says that Class names ReverseWithDynamicArray are only accepted if annotation processing is explicitly requested.