I have to do the Orthogonalization process using Gram-Schimdt so I can get A=QR. I'm working with the matrix columns as double[] and they are stored in an ArrayList, the thing is, I'm just calculating Q so I can do the rest which is easier once I got Q. But my problem is that when the program gets to add the finalArray, I don't know why the parameter array got modified and every vector in it is altered and obviously the result is wrong. What I'm doing wrong? (By the way, I can't use libraries of Java to solve this). Also I test the algorithm doing it with my own hands and it should works because I also print the result and notice where it got altered
/*
I should do Uk = (Vk - (Uk-1*Vk)*Uk-1 - ... - (U1*Vk)*Uk1)/||Uk||
Where ||Uk|| = Length of Uk
Vectors U own to finalArray and the V ones own to array
*/
public ArrayList< double[] > gramSchmidt(ArrayList< double[] > array)
{
ArrayList< double[] > finalArray= new ArrayList<>();
//I set the first vector because it never changes, it's always the first vector of the array receive divided between it's length
finalArray.add(multiplyScalarPerVector(1/(calculateVectorLength(array.get(0))), array.get(0)));
//This last line is the one that modifies EVERYTHING in array and it shouldn't
for(int i=1; i<array .size(); i++)
{
double[] newVector= substractVectors(array .get(i), proyection(finalArray.get(i-1),array .get(i)));
for(int e=i-1;e>0;e--)
{
newVector= substractVectors(newVector, proyection(finalArray.get(e-1),array .get(i)));
}
newVector= multiplyScalarPerVector(1/(calculateVectorLength(newVector)), newVector);
finalArray.add(newVector);
}
return finalArray;
}
//Obtain the (Uk-1*Vk)*Uk-1
public double[] proyection(double[] array1, double[] array2)
{
double dotProductResult= dotProduct(array1,array2);
double[] finalVector= multiplyScalarPerVector(dotProductResult, array1);
return finalVector;
}
//To do Uk-1*Vk
public double dotProduct(double[] vector1, double[] vector2)
{
double result = 0;
for(int i=0; i<vector1.length; i++)
{
result +=vector1[i]*vector2[i];
}
return result ;
}
public double[] multiplyScalarPerVector(double scalar, double[] vector)
{
double[] newVector = new double[vector.length];
for(int i=0; i<vector.length; i++)
{
newVector[i] = scalar*vector[i];
}
return newVector;
}
public double[] substractVectors(double[] vector1, double[] vector2)
{
double[] finalVector= new double[vector1.length];
for(int i=0; i<vector1.length; i++)
{
finalVector[i] = vector1[i] - vector2[i];
}
return finalVector;
}
//Calculate the euclidean distance
public double calculateVectorLength(double[] vector)
{
double result = 0;
for(int i=0; i<vector.length; i++)
{
result +=Math.pow(vector[i], 2);
}
return Math.sqrt(result );
}
When you assign
double[] newVector= vector;
then you don't create a new object, but newVector and vector reference to the same object. newVector is not a new object it is a reference to the same object as vector references to.
This causes that in the loop
for(int i=0; i<vector.length; i++)
{
newVector[i] = scalar*vector[i];
}
newVector and vector are change alike. An assignment to newVector[i] changes also vector[i], because they refer to the same object.
The same is the case when you do:
double[] finalVector= vector1;
for(int i=0; i<vector1.length; i++)
{
finalVector[i] = vector1[i] - vector2[i];
}
Note, you don't create new vector objects with new content, but you change the input vector objects and return a reference to the changed objects.
Because of that you change source vector objects, which are still needed in the further calculation. This causes that the final result is not as it ought to be.
Create new objects to solve the issue:
double[] newVector= new double[vector.length];
double[] finalVector= new double[vector1.length];
See also Java assigning object reference
Related
I am new to programming and trying to learn Java and I am trying to do some Java questions that I find quite tough for a beginner. The question asks to write a method that takes a double c and and array v of type double as it's parameters. The method should return a new array of double formed by multiplying all the elements of array v by c.
I really have no idea to do this and if anyone could help on this I'd appreciate it.
I have written some code but I don't understand what I am supposed to do exactly.
public static double times( double c, double [] v)
int i =0;
for( i =0; i < v .length; i++){
myArray =(c * v[i]);
i++;
}
}
public class Main {
public static void main(String[] args) {
double [] v={5.1,5.2,3.0,4.0};
double c= 4.1;
System.out.println(times(v,c));
It’s a good start but your method should return an array of doubles: double[].
public static double[] times( double c, double [] v)
double[] myArray = new double[v.length]; // this is a new array
int i =0;
for( i =0; i < v .length; i++){
myArray[i] =(c * v[i]); // assign new values to your array
// i++; << don’t need this line as your for loop is already incrementing i
}
return myArray;
}
The answer mentioned above is correct but you could do the same in the same array i.e double[] v, instead of creating a new array, just for optimization scenario
Read carefully your problem.
I added comments to the code so you understand what you did wrongly:
// Return a double[] instead of double
public static double[] times( double c, double [] v)
// Create a new double array
double[] myArray = new double[v.length];
for (int i = 0; i < v.length; i++) {
// Set each element of the new array equals to the old array element in
// The same position multiplied by c
myArray[i] = c * v[i]; // Parenthesis are not needed here
// i++ is not needed because you already add 1 to i in the for instruction
}
// Return the new array
return myArray;
}
Also be careful what you print. I believe you want to print the new values not the array reference.
public static void main(String[] args) {
double[] v = {5.1, 5.2, 3.0, 4.0};
double c = 4.1;
double[] newV = times(c, v);
System.out.print("Array address: ");
System.out.println(newV);
System.out.print("Array as string: ");
System.out.println(Arrays.toString(newV));
System.out.print("Array values for: ");
for (int index = 0; index < newV.length; ++index) {
System.out.println(newV[index]);
}
System.out.print("Array values foreach: ");
for (double value : newV) {
System.out.println(value);
}
}
a) Create an array of random numbers, whose size is a power of 2. Using loops, find the difference for each pair of values (index 0 & 1, 2 & 3, 4 & 5 etc.) and store them in a new array. Then find the difference for each pair of differences and so on until you have only one difference left.
Hint: Think carefully about your loop bounds
b) Now, create a solution that is 'in place', i.e., It does not require the creation of new arrays. Again, this will require careful consideration of loop bounds.
c) Finally, write a solution that makes use of a recursive function, instead of loops.
I have been trying to solve the above exercise but I am stuck with what b means and how can I use recursive function. The following is my solution for part a :
public class RandomArray{
private static double ArrayFn(int p){
double[] orignalArray = new double[(int)Math.pow(2,p)];
for (int i = 0; i< orignalArray.length; i++){
orignalArray[i] = (int)(Math.random() * 10) ;
}
System.out.println(Arrays.toString(orignalArray));
double y = ArrayDifferenceloop(orignalArray);
System.out.println("Value of Array" + y);
return y;
}
private static double ArrayDifferenceloop(double[] arg){
do{
double[] newArr = new double[(arg.length/2)];
for (int i = 0; i< arg.length; i+=2){
newArr[i/2] = arg[i] - arg[i+1];
}
System.out.println("New Array is =" + Arrays.toString(newArr));
//copy newArr to arg
arg = new double[(newArr.length)];
System.arraycopy(newArr,0,arg,0,newArr.length);
}while(arg.length > 1);
return arg[0];
}
public static void main(String[] args){
double z = ArrayFn(3);
System.out.println("value" + z);
}
}
I can help you with point b)
you can store the differences in the original array itself:
difference of [0] and [1] put in [0],
difference of [2] and [3] put in [1],
and so on.
You can calculate the index to put the result from the indexes of the pair or keep two index variables for the result and for picking the pairs.
you just keep iterate over the original array repeatedly, each time over fewer cells until only two cells left.
the recursive solution should be clear...
I guess option b means use the original array to store the differences, rather than creating a new array.
This can be achieved by dynamically changing the active range of elements used, ignoring others (see also Sharon Ben Asher answer ):
private static double ArrayDifferenceloop(double[] array){
int activeLength = array.length;
do{
int index =0; //index where to store difference
for (int i = 0; i< activeLength; i+=2){
array[index++] = array[i] - array[i+1];
}
System.out.println("Modified array (only "+index+ " elements are significant) " + Arrays.toString(array));
activeLength /=2;
}while(activeLength > 1);
return array[0];
}
/* Solution for part (b) hope it works for you*/
public class RandomArray{
static int len; /*modification*/
private static double ArrayFn(int p){
double[] orignalArray = new double[(int)Math.pow(2,p)];
len=(int)Math.pow(2,p);
for (int i = 0; i< orignalArray.length; i++){
orignalArray[i] = (int)(Math.random() * 10) ;
}
System.out.println(Arrays.toString(orignalArray));
double y = ArrayDifferenceloop(orignalArray);
System.out.println("Value of Array" + y);
return y;
}
private static double ArrayDifferenceloop(double[] arg){
do{
for (int i = 0; i< len; i+=2){ /*modification*/
arg[i/2] = arg[i] - arg[i+1];
}
//copy newArr to arg
//arg = new double[(arg.length)];
len=len/2; /*modification*/
System.out.print("new Array : ");
for(int i=0;i<len;i++){
System.out.print(arg[i]+" , ");
}
// System.arraycopy(arg,0,arg,0,len);
}while(len > 1);
return arg[0];
}
public static void main(String[] args){
double z = ArrayFn(3);
//System.out.println(Arrays.toString(orignalArray));
System.out.println("value" + z);
}
}
I have searched for a way to resize an array in Java, but I could not find ways of resizing the array while keeping the current elements.
I found for example code like int[] newImage = new int[newWidth];, but this deletes the elements stored before.
My code would basically do this: whenever a new element is added, the array largens by 1. I think this could be done with dynamic programming, but I'm, not sure how to implement it.
You can't resize an array in Java. You'd need to either:
Create a new array of the desired size, and copy the contents from the original array to the new array, using java.lang.System.arraycopy(...);
Use the java.util.ArrayList<T> class, which does this for you when you need to make the array bigger. It nicely encapsulates what you describe in your question.
Use java.util.Arrays.copyOf(...) methods which returns a bigger array, with the contents of the original array.
Not nice, but works:
int[] a = {1, 2, 3};
// make a one bigger
a = Arrays.copyOf(a, a.length + 1);
for (int i : a)
System.out.println(i);
as stated before, go with ArrayList
Here are a couple of ways to do it.
Method 1: System.arraycopy():
Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest. The number of components copied is equal to the length argument. The components at positions srcPos through srcPos+length-1 in the source array are copied into positions destPos through destPos+length-1, respectively, of the destination array.
Object[] originalArray = new Object[5];
Object[] largerArray = new Object[10];
System.arraycopy(originalArray, 0, largerArray, 0, originalArray.length);
Method 2: Arrays.copyOf():
Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain null. Such indices will exist if and only if the specified length is greater than that of the original array. The resulting array is of exactly the same class as the original array.
Object[] originalArray = new Object[5];
Object[] largerArray = Arrays.copyOf(originalArray, 10);
Note that this method usually uses System.arraycopy() behind the scenes.
Method 3: ArrayList:
Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)
ArrayList functions similarly to an array, except it automatically expands when you add more elements than it can contain. It's backed by an array, and uses Arrays.copyOf.
ArrayList<Object> list = new ArrayList<>();
// This will add the element, resizing the ArrayList if necessary.
list.add(new Object());
You could just use ArrayList which does the job for you.
It is not possible to change the Array Size.
But you can copy the element of one array into another array by creating an Array of bigger size.
It is recommended to create Array of double size if Array is full and Reduce Array to halve if Array is one-half full
public class ResizingArrayStack1 {
private String[] s;
private int size = 0;
private int index = 0;
public void ResizingArrayStack1(int size) {
this.size = size;
s = new String[size];
}
public void push(String element) {
if (index == s.length) {
resize(2 * s.length);
}
s[index] = element;
index++;
}
private void resize(int capacity) {
String[] copy = new String[capacity];
for (int i = 0; i < s.length; i++) {
copy[i] = s[i];
s = copy;
}
}
public static void main(String[] args) {
ResizingArrayStack1 rs = new ResizingArrayStack1();
rs.push("a");
rs.push("b");
rs.push("c");
rs.push("d");
}
}
You could use a ArrayList instead of array. So that you can add n number of elements
List<Integer> myVar = new ArrayList<Integer>();
Standard class java.util.ArrayList is resizable array, growing when new elements added.
You can't resize an array, but you can redefine it keeping old values or use a java.util.List
Here follows two solutions but catch the performance differences running the code below
Java Lists are 450 times faster but 20 times heavier in memory!
testAddByteToArray1 nanoAvg:970355051 memAvg:100000
testAddByteToList1 nanoAvg:1923106 memAvg:2026856
testAddByteToArray1 nanoAvg:919582271 memAvg:100000
testAddByteToList1 nanoAvg:1922660 memAvg:2026856
testAddByteToArray1 nanoAvg:917727475 memAvg:100000
testAddByteToList1 nanoAvg:1904896 memAvg:2026856
testAddByteToArray1 nanoAvg:918483397 memAvg:100000
testAddByteToList1 nanoAvg:1907243 memAvg:2026856
import java.util.ArrayList;
import java.util.List;
public class Test {
public static byte[] byteArray = new byte[0];
public static List<Byte> byteList = new ArrayList<>();
public static List<Double> nanoAvg = new ArrayList<>();
public static List<Double> memAvg = new ArrayList<>();
public static void addByteToArray1() {
// >>> SOLUTION ONE <<<
byte[] a = new byte[byteArray.length + 1];
System.arraycopy(byteArray, 0, a, 0, byteArray.length);
byteArray = a;
//byteArray = Arrays.copyOf(byteArray, byteArray.length + 1); // the same as System.arraycopy()
}
public static void addByteToList1() {
// >>> SOLUTION TWO <<<
byteList.add(new Byte((byte) 0));
}
public static void testAddByteToList1() throws InterruptedException {
System.gc();
long m1 = getMemory();
long n1 = System.nanoTime();
for (int i = 0; i < 100000; i++) {
addByteToList1();
}
long n2 = System.nanoTime();
System.gc();
long m2 = getMemory();
byteList = new ArrayList<>();
nanoAvg.add(new Double(n2 - n1));
memAvg.add(new Double(m2 - m1));
}
public static void testAddByteToArray1() throws InterruptedException {
System.gc();
long m1 = getMemory();
long n1 = System.nanoTime();
for (int i = 0; i < 100000; i++) {
addByteToArray1();
}
long n2 = System.nanoTime();
System.gc();
long m2 = getMemory();
byteArray = new byte[0];
nanoAvg.add(new Double(n2 - n1));
memAvg.add(new Double(m2 - m1));
}
public static void resetMem() {
nanoAvg = new ArrayList<>();
memAvg = new ArrayList<>();
}
public static Double getAvg(List<Double> dl) {
double max = Collections.max(dl);
double min = Collections.min(dl);
double avg = 0;
boolean found = false;
for (Double aDouble : dl) {
if (aDouble < max && aDouble > min) {
if (avg == 0) {
avg = aDouble;
} else {
avg = (avg + aDouble) / 2d;
}
found = true;
}
}
if (!found) {
return getPopularElement(dl);
}
return avg;
}
public static double getPopularElement(List<Double> a) {
int count = 1, tempCount;
double popular = a.get(0);
double temp = 0;
for (int i = 0; i < (a.size() - 1); i++) {
temp = a.get(i);
tempCount = 0;
for (int j = 1; j < a.size(); j++) {
if (temp == a.get(j))
tempCount++;
}
if (tempCount > count) {
popular = temp;
count = tempCount;
}
}
return popular;
}
public static void testCompare() throws InterruptedException {
for (int j = 0; j < 4; j++) {
for (int i = 0; i < 20; i++) {
testAddByteToArray1();
}
System.out.println("testAddByteToArray1\tnanoAvg:" + getAvg(nanoAvg).longValue() + "\tmemAvg:" + getAvg(memAvg).longValue());
resetMem();
for (int i = 0; i < 20; i++) {
testAddByteToList1();
}
System.out.println("testAddByteToList1\tnanoAvg:" + getAvg(nanoAvg).longValue() + "\t\tmemAvg:" + getAvg(memAvg).longValue());
resetMem();
}
}
private static long getMemory() {
Runtime runtime = Runtime.getRuntime();
return runtime.totalMemory() - runtime.freeMemory();
}
public static void main(String[] args) throws InterruptedException {
testCompare();
}
}
You can try below solution inside some class:
int[] a = {10, 20, 30, 40, 50, 61};
// private visibility - or change it as needed
private void resizeArray(int newLength) {
a = Arrays.copyOf(a, a.length + newLength);
System.out.println("New length: " + a.length);
}
It is not possible to resize an array. However, it is possible change the size of an array through copying the original array to the newly sized one and keep the current elements. The array can also be reduced in size by removing an element and resizing.
import java.util.Arrays
public class ResizingArray {
public static void main(String[] args) {
String[] stringArray = new String[2] //A string array with 2 strings
stringArray[0] = "string1";
stringArray[1] = "string2";
// increase size and add string to array by copying to a temporary array
String[] tempStringArray = Arrays.copyOf(stringArray, stringArray.length + 1);
// Add in the new string
tempStringArray[2] = "string3";
// Copy temp array to original array
stringArray = tempStringArray;
// decrease size by removing certain string from array (string1 for example)
for(int i = 0; i < stringArray.length; i++) {
if(stringArray[i] == string1) {
stringArray[i] = stringArray[stringArray.length - 1];
// This replaces the string to be removed with the last string in the array
// When the array is resized by -1, The last string is removed
// Which is why we copied the last string to the position of the string we wanted to remove
String[] tempStringArray2 = Arrays.copyOf(arrayString, arrayString.length - 1);
// Set the original array to the new array
stringArray = tempStringArray2;
}
}
}
}
Sorry, but at this time is not possible resize arrays, and may be never will be.
So my recommendation, is to think more to find a solution that allow you get from the beginning of the process, the size of the arrays that you will requiere. This often will implicate that your code need a little more time (lines) to run, but you will save a lot of memory resources.
We can't do that using array datatype. Instead use a growable array which is arrayList in Java.
I have a global ArrayList list declared with the intention of adding a float variable to it in a method:
ArrayList<Float[]> list = new ArrayList<Float[]>();
Here is the method:
public void recieve(float[] coords)
{
this.list.add(?);
}
What is the syntax for adding coords to the ArrayList?
You will have to convert it manually I think.
public void recieve(float[] coords) {
this.list.add(convertToFloat(coords));
}
public Float[] convertToFloat(float[] coords) {
Float[] converted = new Float[coords.length];
for (int i = 0; i < coords.length; i++) {
converted[i] = Float.valueOf(coords[i]));
}
return converted;
}
You're looking for this:
public void receive(float[] coords) { // fixed misspelling in name
Float[] fCoords = new Float[coords.length];
for (int i = 0; i < coords.length; i++)
fCoords[i] = coords[i]; // autoboxing takes place here
this.list.add(fCoords);
}
Because the ArrayList expects a Float[], but you have a float[] as parameter (notice the difference in letter case!) a manual conversion is required before adding it to the list.
Since you declare Arraylist type as float array, I believe you can just use as
list.add(coords);
Cheers
This question already has answers here:
How to add new elements to an array?
(19 answers)
Closed 6 years ago.
Want to add or append elements to existing array
int[] series = {4,2};
now i want to update the series dynamically with new values i send..
like if i send 3 update series as int[] series = {4,2,3};
again if i send 4 update series as int[] series = {4,2,3,4};
again if i send 1 update series as int[] series = {4,2,3,4,1}; so on
How to do it????
I generate an integer every 5 minutes in some other function and want to send to update the int[] series array..
The length of an array is immutable in java. This means you can't change the size of an array once you have created it. If you initialised it with 2 elements, its length is 2. You can however use a different collection.
List<Integer> myList = new ArrayList<Integer>();
myList.add(5);
myList.add(7);
And with a wrapper method
public void addMember(Integer x) {
myList.add(x);
};
try this
public static void main(String[] args) {
int[] series = {4,2};
series = addElement(series, 3);
series = addElement(series, 1);
}
static int[] addElement(int[] a, int e) {
a = Arrays.copyOf(a, a.length + 1);
a[a.length - 1] = e;
return a;
}
If you are generating an integer every 5 minutes, better to use collection. You can always get array out of it, if required in your code.
Else define the array big enough to handle all your values at runtime (not preferred though.)
You'll need to create a new array if you want to add an index.
Try this:
public static void main(String[] args) {
int[] series = new int[0];
int x = 5;
series = addInt(series, x);
//print out the array with commas as delimiters
System.out.print("New series: ");
for (int i = 0; i < series.length; i++){
if (i == series.length - 1){
System.out.println(series[i]);
}
else{
System.out.print(series[i] + ", ");
}
}
}
// here, create a method
public static int[] addInt(int [] series, int newInt){
//create a new array with extra index
int[] newSeries = new int[series.length + 1];
//copy the integers from series to newSeries
for (int i = 0; i < series.length; i++){
newSeries[i] = series[i];
}
//add the new integer to the last index
newSeries[newSeries.length - 1] = newInt;
return newSeries;
}
Like others suggested you are better off using collection. If you however for some reason must stick to array then Apache Commons ArrayUtils may help:
int[] series = {4,2};
series = ArrayUtils.add(series, 3); // series is now {4,2,3}
series = ArrayUtils.add(series, 4); // series is now {4,2,3,4};
Note that the add method creates a new array, copies the given array and appends the new element at the end, which may have impact on performance.
You could also try this.
public static int[] addOneIntToArray(int[] initialArray , int newValue) {
int[] newArray = new int[initialArray.length + 1];
for (int index = 0; index < initialArray.length; index++) {
newArray[index] = initialArray[index];
}
newArray[newArray.length - 1] = newValue;
return newArray;
}
The size of an array can't be changed. If you want a bigger array you have to create a new array.
However, a better solution would be to use an (Array)List which can grow as you need it. The method ArrayList.toArray(T[] a) returns an array if you need to use an array in your application.
public int[] return_Array() {
int[] a =new int[10];
int b = 25;
for(int i=0; i<10; i++) {
a[i] = b * i;
}
return a;
}
import java.util.Arrays;
public class NumberArray {
public static void main(String []args){
int[] series = {4,2};
int[] newSeries = putNumberInSeries(1,series);
System.out.println(series==newSeries);//return false. you won't get the same int[] object. But functionality achieved.
}
private static int[] putNumberInSeries(int i, int[] series) {
int[] localSeries = Arrays.copyOf(series, series.length+1);
localSeries[series.length] = i;
System.out.println(localSeries);
return localSeries;
}
}
The ... can only be used in JDK 1.5 or later. If you are using JDK 4 or lower, use this code:'
public static int[] addElement(int[] original, int newelement) {
int[] nEw = new int[original.length + 1];
System.arraycopy(original, 0, nEw, 0, original.length);
nEw[original.length] = newelement;
}
otherwise (JDK 5 or higher):
public static int[] addElement(int[] original, int... elements) { // This can add multiple elements at once; addElement(int[], int) will still work though.
int[] nEw = new int[original.length + elements.length];
System.arraycopy(original, 0, nEw, 0, original.length);
System.arraycopy(elements, 0, nEw, original.length, elements.length);
return nEw;
}
Of course, as many have mentioned above, you could use a Collection or an ArrayList, which allows you to use the .add() method.
class AddElement {
public static void main(String s[]) {
int arr[] ={2,3};
int add[] = new int[arr.length+1];
for(int i=0;i<add.length;i++){
if(i==add.length-1){
add[i]=4;
}else{
add[i]=arr[i];
}
System.out.println(add[i]);
}
}
}
This works for me:
int[] list = new int[maximum];
for (int i = 0; i < maximum; i++{
list[i] = put_input_here;
}
This way, it's simple, yet efficient.
similar to Evgeniy:
int[] series = {4,2};
add_element(3);
add_element(4);
add_element(1);
public void add_element(int element){
series = Arrays.copyOf(series, series.length +1);
series[series.length - 1] = element;
}
int[] oldArray = {1,2,3,4,5};
//new value
int newValue = 10;
//define the new array
int[] newArray = new int[oldArray.length + 1];
//copy values into new array
for(int i=0;i < oldArray.length;i++)
newArray[i] = oldArray[i];
//another solution is to use
//System.arraycopy(oldArray, 0, newArray, 0, oldArray.length);
//add new value to the new array
newArray[newArray.length-1] = newValue;
//copy the address to the old reference
//the old array values will be deleted by the Garbage Collector
oldArray = newArray;