I just started learning java and wrote this variable length array as my first program. I wanted people's opinion on how "java" this code is. For example coding conventions, idioms that I could have used or any improvements that could be made.
MyClass.java:
package myproject;
public class MyClass {
public static void main(String[] args) {
MyArray array = new MyArray();
for (int i = 0; i < 100; i++) array.Push(i);
for (int i = 0; i < array.Size(); i++) {
System.out.println(array.Get()[i]);
}
}
}
MyArray.java:
package myproject;
public class MyArray {
public MyArray() {
array = new int[size];
}
public MyArray(int size) {
this.size = size;
array = new int[size];
}
public int[] Get() {
return array;
}
public void Push(int number) {
if (currentIndex >= size) {
IncreaseArraySize();
}
array[currentIndex] = number;
currentIndex++;
}
int Size() {
return currentIndex;
}
private void IncreaseArraySize(){
if (size == 0){
size++;
}
else {
size *= 2;
}
int newArray[] = new int[size];
for (int i = 0; i < array.length; i++) {
newArray[i] = array[i];
}
array = newArray;
}
private int size = 0;
private int currentIndex = 0;
private int[] array;
}
I was also kind of sceptical of this line : array = newArray;
at first but the program seems to work fine.