FREEDOM FREEDOM - 6 months ago 9
Java Question

Retrieving length of an array in a method doesn't work

I'm fairly new to Java so please imagine that I'm 5 years old and require an answer suitable for a five year old.

First some information:
I'm trying to make a "Dynamic array" that grows and shrinks when needed. I know of arraylists, the purpose of this is that i need to be able to do this myself and also learn at the same time since i assume this is how

ArrayList
s work anyway.

The problem: I want to add some fancy methods for the reason above one of which is to calculate the mean value of the whole array.
when i try to get the length (
varible.length
) so does it not work while in some other methods it does!

This is the method that doesn't work:

public double meanValue() {
double meanDiv = 0;
int div = x.length;

for (int i = 0 ; i < div ; i++) {
meanDiv += x[i];
System.out.println("Testing");
}
meanDiv = meanDiv/x.length;
return meanDiv;
}


It's returning NaN (not a number), but if you skip the division part it returns 0, it's totally not reading
x.length
.

Worst part? Some methods are working, this one for example to return the last position:

public long last() {
long pepe = -1;
if (x.length==0) {
System.out.println("Nothing in the array! Returning -1");
}
else {
pepe = x[x.length-1];
}
return pepe;
}


I've tried way too many things, different kind of variables, while instead of for, etc.

Here is the whole class if it's required:

public class dynamicArray {
private long[] x;
private int size = 0;

public dynamicArray() {
x = new long[size];
} // CREATES THE ARRAY

public long grab(int x) {
if (x < this.x.length) {
return this.x[x];
}
else {
System.out.println("(GET) Out of bounds");
return -1;
}
}
public void add(long value) {
dynamicInAction();
x[size] = value;
size++;
} // ADD A NUMBER TO THE ARRAY
public void remove(int position) {

} // REMOVES A SPECIFIC ARRAY POSITION----------------
public long last() {
long pepe = -1;
if (x.length==0) {
System.out.println("Nothing in the array! Returning -1");
}
else {
pepe = x[x.length-1];
}
return pepe;
} // RETURNS THE VALUE IN THE LAST POSITION
public long first() {
long pepe = -1;
if (x.length==0) {
System.out.println("Nothing in the array! Returning -1");
}
else {
pepe = x[0];
}
return pepe;
} // RETURNS THE VALUE IN THE FIRST POSITION
public void modify(int position, int value) {
if (position < x.length && position >= 0) {
x[position] = value;
}
else {
System.out.println("This position does not exist");
}
} // MODIFIES A SPECIFIC POSITION TO THE ASSIGNED VALUE
public int size() {
return x.length;
} // RETURNS THE CURRENT ARRAY SIZE
public double meanValue() {
double meanDiv = 0;
int div = x.length;

for (int i = 0 ; i < div ; i++) {
meanDiv += x[i];
System.out.println("Testing");
}
meanDiv = meanDiv/x.length;
return meanDiv;
} // RETURNS THE MEAN VALUE OF THE WHOLE ARRAY-------------
public long median() {
return -1;
} // RETURNS THE MEDIAN VALUE OF THE WHOLE ARRAY-------------
private void dynamicInAction() {
if (x.length == size) {
long[] temp = new long[x.length];
for (int i = 0; i<x.length ; i++) {
temp[i] = x[i];
}
x = null;
//System.gc(); // Might be unnecessary
x = new long[size+1];
for (int i = 0; i<temp.length ; i++) {
x[i] = temp[i];
}
}
} // ENSURES THE ARRAY GROWS WHEN NEEDED
public void reverse() {

} // REVERSING ALL POSITIONS IN THE ARRAY------------------
public void clear() {
x = null;
size = 0;
x = new long[size];
} // CLEARS THE ARRAY



}

Answer

You are creating an array with a size of 0 in the constructor. That means unless you reassign x to another array, it will always have 0 elements. Try passing in the size in the constructor like so:

public DynamicArray(int size){
  this.size = size;
  this.x = new long[size];
}

Also, in Java class names have upper-case letters for each word (notice DynamicArray not dynamicArray).

Comments